Click here to Skip to main content
15,887,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e){
        while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
        {
            //MessageBox.Show("byte");
            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


C#
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()
Posted
Updated 4-Jan-21 10:54am
v2

Couple of things here: firstly, that won't work. The DataRecieved event is not fired on the UI thread so you cannot access UI controls such as textboxes directly from within it.

The important thing to note about RS232 is this: it's slow. Very slow, in terms of your processor speed - and your baud rate is really slow, even for RS232!

A baud rate of 1200 means 1200 bits per second, and since RS232 requires start and stop bits, often a parity bit, and either 7 or 8 bits of data you should consider that as 1200 / 10 characters per second max, or 120 characters per second.

Which means that while your data is coming in, any PC capable of running a .NET app will be spending most of it's time "twiddling it's thumbs" - and that means that each time a single character arrives at your app you will get an individual DataRecieved event - so there is little point in processing anything until a "full weight" has been received - and if that "sample" is of any use, then it would appear to have either a leading of trailing "=" which you should be using to mark the boundaries of the data being received.

Assuming that you have read the manual for the weighing machine, I'd start with something like Hyperterminal to monitor exactly what the device is sending (using "known weights" to confirm that what you think is being received is accurate before starting to code based on assumptions (it's all too easy in this field to misinterpret what is happening)

Then what I would do is ignore the DataRecieved event completely, and use a BackgroundWorker class thread to assemble the full message, and pass it back to your UI code as a Progress message for it to process in it's entirety.
 
Share this answer
 
I have read the documentation of this device - it is not very good ... but one point seams clear for me :
With your code-snippet you read everytime all chacters inside the buffer. This might be wrong. I suggest you check (before reading) if there are 6 (or more characters inside the buffer. Now read only the first 6 (and not all of them) and convert them for the display. Also you could change your Timer-Interval (perhaps) to a lower value. If there are not 6 characters inside the buffer do nothing ...

Additional :
You could also use the '=' character to indicate a new sequence ...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900