适用于 Windows CE 6.0 的 .net Compact 2.0 中的 dataReceived 事件仅适用于串行通信一次

发布于 2024-09-19 03:10:30 字数 882 浏览 7 评论 0原文

我有一个简单的串行端口程序,该程序应该读取串行端口并回显键入的字符。我可以通过轮询使用计时器收到的串行数据来使其正常工作,但我想改用 dataReceived 事件。由于某种原因,该事件只会发生一次,但不会再发生。

    SerialPort bsp;
    public Form1()
    {
        InitializeComponent();
        bsp = new SerialPort("COM2", 2400, Parity.None, 8, StopBits.One);
        bsp.DataReceived +=new SerialDataReceivedEventHandler(whasup);
        System.Threading.Thread.Sleep(1000);
        bsp.Open();
    }
    void whasup(object sender, SerialDataReceivedEventArgs e)
    {
        char[] text = new char[100];
        int temp = bsp.BytesToRead;
        string j = temp.ToString();
        bsp.Read(text, 0, temp);
        bsp.Write(text, 0, temp);
    }

我尝试过将其移植到 PC 上,效果很好,但在这个 Windows CE 设备上,它确实不想合作。我知道串行对象仍然处于打开状态,因为即使在停止回显其接收数据之后,我也有一个计时器在运行,从 Win CE 设备中吐出文本。我还有计时器输出 bsp.BytesToRead,当我在与 Windows CE 设备串行通信的 PC 上输入更多按键时,我可以看到该值不断攀升。我错过了什么吗?

I have simple serial port program that is supposed to read the serial port and echo back the characters typed. I can get this to work fine by polling the serial recieved with a timer, but I'd like to use the dataReceived event instead. For some reason the event will occur once but never again.

    SerialPort bsp;
    public Form1()
    {
        InitializeComponent();
        bsp = new SerialPort("COM2", 2400, Parity.None, 8, StopBits.One);
        bsp.DataReceived +=new SerialDataReceivedEventHandler(whasup);
        System.Threading.Thread.Sleep(1000);
        bsp.Open();
    }
    void whasup(object sender, SerialDataReceivedEventArgs e)
    {
        char[] text = new char[100];
        int temp = bsp.BytesToRead;
        string j = temp.ToString();
        bsp.Read(text, 0, temp);
        bsp.Write(text, 0, temp);
    }

I've tried porting this to a PC and it works great, but on this Windows CE device it really doesn't want to cooperate. I know the serial object is still open because i've had a timer running spitting text out of the Win CE device even after it stop echoing back its receive data. I also had the timer output bsp.BytesToRead, and I could see that value climb as I typed more keys in on my PC that talked serially to the Windows CE device. Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-09-26 03:10:30

我们在使用 Read() 时遇到了类似的问题,但使用 ReadExisting() 进行了管理,以始终读取整个缓冲区。可能正是这个事实允许引发下一个事件。

包含 Sleep(500) 是因为一旦第一个字节写入缓冲区就会引发该事件。通过一些尝试/错误,并且知道我们正在读取的消息从未超过特定长度,我们知道它们将在事件发生后的 500 毫秒内(实际上是很久以前)被完全写入。

有点偶然,我会非常高兴看到串行专家提供“正确”的解决方案!

代码:

private SerialPort port;
private object readLock = new object();

private SerialManager()
{
    port = new SerialPort(SerialPort, BaudRate, Parity.None, 8, StopBits.One);
    port.Handshake = Handshake.None;
    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    port.Open();
}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    // Lock to subsequent DataReceived events
    lock (readLock)
    {
        Thread.Sleep(500);
        string data = port.ReadExisting();
    }
}

We suffered similar problems with Read() but managed it with ReadExisting() instead, to always read the whole buffer. It may be this fact that allows the next event to be raised.

The Sleep(500) was included because the event is raised as soon as the first byte is written to the buffer. Through some trial/error and in the knowledge that the messages we were reading never exceeded a particular length, we knew they'd be fully written within (and in fact long before) 500ms of the event.

A bit hit/miss and I'd be more than happy to see a 'correct' solution from a serial expert!

The code:

private SerialPort port;
private object readLock = new object();

private SerialManager()
{
    port = new SerialPort(SerialPort, BaudRate, Parity.None, 8, StopBits.One);
    port.Handshake = Handshake.None;
    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    port.Open();
}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    // Lock to subsequent DataReceived events
    lock (readLock)
    {
        Thread.Sleep(500);
        string data = port.ReadExisting();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文