接收串口数据(窗口应用程序) [英] Receive serial port data(window application)

查看:162
本文介绍了接收串口数据(窗口应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要接收5字节的串行端口数据.

我想要的代码是,如果第一次接收2字节的数据,然后在接收第二次数据时添加3字节的数据.并且如果有更多的3字节数据,则在其他字符串数组中添加其他数据.
它不是静态数据,它可能第一次接收3个字节的数据,然后第二次添加2个字节的数据.

I want to receive serial port data of 5 byte.

I want code for if first time receive 2 byte of data then add 3 byte of data when receive second time data. And if there is more the 3 byte of data add other data in other string array.
It is not static data it may receive 3 byte data in first time then add 2 byte data in it second time.

推荐答案

如果您不知道有多少个您将要接收的字节,然后我将处理DataReceived事件并获取所有可用字节.然后,我将它们组装成一个字符串或数组,并以此进行处理.如果您知道它将始终为五个字节,则等待总共五个字节,然后在末尾对其进行排序.


"
If you don''t know how many bytes you are going to receive, then I would handle the DataReceived event and fetch all available bytes. I would then assemble these into a string or array and process them as that. If you know it is always going to be five bytes, then wait for a total of five, then sort it out at the end.


"
look suppose i have like this data
ff ff 85 01 02 03 04 05 dd 02 cc dd 11 22

in this i convert 85 in to binary and compare with 0000 0000 if first digit is 1 then take 01 02 03 04 05 and first digit is 0 then take only 01. now my problem is first time i receive ff ff 85 01 02 so i store 01 02 in one string array is string [] address. Now second time i receive 03 04 05 dd 02 so i want to store 03 04 05 in address string. This is my problem.


"


因此FF 16 是空白,什么也没有发生",而85 16 开始您的数据序列. DD 16 或CC 16 终止每个词组. (或更可能是设置了最高位的任何内容).
处理DataRecieved事件,并使用基于状态的计算机:等待启动",接收数据",等待新短语"或类似内容将起作用-我不知道您的数据流的详细信息,所以我不得不猜测.
根据收到的内容,在组装时将状态从一个状态移到另一个状态,并发信号通知自己的可用数据事件(可能是通过自定义EventArgs传递数据).

哪一部分比较困难?


"


So FF16 is "blank, nothing happening" and 8516 starts your data sequence. DD16 or CC16 terminates each phrase. (or more likely, anything with the top bit set).
Handle the DataRecieved event, and use a state based machine: "Waiting to start", "Receiveing data" Waiting for new phrase" or similar will do - I don''t know the details of your data stream, so I have to guess.
depending on what you receive, move from state to state as necessary, and signal a data available event of your own (probably with a custom EventArgs to pass the data) when you have assembled it.

What part of this is difficult?


您需要一个FIFO缓冲区.取一个List<byte>.您收到的每个字节都通过Add()AddRange()进入该缓冲区.
然后,您可以检查缓冲区中的有效信息.最有可能的是,您将使用一些定界符字节序列.如果存在满足您要求的块,请通过GetRange()复制它,并使用RemoveRange()从缓冲区中将其删除.
别忘了正确地lock.
You need a FIFO buffer. Take a List<byte>. Every byte you receive goes in that buffer via Add() or AddRange().
Then, you can check the buffer for valid information. Most likely, you will use some delimiter byte sequence. If there is a block that suits your requirements, copy it via GetRange() and delete it from buffer using RemoveRange().
Don''t forget to lock properly.


我为所有接收到的数据创建了切换案例语句,并记住了我最后添加数据的最后位置,然后在我收到新数据时添加了该数据.通过这种方式,在最后一个位置,我将在数据完成时获取所有数据,并赋予状态1并继续此过程.
I create switch case statement for all receive data and remember last position where i add last data then when i receive new data i add that data at last position by this way i get all data when data complete i give status 1 and continue this process.


这篇关于接收串口数据(窗口应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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