C#串行端口无法发出命令 [英] C# serial port cannot issue commands

查看:113
本文介绍了C#串行端口无法发出命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SerialPort sp = new SerialPort("COM16", 115200, Parity.None, 8, StopBits.One);       
string prn;
sp.Handshake = Handshake.None;

sp.WriteTimeout = 3500;
sp.ReadTimeout = 4500;

sp.Open();
sp.WriteLine("AT+CREG?\r\n");
prn=sp.ReadLine();

richTextBox1.AppendText(prn);
sp.Close();




当我在sp.ReadLine()调试代码时

一段时间显示错误
>>>
System.dll中发生了类型为"System.TimeoutException"的未处理的异常
附加信息:操作已超时.


一段时间后显示
>>
AT + CREG?

在readline();我尝试使用\ n \ r




when i debug code at sp.ReadLine()

it Some time shows Error
>>>
An unhandled exception of type ''System.TimeoutException'' occurred in System.dll
Additional information: The operation has timed out.


some time it shows
>>
AT+CREG?

at readline(); i have tried using all possible combination of \n\r

推荐答案

所有可能的组合有什么异常之处?您用超时调用阻止操作,但是串行电缆另一端的东西什么也没给您发送. :-)

您可以检查缓冲区是否有数据,如果没有数据则不读取,请使用System.IO.Ports.SerialPort.BytesToRead并使用更长的超时时间,请使用System.IO.Ports.SerialPort.ReadTimeout.

请参阅 http://msdn.microsoft.com/en-us/library/system .io.ports.serialport.aspx [ ^ ].

现在,在处理阻塞调用时,您是否为此目的使用了单独的线程?你应该.最好的选择是创建一个单独的线程,该线程无条件地读取所有内容,并在发生某些情况时将数据推送到其他线程.读取操作正在阻塞,因此您不想阻塞您的主线程.

要进行适当的线程间通信,请阅读我的简短提示/技巧文章 Simple Blocking Queue用于线程通信和线程间调用 [ Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
What''s so unusual? You call a blocking operation with timeout, but something from the other side of the serial cable don''t send you anything. :-)

You can check up the buffer if it has data and don''t read when there is no data, use System.IO.Ports.SerialPort.BytesToRead and use longer timeout, use System.IO.Ports.SerialPort.ReadTimeout.

See http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^].

Now, as you''re dealing with blocking calls, do you use a separate thread for this purpose? You should. Your best option is creating a separate thread which unconditionally reads everything and push data to other thread when something happen. Read operation is blocking, so you don''t want to block you main thread.

For appropriate inter-thread communication read my short Tips/Tricks article Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

To notify UI thread, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also my collection of links to my past answers on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于C#串行端口无法发出命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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