该操作已在serialPort1.ReadLine()中超时 [英] The operation has timed out in serialPort1.ReadLine()

查看:611
本文介绍了该操作已在serialPort1.ReadLine()中超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我还在用串口做我的小项目。



我我在数据接收事件处理程序中使用以下代码行来读取多个字节;



line = serialPort1.ReadLine();



字节通过PIC单片机发送到PC,现在只需测试我的软件。值是



0x03,0x04,0x05,0x06等......



虽然当我的代码到了C#代码行,它给了我一个超时异常(详见帖子底部)



我理解ReadLine方法读取的值为as一个字符串



我的串口组件的ReadTimeout属性设置为1500,我尝试了一些值。这与它有什么关系吗?



System.TimeoutException未处理

消息=操作已经超时。

Source =System

StackTrace:

在System.IO.Ports.SerialPort.InternalRead(Char []缓冲区,Int32偏移量,Int32计数,Int32 timeout,Boolean countMultiByteCharsAsOne)

at System.IO.Ports.SerialPort.ReadTo(String value)

at System.IO.Ports.SerialPort.ReadLine()<在C:\Documents and Settings \ jharris \Desktop\Visual Studio 2008 \Projects \ConciseSerialPortTest \ConciseSerialPortTest \中的ConciseSerialPortTest.frmConciseTest.serialPort1_DataReceived(Object sender,SerialDataReceivedEventArgs e)中的
frmConciseTestMain.cs:第116行

在System.IO.Ports.SerialPort.CatchReceivedEvents(Object src,SerialDataReceivedEventArgs e)

在System.IO.Ports.SerialStream.EventLoopRunner。 CallReceiveEvents(OB ject state)

在System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(对象状态)

在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)<在System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)上的


在System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(对象状态)

InnerException:

Hello,

I am still doing my little project with a serial port.

I am using the following line of code in the data received event handler to read in a number of bytes;

line = serialPort1.ReadLine();

The bytes are being sent to the PC via a PIC microcontroller, and for now are simply to test my software. The values are

0x03, 0x04, 0x05, 0x06 and so on ...

Though when my code get to the the line of C# code it gives me a timeout exception (see bottom of post for details)

I understand the ReadLine method reads the values in as a string

The ReadTimeout property of my serial port component is set to 1500, and I have tried a few values. Does this have anything to do with it?

System.TimeoutException was unhandled
Message="The operation has timed out."
Source="System"
StackTrace:
at System.IO.Ports.SerialPort.InternalRead(Char[] buffer, Int32 offset, Int32 count, Int32 timeout, Boolean countMultiByteCharsAsOne)
at System.IO.Ports.SerialPort.ReadTo(String value)
at System.IO.Ports.SerialPort.ReadLine()
at ConciseSerialPortTest.frmConciseTest.serialPort1_DataReceived(Object sender, SerialDataReceivedEventArgs e) in C:\Documents and Settings\jharris\Desktop\Visual Studio 2008\Projects\ConciseSerialPortTest\ConciseSerialPortTest\frmConciseTestMain.cs:line 116
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
InnerException:

推荐答案

ReadLine期望数据基于行 - 即由系统行尾序列终止 - ASCII CR或NL,或CR NL对,具体取决于在系统上 - 如果您的PIC没有发送这些,那么ReadLine将等待一段时间,然后决定不发送可识别的数据,并终止操作 - 这称为超时。



要么说服PIC发送正确的数据,要么改为SerialPort.Read [ ^ ]。
ReadLine expects the data to be line based - i.e. terminated by a system end-of-line sequence - either ASCII CR or NL, or a CR NL pair, depending on the system - if your PIC is not sending these, then ReadLine will wait for a while, then decide no recognisable data is being sent, and terminate the operation - this is called a time-out.

Either persuade your PIC to send the right data, or change to SerialPort.Read[^] instead.


这篇关于该操作已在serialPort1.ReadLine()中超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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