Click here to Skip to main content
15,071,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
Hi, i did an application on C# that send and receive data in the same time, the application is built with C# on visula studio 2008 smart device application.

i have to communicate with an external component. so i send data via a "PDA to this component" , and the componenet send me data after it receive data sent by my pda.

the problem is that my application can't receive all the data sent by this component. there is some missed data.

i shoud receive this data "6 6 2 83 71 0 1 3 209 41" but me i receive "2 63 71 0 63 209 41"

plaese help. thanks Smile | :) 


here is my code

C#
private void button2_Click(object sender, EventArgs e)
{


//first data
Thread.Sleep(70);
serialPort1.Write(new byte[] { 65 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 18 }, 0, 1);
Thread.Sleep(70);


// here i should get the nubmer 6 from the receiver


serialPort1.Parity = Parity.Odd; ////////

// second data

serialPort1.Write(new byte[] { 2 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 91 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 69 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 3 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 3 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 66 }, 0, 1);
Thread.Sleep(70);


// here i should get 6 from the receier




//third data

serialPort1.Write(new byte[] { 65 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 17 }, 0, 1);
Thread.Sleep(70);

// here i should get 2 83 71 0 1 3 209 41

// last data

serialPort1.Write(new byte[] { 6 }, 0, 1);
//serialPort1.Parity = Parity.None; ////////
} 



private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (serialPort1.BytesToRead >5)
{
data = serialPort1.ReadExisting();

char[] charValues = data.ToCharArray();

foreach (char _eachChar in charValues)
{
value = (_eachChar);
data2 += String.Format("{0:X}", value + " ");

}

this.Invoke(new EventHandler(display_data_event));// fonction temps réel
data2 = "";
}
}

private void display_data_event(object sender, EventArgs e)
{
textBox2.Text = data;
textBox4.Text = data2;
}


What I have tried:

private void button2_Click(object sender, EventArgs e)
{


//first data
Thread.Sleep(70);
serialPort1.Write(new byte[] { 65 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 18 }, 0, 1);
Thread.Sleep(70);


// here i should get the nubmer 6 from the receiver


serialPort1.Parity = Parity.Odd; ////////

// second data

serialPort1.Write(new byte[] { 2 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 91 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 69 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 3 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 3 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 66 }, 0, 1);
Thread.Sleep(70);


// here i should get 6 from the receier




//third data

serialPort1.Write(new byte[] { 65 }, 0, 1);
Thread.Sleep(70);
serialPort1.Write(new byte[] { 17 }, 0, 1);
Thread.Sleep(70);

// here i should get 2 83 71 0 1 3 209 41

// last data

serialPort1.Write(new byte[] { 6 }, 0, 1);
//serialPort1.Parity = Parity.None; ////////
}



private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (serialPort1.BytesToRead >5)
{
data = serialPort1.ReadExisting();

char[] charValues = data.ToCharArray();

foreach (char _eachChar in charValues)
{
value = (_eachChar);
data2 += String.Format("{0:X}", value + " ");

}

this.Invoke(new EventHandler(display_data_event));// fonction temps réel
data2 = "";
}
}

private void display_data_event(object sender, EventArgs e)
{
textBox2.Text = data;
textBox4.Text = data2;
}
Posted
Updated 20-May-16 10:03am

The problem is quite likely to be that you don't necessarily receive all the data in one event - Serial ports are slooooow, and data arrives byte-by-byte: so the DataReceived event will fire multiple times for each "message" received from your PDA device. It's quite possible that it will fire for each individual character!
And when you do accumulate five bytes, you display them, and overwrite the previous content, throwing it away.

I'd start by accumulating all the data (probably as raw bytes into a log file) and adding it to my text box instead of overwriting the content - particularly as the data you show doesn't match the code you give: it isn't hex data!
   
Comments
OriginalGriff 20-May-16 4:43am
   
"it's a decimal what i receive not hexa,"
Um...
data2 += String.Format("{0:X}", value + " ");
That's converting it to Hex - which your example isn't showing. And if you are receiving binary data, read the bytes from the serial port, not as a string: strings involve data conversion to Unicode, which may "swallow" or change some bytes.
"how do i to accumate all data please could you write me a peace of code"
You are kidding, right?
You can't concatenate strings on your own?
OriginalGriff 20-May-16 5:09am
   
"how do i read byte from the serial port"
https://msdn.microsoft.com/en-us/library/ms143549(v=vs.110).aspx
Shows you how.
To accumulate them in a log file, is easy:

using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(data);
bw.Close();
}
}

To accumulate them into a textbox is a case of building your string as you do in a loop, then using
myTextBox.Text += theStringIJustBuilt;
instead of
myTextBox.Text = theStringIJustBuilt;
which you use at the moment.

If you are a beginner and you don't know this stuff, then you are trying to punch well above your weight by trying to work with SerialPorts! Learn the basics first, or you will just get more and more confused... :laugh:
Did you try to use a terminal emulator as I suggested in first question ?

To debug a serial link, the first thing to do is to proof that the other half of the link works as expected. Proof link parameters and data you receive.
Once it is done, you know problems are on your side.
If you don't do it, you are in the dark trying random things.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900