我使用C#和visual studio进行串口通信时遇到问题。 [英] I have a problem with serial port communication using C# and visual studio.

查看:349
本文介绍了我使用C#和visual studio进行串口通信时遇到问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Pic微控制器的系统,它定期发送一串数据。

Normaly我用HyperTerminal检查它,但现在我想使用我自己的小应用程序正确显示数据。

应用程序工作正常,直到我尝试分析数据用if()语句等。附件是代码的片段,它给我带来了问题(它抛出了System.IndexOutOfRange异常,以及其他讨厌的东西)。

我已经尝试了好几周才想知道解决方案是什么,但我已经70多岁了岁了,我老化的大脑发现很难探索微软的痛苦。一些聪明的黑客可以请教我这个障碍。



private void Port_DataReceived_1(对象发送者,SerialDataReceivedEventArgs e)

{

SetText(此.ComPort.ReadExisting());



} // datareceived

// ----- ----------------------------------------------

private void SetText(string text)

{



if(this.rtbIncoming.InvokeRequired)

{

SetTextCallback d = new SetTextCallback(SetText);

this.Invoke(d,new object [] {text});

}

其他

{

// RecDataString =SensMon:ABCDE(这是我从Pic System收到的)



if(text [8] ==('A')){flagsA =(A);}

else { flagsA =(?);}

if(text [9] ==('B')){flagsB =(B);}

else {flagsB =(?);}

if(text [10] ==('C')){flagsC =(C);}

else {flagsC =(?);}



//以下不起作用:

myData =(String.Format( {0}:{1}:{2}:,flagsA,flagsB,flagsC));

this.rtbIncoming.Text + = myData;



//以下工作:

//this.rtbIncoming.Text + = text;



}



我的尝试:



if(text [8] ==( 'A')){flagsA =(A);}

else {flagsA =(?);}

//上面抛出异常。

I have a Pic Microcontroller based system, which sends out a string of data periodicly.
Normaly I check it with HyperTerminal, but now I want to use my own little App to display the data properly.
The App works fine, until I try analising the data with if() statements and so on. Attached is a snippet of the code that gives me problems (it throws System.IndexOutOfRange Exception,and other nasty things).
I have tried for weeks to wonder what the solution is,but I am 70+ years old, and my aging brain finds it difficult to explore the miseries of Microsoft. Could some clever hacker please enlighten me on the obstacle.

private void Port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
{
SetText (this. ComPort.ReadExisting());

}//datareceived
//---------------------------------------------------
private void SetText(string text)
{

if (this.rtbIncoming.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
//RecDataString = "SensMon:ABCDE" (this is what I receive from Pic System)

if (text[8]== ('A')) {flagsA = ("A");}
else {flagsA = ("?");}
if (text[9] == ('B')) {flagsB = ("B");}
else {flagsB = ("?");}
if (text[10] == ('C')){flagsC = ("C");}
else {flagsC = ("?");}

//Below does not work:
myData = (String.Format("{0}: {1}: {2}:", flagsA, flagsB, flagsC));
this.rtbIncoming.Text += myData;

//Below does work:
//this.rtbIncoming.Text += text;

}

What I have tried:

if (text[8]== ('A')) {flagsA = ("A");}
else {flagsA = ("?");}
//The above throws the exception.

推荐答案

串口数据顾名思义:每个b数据的yte以串行顺序独立于其他数据到达。当每个字节到达时,Windows会引发DataReceived事件以让您的代码知道。您没有收到定义的数据包数据,因为串行端口本质上不是面向数据包的。

这意味着如果进入的数据是(例如)10个字节,您将获得任何地方发生1到10个事件,具体取决于计算机的速度,通信端口的波特率和系统总负载的速度。

如果要处理多个字节,然后你必须缓冲数据,直到一个可识别的数据包已经组装,并且视图。我这样做的方法是将它缓冲到DataReceived事件中的一个集合(例如List< T>)中,然后通过Invoke将它传递给你的主线程 - 因为DataReceived事件是 从不 在主线程上执行,它总是需要一个Invoke来访问UI组件,并且没有一个标准集合是线程安全的。这样做可以让你完成一个集合,然后将它完整地传递给另一个线程并且不再与它有任何关系。
Serial data is as the name implies: each byte of the data arrives independently of the others, in a serial sequence. And when each byte arrives, Windows raises a DataReceived event to let your code know. You do not receive a defined "packet" of data, because a serial port is not intrinsically packet oriented.
That means that if the data coming in is (for example) 10 bytes, you will get anywhere from 1 to 10 events occurring, depending on how fast your computer is, how fast the baud rate of the communications port, and the total load on the system.
If you want to process more than a byte, then you have to "buffer up" the data until a recognisable "packet" has been assembled, and the view that. The way I would do it is to buffer it into a collection (a List<T> for example) inside the DataReceived event, and then pass that via an Invoke to your main thread - because the DataReceived event is never executed on the main thread, it always requires an Invoke to access UI components, and none of the standard collections are thread safe. Doing it this way lets you "finish" with a collection before passing it intact to a different thread and having nothing more to do with it.


这篇关于我使用C#和visual studio进行串口通信时遇到问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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