Click here to Skip to main content
15,920,576 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day to all,
Please I need your support on how to resolve this problem. I am using the function below to read Bytes from FTDI2232D Device(Serial Port)and then display the value on my GUI. If I send two bytes, for example 110 and 40,
I am expecting 110 to be displayed on label3 while 40 is displayed on label5.However instead of displaying 110 and 40 respectively, both label3 and label5 displayed the same value that is 110 or 40(the microcontroller sends the two bytes to the serial port continuosely ).So the two labels always have the same value at any giving time(that is 110 or 40) instead of different values. When I debugged in order to check the content of both readData[0] and readData[1],they both contained the same value.
I would be glad if somebody could help me have a look at the codes below with a view to identifying the error .Thank you for the usual support.



C#
private void rxtemp_CheckedChanged(object sender, EventArgs e)
       {
      
           UInt32 numBytesRead = 2;           //     The number of bytes actually read.
           UInt32 numBytesToRead = 2;         //     The number of bytes requested from the device.
           byte[]  readData = new byte[5];

           Thread th = new Thread(() =>
               {
                   while (true)
                   {
                       ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
                     
                           label3.Invoke(new Action(() =>
                           {
                               label3.Text = readData[0] + "ºC";


                           }));


                           label5.Invoke(new Action(() =>
                           {

                               label5.Text = readData[1] + "ºC";

                           }));
                    
                   }
                   
                    
               });
           th.IsBackground = true;
           th.Start(); 
       }
Posted
Updated 12-Jan-15 4:33am
v2
Comments
Jochen Arndt 12-Jan-15 7:51am    
You should check the return value stored in ftStatus and the number of bytes that has been actually read in numBytesRead.

Because your controller sends continuously, the RX buffer may have been already filled. To avoid this call FT_Purge just before starting receipton of data.

Well, what did you expect?
If they device is continuously sending two byte values: 110 then 40 and you always read two bytes at a time, then the first byte will always be teh same, and so will teh second byte.

If you pretend the device is continuously sending 'A' followed by 'B' (to make my typing easier) then it just sends
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
Then it doesn't matter where in that stream of data your code starts to read two byte pairs, the two labels will always get the same value: either 'A' in label 1 and 'B' in label 2 or vice versa. There is no other data to display!
 
Share this answer
 
You need to cache the actual value in the actions, like this:
C#
label3.Invoke(new Action(() =>
{
 var value = readData[0];
 label3.Text = value + "ºC";
}));

label5.Invoke(new Action(() =>
{
 var value = readData[1];
 label5.Text = value + "ºC";
}));


Good luck!
 
Share this answer
 
Another problem is that you appear to have no framing information. What if the data you get is corrupted? How are you going to know which values you're actually looking at?

Unless you don't really care which byte ends up in which textbox you need to pack up your data on the microcontroller and send it to the PC in a known format. If you're using a language on the microcontroller that can serialize a structure for you, that makes it easy. A nice example would be JSON. If not...

You need to send your data in a packet that contains something of a packet start sequence, the data in some format, and an end sequence and possibly a checksum value. Your application code would have to receive the packet, validate it and deserialize the data into a structure in your code. You can then pass that structure to some method that updates your UI.

If your using two-way communication with the micro, then you can even put acknowledgements into your protocol so that the micro knows that the PC received the data and doesn't need to send it again.
 
Share this answer
 

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