C#串行数据被分成随机数,试图找到回车没有运气

时间:2018-07-21 14:39:36

标签: c# arduino

您好,我已经制作了一个C#应用程序以从Arduino读取串行数据,并且仅当在发送数据时我在Arduino上使用Serial.println时,由于使用ReadLine(),我的应用程序才能正确获取所有数据,由于\ n \ r Arduino附有打印线。

但是,如果我只是使用不带\ n的Serial.print从Arduino中正常发送数据,并因此使用ReadExisting(),则我无法立即获得所有文本,而我可能会随机获得一半的数量流或仅2个字符,然后是10和6,则完全是随机的。我试图将文本放置在char数组中,并使用Contains或EndsWith以及readChar和readByte,寻找没有运气的“空或CR或ETX或EOT”。 ascii table

我该怎么办才能检测到流的结束,欢迎任何指导。

编辑::经过一番思考,我发现即使使用port.ReadExisting()随机分割了数据,文本的缺失部分也很快出现了,远小于Arduino每次传输之间的间隔为1此刻第二。因此,如果下一个数据包的时间临近,我将尝试将它们连接在一起。

这是我为收到的文本的每个部分制作的简单日志,请注意,我现在将其作为csv格式用于仅测试我的查找功能。并非总是这种格式。

Current mills: 1532199847091 ms   <=================================== START OF PROGRAM ON ARDUINO currentMills - previousMills, .....previousMills = currentMills 
elapsed time: 1532199847091 ms
receviedText: Starting
,test1,test2,test3,test4,test5,test6,test7


Current mills: 1532199848085 ms
elapsed time: 994 ms
receviedText: ,test1,test2,test3,test4,test5,test6,test7


Current mills: 1532199849068 ms
elapsed time: 983 ms
receviedText: ,test1,test2,test3,test4,test5,t

Current mills: 1532199849072 ms
elapsed time: 4 ms
receviedText: est6,test7


Current mills: 1532199850068 ms
elapsed time: 996 ms
receviedText: ,test1,test2

Current mills: 1532199850072 ms
elapsed time: 4 ms
receviedText: ,test3,test4,test5,test6,test7


Current mills: 1532199851071 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,test5,test6,te

Current mills: 1532199851075 ms
elapsed time: 4 ms
receviedText: st7


Current mills: 1532199852070 ms
elapsed time: 995 ms
receviedText: ,test1,test2,test3,

Current mills: 1532199852074 ms
elapsed time: 4 ms
receviedText: test4,test5,test6,test7


Current mills: 1532199853074 ms
elapsed time: 1000 ms
receviedText: ,test1,test2,test3,test4,test5,test6,test7


Current mills: 1532199854073 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,t

Current mills: 1532199854077 ms
elapsed time: 4 ms
receviedText: est5,test6,test7


Current mills: 1532199855072 ms
elapsed time: 995 ms
receviedText: ,test1

Current mills: 1532199855077 ms
elapsed time: 5 ms
receviedText: ,test2,test3,test4,test5,test6,test7


Current mills: 1532199856076 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,test5,te

Current mills: 1532199856080 ms
elapsed time: 4 ms
receviedText: st6,test7


Current mills: 1532199857075 ms
elapsed time: 995 ms
receviedText: ,test1,test2,

Current mills: 1532199857079 ms
elapsed time: 4 ms
receviedText: test3,test4,test5,test6,test7


Current mills: 1532199858079 ms
elapsed time: 1000 ms
receviedText: ,test1,test2,test3,test4,test5,test6,tes

Current mills: 1532199858082 ms
elapsed time: 3 ms
receviedText: t7


Current mills: 1532199859078 ms
elapsed time: 996 ms
receviedText: ,test1,test2,test3,t

Current mills: 1532199859082 ms
elapsed time: 4 ms
receviedText: est4,test5,test6,test7


Current mills: 1532199860081 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,test5,test6,test7


Current mills: 1532199861081 ms
elapsed time: 1000 ms
receviedText: ,test1,test2,test3,test4,te

Current mills: 1532199861085 ms
elapsed time: 4 ms
receviedText: st5,test6,test7


Current mills: 1532199862080 ms
elapsed time: 995 ms
receviedText: ,test1

Current mills: 1532199862084 ms
elapsed time: 4 ms
receviedText: ,test2,test3,test4,test5,test6,test7


Current mills: 1532199863083 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,test5,tes

Current mills: 1532199863087 ms
elapsed time: 4 ms
receviedText: t6,test7


Current mills: 1532199864083 ms
elapsed time: 996 ms
receviedText: ,test1,test2,t

Current mills: 1532199864087 ms
elapsed time: 4 ms
receviedText: est3,test4,test5,test6,test7


Current mills: 1532199865086 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,test5,test6,test

Current mills: 1532199865090 ms
elapsed time: 4 ms
receviedText: 7


Current mills: 1532199866085 ms
elapsed time: 995 ms
receviedText: ,test1,test2,test3,te

Current mills: 1532199866089 ms
elapsed time: 4 ms
receviedText: st4,test5,test6,test7


Current mills: 1532199867086 ms
elapsed time: 997 ms
receviedText: ,

Current mills: 1532199867090 ms
elapsed time: 4 ms
receviedText: test1,test2,test3,test4,test5,test6,test7


Current mills: 1532199868089 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,tes

Current mills: 1532199868093 ms
elapsed time: 4 ms
receviedText: t5,test6,test7


Current mills: 1532199869088 ms
elapsed time: 995 ms
receviedText: ,test1,t

Current mills: 1532199869092 ms
elapsed time: 4 ms
receviedText: est2,test3,test4,test5,test6,test7


Current mills: 1532199870092 ms
elapsed time: 1000 ms
receviedText: ,test1,test2,test3,test4,test5,test

Current mills: 1532199870096 ms
elapsed time: 4 ms
receviedText: 6,test7


Current mills: 1532199871091 ms
elapsed time: 995 ms
receviedText: ,test1,test2,te

Current mills: 1532199871095 ms
elapsed time: 4 ms
receviedText: st3,test4,test5,test6,test7


Current mills: 1532199872095 ms
elapsed time: 1000 ms
receviedText: ,test1,test2,test3,test4,test5,test6,test7

Current mills: 1532199872098 ms
elapsed time: 3 ms
receviedText: 


Current mills: 1532199873094 ms
elapsed time: 996 ms
receviedText: ,test1,test2,test3,tes

Current mills: 1532199873098 ms
elapsed time: 4 ms
receviedText: t4,test5,test6,test7


Current mills: 1532199874093 ms
elapsed time: 995 ms
receviedText: ,

Current mills: 1532199874097 ms
elapsed time: 4 ms
receviedText: test1,test2,test3,test4,test5,test6,test7


Current mills: 1532199875098 ms
elapsed time: 1001 ms
receviedText: ,test1,test2,test3,test4,test

Current mills: 1532199875101 ms
elapsed time: 3 ms
receviedText: 5,test6,test7


Current mills: 1532199876096 ms
elapsed time: 995 ms
receviedText: ,test1,te

Current mills: 1532199876100 ms
elapsed time: 4 ms
receviedText: st2,test3,test4,test5,test6,test7


Current mills: 1532199877099 ms
elapsed time: 999 ms
receviedText: ,test1,test2,test3,test4,test5,test6

Current mills: 1532199877103 ms
elapsed time: 4 ms
receviedText: ,test7

0 个答案:

没有答案