I want to read data from weighing scale data via RS232 and i try any more way
my weighing scale model yh-t7e datasheet
The output of the scale on the AccessPort program is this value.
image link
The weight on the scales = 3.900 kg
in picture =009.300
baud rate 1200
this code when run write to text box all result like =009.300=009.300=009.300=009.300=009.300
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e){
while (_serialPort.BytesToRead > 0)
{
hex += string.Format("{0:X2} ", _serialPort.ReadByte());
}
textBox1.Text = String.Empty;
if (hex != "")
{
byte[] data = FromHex(hex.Trim());
int lengthOfNumbers = data.Length;
textBox2.Text = lengthOfNumbers.ToString();
textBox1.Text = Encoding.ASCII.GetString(data, 0, data.Length).Trim();
}
else
{
textBox1.Text = "no";
}
}
}
public byte[] FromHex(string aHex)
{
aHex = aHex.Replace(" ", "");
byte[] raw = new byte[aHex.Length / 2];
for (int i = 0; i < raw.Length; i++)
{
raw[i] = Convert.ToByte(aHex.Substring(i * 2, 2), 16);
}
return raw;
}
And I tried this code too
private void timer2_Tick(object sender, EventArgs e)
{
while (this.serialPort1.BytesToRead > 0)
{
var dataLen = this.serialPort1.BytesToRead;
var byteArray = new byte[dataLen];
this.serialPort1.Read(byteArray, 0, dataLen);
var txt = Encoding.UTF8.GetString(byteArray);
this.textBox4110.Text = txt;
}
}
I put the code inside a timer and it shows the weight value with each refresh,
but it shows a different value each time.
For example, if the weight is 50,000 kg.
The number of places changes with each refresh.
Once 50,000
Once 050.00
Once 00.500
and ….
What I have tried:
.read()
.readbyte()
.readexisting()