串行端口事件处理程序不触发

编程入门 行业动态 更新时间:2024-10-11 13:20:03
本文介绍了串行端口事件处理程序不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我有一个特殊的问题,我有一个串行端口事件,在少数情况下会触发该事件,然后此后将无法工作.设置如下,我插入了一个USB RF软件狗,它模拟了串行端口,这就是我用来连接程序的接口. 这不是加密狗故障,因为加密狗的指示灯会在接收到RF时闪烁,并且当我关闭程序时看到串行事件处理程序失效,并且将同一加密狗连接到Docklight时,我会看到数据通过. 我已经看到了很多建议的解决方案,甚至在这个论坛上有一个关于带有串行事件处理程序的.Net 2.0错误的解决方案,那个人建议使用Readbyte方法,这会导致串行端口事件再次唤醒. 但是,这对我不起作用.下面是我正在使用的代码.

Hi Everyone, I have a peculiar problem, I have a serial Port event that fires for a few instances and then just doesnt work afterwards. The setup is as follows, I have a Usb RF dongle plugged in that emulates the Serial Port and that is what I use to interface to my program. It isn''t the Dongles fault, as the Dongle has an indicator light that flashes when RF is received, and When I close my program when I see the Serial Event Handler is dead, and Connect the Same dongle to Docklight, I see data coming through. I have seen quite a few proposed solutions, even one on this forum about a Bug with .Net 2.0 with the Serial Event handler, that person suggested using the Readbyte method which resulted in the Serial Port event waking up again. However this did not work for me. Below is the code that I am using.

SP.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived);//How I assign the handle public void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { WriteToText(Environment.NewLine + "Entered Serial Handler" + Environment.NewLine); this.BeginInvoke(new EventHandler(ReceiveHandler)); } public void ReceiveHandler(object s, EventArgs e) { ReadBufferByte = new byte[SP.BytesToRead]; int BytesSent = SP.Read(ReadBufferByte, 0, ReadBufferByte.Length); if (BytesSent != ReadBufferByte.Length) MessageBox.Show("ERROR: Not all bytes read from the buffer!"); Stacked.AddToInbox(ReadBufferByte,ReadBufferByte.Length); }

Any help is greatly appreciated.

推荐答案

乍一看,唯一的原因是该事件不会触发,这是因为SP没有收到任何东西.在这种情况下,很大程度上取决于RS-232连接器另一端发生的情况(在您的情况下是在加密狗中).您没有显示加密狗规范,也没有显示发送给它的通信,所以谁知道.阅读文档,进行实验,但是要进行加密狗所期望的操作.并且,首先,使用调试器查看发生了什么. 此外,我建议您不参加此活动.这种方法更直接,更容易实现,而且重要的是调试.在一个单独的线程中执行所有串行通信,并使所有代码顺序执行,而无需所有那些异步方法.由于通信位于单独的线程中,因此可以允许阻止调用.设置一个断点;然后您会看到何时接收到一些数据,什么没有接收到.
—SA
From the first glance, the only reason is that the event does not fire, and this is because SP does not receive anything. In such cases, a lot depends on what''s going on on the other end of your RS-232 connector, in your case, in a dongle. You did not show the dongle specs, the communications you send to it, so who knows. Read documentation, experiment, but do something which the dongle expect. And, first of all, use the Debugger to see what exactly is going on. Besides, I would recommend you to work without this event. This way is more straightforward, easier to implement, and, importantly, to debug. Do all serial communications in a separate thread, and makes all the code sequential, without all those asynchronous methods. As the communication is in a separate thread, you can allow for blocking calls. Put a break point; and you will see when some data is received and what it is not.
—SA

更多推荐

串行端口事件处理程序不触发

本文发布于:2023-11-12 13:19:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   事件   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!