Click here to Skip to main content
16,021,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. Permanent division of the data from my problem. The code below. Thanks.

HOST2 send message sample : 12634567897897TESTMESSAGE

HOST 1 Result screen :

HOST1 : Port is open.
HOST2 : 126345678978
HOST2 : 97TESTMESSAGE

Received message randomly split. Not a single line.

My request:

HOST1 : Port is open.
HOST2 : 12634567897897TESTMESSAGE

Is it possible in this way

C#
private string indata;

       private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               serialPort1.PortName = portBox.Text;
               serialPort1.BaudRate = 9600;
               serialPort1.DataBits = 8;
               serialPort1.StopBits = StopBits.One;
               serialPort1.ReadTimeout = -1;
               serialPort1.ReadBufferSize = 4096;
               serialPort1.WriteTimeout = -1;
               serialPort1.Parity = Parity.None;
               serialPort1.Handshake = Handshake.None;
               serialPort1.Open();
               richTextBox1.Text += "HOST1 : Port is open.\r\n";
           }
           catch//(Exception ex)
           {
               richTextBox1.Text += "HOST1 : Port is not open.\r\n";
           }

       }

       private void button2_Click(object sender, EventArgs e)
       {
           serialPort1.Write("\u0005\r\n");
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           portBox.Items.Clear();
           string[] ports = SerialPort.GetPortNames();
           foreach (string Comport in ports)
           {
               portBox.Items.Add(Comport);
           }
       }

       private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           indata = serialPort1.ReadExisting();

           richTextBox1.Invoke((MethodInvoker)delegate { richTextBox1.Text += "HOST2 : " + indata + "\r\n"; });

       }
Posted

You use ReadExisting[^] for reading data. This function returns immediately returning the data actually available in the input buffer (which might be even no data). So you should collect received data until some kind of end of data is received or a timeout occurs.

When the answer is terminated with a new line, you might use ReadLine[^] instead. But you should set a timeout value then to avoid infinite blocking if no answer is received.
 
Share this answer
 
Comments
[no name] 21-Nov-15 7:05am    
That sounds good, a 5.
RS232 is way slow. RS232 receive chars 1 by 1.

You need to make sure that you have received the complete message.
Either you know the length of message, or you know the end char of the message, or you simply wait for a timeout to tell you that the message is over.

Timeout is mandatory since the message can be interrupted.

I am C# expert, but 1 know RS232.
 
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