Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I want to migrate one VB6 project to C#. In VB6 project MSCOMM control is used for serial port communication. Instead of using same control in C# application I have used serial port class.

Initially I am sending 4 consecutive commands to serial port. When I tried to debug the code I found DataReceive event is working in separate thread.

I need 15 bytes response for first command 17 bytes response to second command.
But as we know DataReceive event for one byte also.

I want to get whole 15 bytes response for first command and then want to send second command.

I have tried following code.

C#
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (serialPort1.ReceivedBytesThreshold > sent_command.Length)
            {
                string data = serialPort1.ReadExisting();
                int bufferLengh = data.Length;
                string output_str;

               char[] array = data.ToCharArray();
               string final = "";
               foreach (var i in array)
               {
                   string hex = String.Format("{0:X}", Convert.ToInt32(i));
                   final += hex.Insert(0, "") ;
               }
               final = final.TrimEnd();

               output_str = final;
                MessageBox.Show(output_str);
            }
        }


by using above code I am getting data filled with one or two bytes and rest data bytes as "0" but data byte length is correct. (for first command it is 15 and second command 17 bytes)

or some times data = ""

My question is how to wait in data receive event to get whole response and then it will receive the response for second command.

In VB6 application one polling timer is used.
If I used a timer in C# application then in that timer event timeout exception get fired and data receive event does not get fired.

Please help me to resolve this.

Thanks in advance.
Posted
Comments
Richard MacCutchan 29-Jan-15 3:43am    
The ReadExisting method reads all the data that is in the serial port's buffer. But there may still be data waiting to be transferred from the device, so you need some other method of determining when all the data has been presented. You should check the device documentation to see how to be certain that you have all the available data.
Member 11379036 29-Jan-15 5:03am    
Yes there is check for full data received or not.

But my question is how to again wait in the data received event to satisfy that condition.
Because now in the current code I am facing a problem as 2-3 data byes received from command one and then application gets stopped or proceed to read response for next command.
I want whole data received for one command and then application should end or read next command if available.
Member 11379036 30-Jan-15 0:05am    
Can any one help me on this.
I am stuck at this point.

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