formating the buffer input from a serial port?

shay Avital

Member
Joined
Oct 9, 2020
Messages
12
Programming Experience
Beginner
hello,

i have a program where i read a list of values from an energy meter through a serial port. to read the buffer i use the ReadExisting() method. my problem is that the words in the second column have different length each raw so the list is not orginized well:

enter image description here




I am looking for a way to get it like that:

enter image description here



here is the code i use to read the buffer:
C#:
private void serialPort1_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
        {
            DataIn = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(Showdata));
        }

      
        private void Showdata(object sender, EventArgs e)
        {
           
                textBox1.Text += DataIn;
                textBox1.SelectionStart = textBox1.TextLength;
                textBox1.ScrollToCaret();
         }

how can i get the input from the buffer to be aligned?

thank you,

shay.

thanks
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,085
Location
Chesapeake, VA
Programming Experience
10+
Cheap solution: Use a fixed width font in the text box when showing what you got from the serial port.

Correct solution: Parse the input you get from the serial port and break it up into component parts. Then display the values in a proper GUI control that has proper columns like a data grid view or list view. If within each column, you need additional alignment, then prefer to use some kind of fixed width font.
 
Last edited:

shay Avital

Member
Joined
Oct 9, 2020
Messages
12
Programming Experience
Beginner
Cheap solution: Use a fixed width font in the text box when showing what you got from the serial port.

Correct solution: Parse the input you get from the serial port and break it up into component parts. Then display the values in a proper GUI control that has proper columns like a data grid view or list view. If within each column, you need additional alignment, then prefer to use some kind of fixed width font.
Thank you very much,
Can you please share a code example?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,085
Location
Chesapeake, VA
Programming Experience
10+
No.

For the cheap solution, just use your WinForms Designer and pick a font with fixed width. I like Bitstream Vera, but you can choose what you like.

For the correct solution, there is not enough information in this thread about the incoming data stream to determine an appropriate way to parse it. Yes, you may have a screenshot of the data, but more precision is needed to determine what kind of data to expect, when to expect it, size limits, field names, etc.
 

shay Avital

Member
Joined
Oct 9, 2020
Messages
12
Programming Experience
Beginner
No.

For the cheap solution, just use your WinForms Designer and pick a font with fixed width. I like Bitstream Vera, but you can choose what you like.

For the correct solution, there is not enough information in this thread about the incoming data stream to determine an appropriate way to parse it. Yes, you may have a screenshot of the data, but more precision is needed to determine what kind of data to expect, when to expect it, size limits, field names, etc.
OK, thanks again
 
Top Bottom