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.
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.