Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 .

output on Access Port

The weight on the scales = 3.900 kg

in picture =009.300

baud rate 1200

When I use this code

            string s = "";
            int num8 = 0;
            string RST = "";
            while (this.serialPort1.BytesToRead > 0)
            {
                string data = serialPort1.ReadExisting();
                if (data != null)
                {

                    if (data.ToString() != "")
                    {
                        if (data.Length > 6)
                        {
                            RST =  data.Substring(6, 1) + data.Substring(5, 1) + data.Substring(4, 1) + data.Substring(3, 1) + data.Substring(2, 1);

                            this.textBox4110.Text = RST.ToString();
                        }
                    }
                }
            }

output in my program

When I use the above code in the program Sometimes displays the weight number and sometimes does not. I have to open and close the program several times. And by changing the weight on the scale, its number does not change on the program and the display value is fixed.

and When I use this code

 while (this.serialPort1.BytesToRead > 0)
{
int data = serialPort1.ReadByte();
 this.textBox4110.Text = data.ToString();
}

in my program Displays the number 48

What should I do?

thanks regards


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
347 views
Welcome To Ask or Share your Answers For Others

1 Answer

I don't know why your serial port sometimes responds and sometimes doesn't. I worked with RS232 years ago and never had this problem.

About other questions:

  • You're working with a byte array, can't call the ToString, since it will convert to string the byte rappresentation
  • If you have to reverse the bites order (4 - 3 - 2 - 1), you can call the Array.Reverse method

Just for make an example about what I mean, I took your code:

while (this.serialPort1.BytesToRead > 0)
{
   int data = serialPort1.ReadByte();
   this.textBox4110.Text = data.ToString();
}

your "data" variable contains a byte with value 48 that is the 0 char in ASCII table. So, if you want the char, you have to convert it using the right encoding. Suppose you are working with UTF8:

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;
}

Honestly I know the Encoding.UTF8.GetString accept a byte array, not sure it will work only with a single byte...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...