打开后不能立即使用串口 [英] The serial port cannot be used immediately after being opened

查看:133
本文介绍了打开后不能立即使用串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的串口有问题:



我的代码:



  Dim  readData()作为 字节 
Dim port As New SerialPort( COM1 38400 ,Parity.None, 8
port.StopBits = StopBits.One
port.Handshake = Handshake.None
port .ReadTimeout = 1000
port.Open()

port.Read(readData, 0 8

如果 readData.Length> ; 0 然后
对于 < span class =code-keyword>每个 b 作为 字节 In readData
Me .TextBox1.Text& = b.ToString( X2
下一步
结束 如果

port.Close()





尝试在打开后立即读取该端口给出了以下例外情况:



System.IO .IOException

由于线程退出或应用程序请求,I / O操作已中止





但是如果我在Open和Read线之间放置Thread Sleep,问题就会消失:



 port.Open()
System.Threading.Thread.Sleep( 20
port.Read(readData, 0 8





有人知道为什么会这样吗?

使用Thread Sleep是否正确或有更好的解决方案?

解决方案

嗯...我没有看到这个问题。做一些谷歌搜索,似乎你不是唯一遇到这种情况的人。似乎SerialPort类可能有一个小错误。



奇怪的是,如果你编译应用程序发布并运行它(不在调试器下!)它没有睡眠可能会正常工作。试试吧......



将它留在那里没有什么害处。


谢谢你的回答戴夫。我试图调查这个问题,但我没有太多运气,特别是因为端口的行为不是我可以尝试的常量。



重新启动我的电脑后,我发布的问题大大减少了,它似乎与内存有关(我真的不知道)。



我完全避免了这个错误以下扩展方法,我不知道它是否是最佳选择,但我希望有所帮助。



< extension( )> 
公共 Sub OpenPort( ByVal port As SerialPort,可选 ByVal millisecondsSleep 作为 整数 = 20
port.Open()
System.Threading.Thread.Sleep(millisecondsSleep)
结束 Sub


I have a problem with the serial port:

My code:

Dim readData() As Byte
Dim port As New SerialPort("COM1", 38400, Parity.None, 8)
port.StopBits = StopBits.One
port.Handshake = Handshake.None
port.ReadTimeout = 1000
port.Open()

port.Read(readData, 0, 8)

If readData.Length > 0 Then
   For Each b As Byte In readData
      Me.TextBox1.Text &= b.ToString("X2")
   Next
End If

port.Close()



trying to read the port immediately after it was opened gives me the following exception:

System.IO.IOException
The I/O operation has been aborted because of either a thread exit or an application request


But if I put a Thread Sleep between the Open and the Read lines, the problem goes away:

port.Open()
System.Threading.Thread.Sleep(20)
port.Read(readData, 0, 8)



Somebody knows why this behavior?
Is it correct to use a Thread Sleep or is there a better solution?

解决方案

Hmmm... I'm not seeing this problem. Doing some Googling, it seems you're not the only one running into this. It seems the SerialPort class might have a little bug in it.

What's weird is that if you compile the app Release and run it (NOT under the debugger!) it might work fine without the Sleep. Try it...

There's no harm in leaving it in there though.


Thanks for your answer Dave. I have tried to investigate this issue but I haven't had much luck, especially as the behavior of the port is not constant as far as I could try.

The problem that I posted was greatly reduced after restarting my PC, it seems it was something related to memory (I do not know really).

I completely avoid this error with the following extension method, I don't know if it's the best option, but I hope that helps.

<extension()>
Public Sub OpenPort(ByVal port As SerialPort, Optional ByVal millisecondsSleep As Integer = 20)
   port.Open()
   System.Threading.Thread.Sleep(millisecondsSleep)
End Sub


这篇关于打开后不能立即使用串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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