Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to get the data from a weighing scale indicator using rs232 but the received data is looping and in reversed order. For example, the weight is 9530 in the indicator but when displayed it's .035900

I want to display the data in textbox without it looping and in proper order.

What I mean about looping is like when the data is .035900 it displays like this .035900.035900.035900.035900.035900.035900.035900.035900.035900.035900 so on... with an interval of I think 1 sec.

I'm not familiar with serial port so please bear with me I'm a newbie.

What I have tried:

private void button2_Click(object sender, EventArgs e)
       {
           if(_serialPort != null && !_serialPort.IsOpen) _serialPort.Close();
           if (_serialPort != null) _serialPort.Dispose();

           _serialPort = new SerialPort(toolStripPort.Text, BaudRate, Parity.None, 8, StopBits.One);
           _serialPort.DataReceived += SerialPortOnDataReceived;
           _serialPort.Open();
       }


       private delegate void Closure();
       private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
       {
           if(InvokeRequired)
               BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));
           else
           {
                   String readData = _serialPort.ReadExisting();
                   Regex regex = new Regex(@"[^\d|\.]");
                   readData = regex.Replace(readData, "");

               if(readData.Count() == 1)
               {
                   richTextBox1.Text += string.Format("{0:X2} ", readData);
               }else if(readData.Count() > 1)
               {
                   _serialPort.Close();
               }

           }

       }
Posted
Updated 10-Jul-22 20:35pm
Comments
[no name] 11-Jul-22 13:47pm    
If this has a "user interface" (as part of an app), you don't need to keep looping if you have a "Weigh" button / function that the user can invoke when he is ready. There's things like "scale in motion", "centering", etc. you have to deal with and it gets a lot harder when you don't know "when" it's important.

1 solution

Quote:
I'm not familiar with serial port so please bear with me I'm a newbie.
Both of your problems have probably nothing to do with the serial communication itself. On the opposite, they are related to the weighing scale you are using (you kept it secret!).
I think that 'looping' is a normal scenario: the scale sends periodically unsolecited output. About 'reversed digits', I don't know.
Anyway, you have to read carefully the device manual, in order to proper interpretate its output data.
 
Share this answer
 
Comments
Nico Ga-ang 11-Jul-22 2:49am    
Thank you for your time.
I think I found out about the reversed digits, probably the device is sending a little-endian byte. What I am thinking is I might just reverse the string using Text.Reverse().ToArray().

Can you tell me a solution for the looping effect? is there something I can do to the textbox to display data periodically sent by serial port?
CPallini 11-Jul-22 2:58am    
It looks more 'ASCII reversed' than 'little-endian byte'.
In order to 'display the data periodically sent' update, in SerialPortOnDataReceived handler, just update your text box according to the received data.
Nico Ga-ang 11-Jul-22 3:31am    
If by any chance, can you give me a code snippet on how to do the 'update your text box according to the received data'?

should I use this AppendText(string.Format("{0:X2} ", readData)); ?
Ralf Meier 11-Jul-22 3:57am    
you should 1st identify what is one weighting-sequence - I suppose that each time you get a string from your unit it is the actual value. So I suggest you not append your textbox (richTextBox1.Text += ...) but assign to it (richTextBox1.Text = ...).

but ... what device you are using - I think that your "problem" is to be solved with paramters of the device ...
Nico Ga-ang 11-Jul-22 6:15am    
Weight Indicator XK3118K8. That is the model of the device.

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