i try to create c# application to read data from weighing scale (HLWagen IT1000). When using RS232 port , the application was able to detect and display the weighing data. Here is my code.
private void Weighing_Load(object sender, EventArgs e)
{
string[] availablePorts = SerialPort.GetPortNames();
if (!availablePorts.Contains(comPort))
{
MessageBox.Show("Make sure the printer is connected to COM1.", "Serial
Port Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
return;
}
// Close and dispose of any existing serial port
if (_serialPort != null && _serialPort.IsOpen)
{
_serialPort.Close();
}
if (_serialPort != null)
{
_serialPort.Dispose();
}
// Create and configure a new SerialPort object
_serialPort = new SerialPort(comPort, BaudRate, Parity.None, 8,StopBits.One);
_serialPort.DataReceived += SerialPortOnDataReceived;
try
{
_serialPort.Open(); // Open the serial port
}
catch (Exception ex)
{
MessageBox.Show("Error opening the serial port: " + ex.Message, "Serial
Port Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SerialPortOnDataReceived(object sender,
SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
if (InvokeRequired) //<-- Makes sure the function is invoked to work
properly in the UI-Thread
BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender,
serialDataReceivedEventArgs); })); //<-- Function invokes itself
else
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
string str = System.Text.Encoding.UTF8.GetString(data);
// Extract the weight measurement and "S S" tag from the incoming data
int startIndex = str.IndexOf("S S")+1;
if (startIndex < 0 || startIndex >= str.Length)
{
// Handle the error here, such as by logging the error or displaying
a message to the user
return;
}
int endIndex = str.IndexOf("k", startIndex);
if (endIndex < 0 || endIndex > str.Length)
{
// Handle the error here, such as by logging the error or displaying
a message to the user
return;
}
string weightStr = str.Substring(startIndex, endIndex - startIndex);
//test display
//MessageBox.Show(weightStr);
// Trim the "k" character from the weight measurement
weightStr = weightStr.TrimEnd('k');
// Display the weight measurement and "S S" tag in the label
weight_label.Text = $"{weightStr} kg";
weightStr = str.Substring(startIndex, endIndex - startIndex - 1);
current_weight =weightStr;
current_weight = float.Parse(weightStr);
using (StreamWriter writer = new StreamWriter("log_weight.txt", true))
{
writer.WriteLine(weightStr);
writer.Flush();
}
}
}
and here the result:
S D 2.535 kg
S D 2.535 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S S 2.534 kg
S D 2.575 kg
S D 2.555 kg
but When i used converter RS-232 USB to Serial converter, using same code the data is not consistence:
S S 2.570 kg
S D 2.565 kg
S D 2.535 kg
S D
2.
535 kg
S D
2.540
kg
S
D
2.550 kg
S
D
2.5
50 kg
S
D 2.
595 kg
S
D 2.
610 kg
S
D 2.
620 kg
S
D
2.625 kg
S
D 2.6
30 kg
S
D
2.630 k
g
S
D 2.6
25 kg
S
D 2.
620 kg
S
D 2.
600 kg
S
D 2
.615 kg
S
D
2.575 k
g
S
D 2
.565 kg
S D
2.555
kg
S
D
2.545
kg
S
D 2
.535 kg
S D
2.535
kg
S
D 2.
535 kg
S
D 2.
535 kg
S D
2.54
5 kg
S
D 2.
560 kg
S
D
2.555 k
g
S
D 2.
555 kg
S
D
2.550 kg
S D
2.550
kg
S
D 2.5
50 kg
S
D 2.5
45 kg
S
D 2.4
75 kg
S
D
2.45
5 kg
S
D 2
.490 kg
S D
2.4
50 kg
S
D
2.355 kg
S
D 1.
615 kg
S
D 1.
425 kg
S
D 1.
405 kg
S
D 1
.410 kg
i need help how to handle the data like this , thank you!