从FTDI232D设备读取字节(串行端口) [英] Reading Bytes from a FTDI232D Device(Serial Port)

查看:184
本文介绍了从FTDI232D设备读取字节(串行端口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我需要你的支持,如何解决这个问题。我正在使用下面的函数从FTDI2232D设备(串行端口)读取字节,然后在我的GUI上显示该值。如果我发送两个字节,例如110和40,

我希望在label3上显示110,而在label5上显示40.但是,不是分别显示110和40,而是显示label3和label5相同的值是110或40(微控制器连续地将两个字节发送到串行端口)。因此,两个标签在任何给定时间(即110或40)总是具有相同的值而不是不同的值。当我调试以检查readData [0]和readData [1]的内容时,它们都包含相同的值。

如果有人可以帮我看一下,我会很高兴的以下代码,以识别错误。感谢您的支持。







< pre lang =c#> private void rxtemp_CheckedChanged( object sender,EventArgs e)
{

UInt32 numBytesRead = 2 ; // 实际读取的字节数。
UInt32 numBytesToRead = 2 ; // 从设备请求的字节数。
byte [] readData = new byte [ 5 ];

线程th = 线程(()= >
{
while true
{
ftStatus = myFtdiDevice .Read(readData,numBytesToRead, ref numBytesRead);

label3.Invoke( new Action(()= >
{
label3.Text = readData [ 0 ] + ºC;


} ));


label5.Invoke( new 行动(()= > ;
{

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

}));

}


});
th.IsBackground = true ;
th.Start();
}

解决方案

那么,你期待什么?

如果他们的设备不断发送两个字节值:110然后是40并且你总是一次读取两个字节,那么第一个字节将始终相同,因此第二个字节也是如此。



如果您假装设备持续发送'A'后跟'B'(为了让我的打字更容易),那么它只是发送

 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB ... 

然后,在您的代码开始读取两个字节对的数据流中的哪个位置无关紧要,这两个标签将始终获得相同的值:标签1中的A和标签2中的B,反之亦然。没有其他数据可以显示!


您需要在操作中缓存实际值,如下所示:

 label3 .Invoke( new 行动(()= >  
{
var value = readData [ 0 ];
label3.Text = value + ºC;
}));

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





祝你好运!


另一个问题是您似乎没有框架信息。如果您获得的数据已损坏怎么办?你怎么知道你实际看到的是哪些值?



除非你真的不在乎哪个字节最终需要打包在哪个文本框中您在微控制器上的数据并以已知格式发送到PC。如果您在微控制器上使用可以为您序列化结构的语言,则可以轻松实现。一个很好的例子是JSON。如果不是......



您需要将数据发送到包含数据包启动序列的数据包,某种格式的数据以及结束序列和可能是校验和值。您的应用程序代码必须接收数据包,验证它并将数据反序列化为代码中的结构。然后,您可以将该结构传递给更新UI的方法。



如果您使用micro进行双向通信,那么您甚至可以将确认信息放入协议中这样微观就知道PC收到了数据而不需要再发送数据。


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.



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(); 
       }

解决方案

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!


You need to cache the actual value in the actions, like this:

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!


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.


这篇关于从FTDI232D设备读取字节(串行端口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆