使用 .NET 2.0 应用程序在 Windows Server 2008 上不会触发 SerialPort 事件
我有一个程序可以通过串行端口从扫描仪读取数据。该程序在 Windows XP 上运行良好。我们设置了一个运行 Windows Server 2008 的终端服务器。我在测试终端上运行超级终端,它通过 COM1 连接并读取扫描仪数据。我在同一个测试终端上运行我的应用程序,但扫描时什么也没得到。
我的应用程序连接到 COM1 没有错误,断开连接也没有错误。但是,DataRecieved 事件永远不会被触发。 ErrorReceived 事件也不是。我玩过握手、RecievedBytesThreshhold 以及我发现的几乎所有设置。其设置与超级终端上的设置完全相同。我什至尝试在不同的线程上启动一个计时器,每秒调用 ReadByte 来尝试踢这个东西做一些事情。没有任何效果。
我一整天都在努力解决这个问题。在我的班级中添加了事件,试图了解正在发生的一切。我所知道的是,它正确地连接到端口并与端口断开连接,但两者之间没有任何反应。扫描时没有数据。连接到端口和断开连接之间根本没有触发任何事件。我厌倦了谷歌。 :-)
帮助!!请???
PS我还下载了其他简单的串行通信应用程序。 .NET 中没有任何功能。时期。 :-(
端口设置代码(mvarSerialPort 是 COM1):
If ComPort Is Nothing Then
ComPort = New SerialPort(mvarSerialPort)
ComPort.BaudRate = 9600
ComPort.Parity = Parity.None
ComPort.StopBits = StopBits.One
ComPort.DataBits = 8
ComPort.ReadTimeout = 2000
ComPort.Encoding = System.Text.Encoding.ASCII
ComPort.Handshake = Handshake.None
ComPort.ReceivedBytesThreshold = 1
End If
If Not ComPort.IsOpen Then ComPort.Open()
ComPort.DtrEnable = True
DataReceived 事件(在该死的终端上永远不会触发):
Private Sub Scan(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles ComPort.DataReceived
Dim sDataStatus As String = ""
Select Case e.EventType
Case SerialData.Chars
sDataStatus = "Data Received Event - Chars "
Case SerialData.Eof
sDataStatus = "Data Received Event - Eof"
End Select
RaiseEvent ClassStatus(sDataStatus)
Dim delInvoke As ScanResults
delInvoke = New ScanResults(AddressOf RaiseScanEvent)
delInvoke.Invoke()
End Sub
RaiseScanEvent(这在我的计算机和我同事运行 XP 的计算机上效果很好)
Private Sub RaiseScanEvent()
Dim sScanned As String = ComPort.ReadLine.TrimEnd
RaiseEvent ClassStatus("RaiseScanEvent data: " & sScanned)
RaiseEvent ComDataReceived(sScanned)
End Sub
I have a program that reads data from a scanner via the serial port. This program works fine on Windows XP. We have a terminal server set up running Windows Server 2008. I run HyperTerminal on our test terminal, it connects and reads the scanner data fine through COM1. I run my app on that same test terminal and get nothing when I scan.
My app connects to COM1 without errors and disconnects without errors. BUT, the DataRecieved event is NEVER getting fired. Neither is the ErrorReceived event. I have played with the Handshaking, with the RecievedBytesThreshhold and pretty much every setup setting I found. Set it up exactly like the settings on Hyperterminal. I have even tried starting a timer on a different thread to call ReadByte every second to try to KICK this thing into doing SOMETHING. Nothing has worked.
I have been trying to fix this for an entire day now. Added events to my class trying to see EVERYTHING that is going on. All I know is, it connects to the port and it disconnects from the port correctly but nothing happens in between. No data when I scan. No event fired at all between connecting to and disconnecting from the port. I'm tired of google. :-)
HELP!! Please???
PS I have also downloaded other's simple serial communications applications. Nothing in .NET works. Period. :-(
Port SetUp code (mvarSerialPort is COM1):
If ComPort Is Nothing Then
ComPort = New SerialPort(mvarSerialPort)
ComPort.BaudRate = 9600
ComPort.Parity = Parity.None
ComPort.StopBits = StopBits.One
ComPort.DataBits = 8
ComPort.ReadTimeout = 2000
ComPort.Encoding = System.Text.Encoding.ASCII
ComPort.Handshake = Handshake.None
ComPort.ReceivedBytesThreshold = 1
End If
If Not ComPort.IsOpen Then ComPort.Open()
ComPort.DtrEnable = True
DataReceived event (which is never fired on that darn terminal):
Private Sub Scan(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles ComPort.DataReceived
Dim sDataStatus As String = ""
Select Case e.EventType
Case SerialData.Chars
sDataStatus = "Data Received Event - Chars "
Case SerialData.Eof
sDataStatus = "Data Received Event - Eof"
End Select
RaiseEvent ClassStatus(sDataStatus)
Dim delInvoke As ScanResults
delInvoke = New ScanResults(AddressOf RaiseScanEvent)
delInvoke.Invoke()
End Sub
RaiseScanEvent (This works great on my computer and my coworker's computer running XP)
Private Sub RaiseScanEvent()
Dim sScanned As String = ComPort.ReadLine.TrimEnd
RaiseEvent ClassStatus("RaiseScanEvent data: " & sScanned)
RaiseEvent ComDataReceived(sScanned)
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕这并不是一个明确的答案,但需要尝试/考虑一些事情:
mvarSerialPort真的在终端上具有“COM1”的值吗?
可能在打开端口之前尝试设置 DtrEnable = True。
使用端口监视器,例如 PortMon 是一个不错的选择想法,尽管它确实为您提供了相当低级的信息。您可以将工作 PC 与终端机的结果进行比较。至少它可以在扫描仪执行任务时向您显示任何活动。
如果您的终端机上有另一个 COM 端口(虽然现在越来越不可能,但您可以使用 USB 串行转换器仅用于测试目的),您可以尝试制作/购买一个 空调制解调器电缆。 然后您可以将这两个端口连接在一起。这样,您可以将应用程序连接到 COM1,将超级终端连接到 COM2(或其他)。这将有效地允许您使用超级终端输入您的应用程序。
Not really a definitive answer I'm afraid, but a few things to try/consider:
Does mvarSerialPort really have the value of "COM1" on the terminal?
Possibly try setting DtrEnable = True before opening the port.
Using a port monitor, such as PortMon is a good idea, although it does give you pretty low level info. You may be able to compare results of working PC's with the terminal machine. At least it may show you any activity when the scanner does its thing.
If you have another COM port on the terminal machine (increasingly unlikely these days though, but you could use a USB-Serial converter just for testing purposes) you could try making/buying a null-modem cable. Then you can connect both theses ports together. This way, you can have your app connected to COM1 and Hyperterminal to COM2 (or whatever). This will effectively allow you to type to your app with Hyperterminal.