不能通过rs232读取10个字节 [英] can't read 10 bytes via rs232

查看:125
本文介绍了不能通过rs232读取10个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我正在尝试让我自己的程序是通过rs232与MCU一起工作。但我不能读取所有10个字节,我只做5个最后5个字节。有人可以帮帮我吗?

这是处理程序的代码:



Hi everyone! I''m trying to make my own program is to work with MCU via rs232. But i can''t read all 10 bytes, i do only 5 last as 5 first. Could anyone help me?
Here is the handler''s code:

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
        ''Dim n As Integer = SerialPort1.BytesToRead
        ''data = New Byte(n - 1) {}
        SerialPort1.Read(data, 0, 10)  ''read data from the buffer
        Me.Invoke(UpdateFormDelegate1) ''call the delegate  
    End Sub

  Private Sub UpdateDisplay()
        TextBox1.Text = Hex(data(0))
        TextBox2.Text = Hex(data(1))
        TextBox3.Text = Hex(data(2))
        TextBox4.Text = Hex(data(3))
        TextBox5.Text = Hex(data(4))
        TextBox6.Text = Hex(data(5))
        TextBox7.Text = Hex(data(6))
        TextBox8.Text = Hex(data(7))
        TextBox9.Text = Hex(data(8))
        TextBox10.Text = Hex(data(9))
    End Sub





我的编程和其他终端的屏幕截图:



http ://linkme.ufanet.ru/images/d8fc1e412c2257d32ab3ef2fe9a1ad84.jpg





http://linkme.ufanet.ru/images/e9e81241d0c6cae6a9dca3aa288a137f.jpg



对不起对于我的坏英语)



Screenshots of my prog and other terminal:

http://linkme.ufanet.ru/images/d8fc1e412c2257d32ab3ef2fe9a1ad84.jpg


http://linkme.ufanet.ru/images/e9e81241d0c6cae6a9dca3aa288a137f.jpg

Sorry for my bad english)

推荐答案

串行端口不是瞬时数据通信设备:数据一次到达,并组装成字节。当您收到DataReceived事件时,这意味着已收到 至少一个字节 ,而不是已收到十个字节。



检查计数,并将字节组装到外部缓冲区 - 可能需要十个事件才能全部接收它们!
Serial ports are not instantaneous data communications devices: data arrives a bit at a time, and is assembled into bytes. When you get a DataReceived event, that means that at least one byte has been received, not that ten bytes have been received.

Check the count, and assemble the bytes into an external buffer - it could take ten events to receive them all!


根据SerialPort.DataReceived的帮助文件:

不保证每收到一个字节都会引发DataReceived事件。使用BytesToRead属性确定要在缓冲区中读取多少数据。



这告诉我DataReceived事件在所有10之前被引发收到的字节数。



如果收到的10个字符总是以x''EE''结尾,你可以将SerialPort.NewLine属性设置为该值并使用SerialPort.Readline读取数据。将返回九个字符,如果您需要,可以添加x''EE''。



来自SeralPort.ReadLine的帮助文件:

请注意,虽然此方法不返回NewLine值,但NewLine值从输入缓冲区中删除



我建议你试试这个

此外,您可以设置SerialPort.ReadTimeOut属性,然后使用SerialPort.ReadLine读取数据。 SerialPort.ReadLine将超时,您的接收字符串中将包含全部十个字符。根据异步连接的速度和您期望的字符数设置SerialPort.ReadTimeOut属性值。每个字符发送十位。对于9600 bps连接,我首先尝试将SerialPort.ReadTimeOut设置为11毫秒。



According to the Help file for SerialPort.DataReceived:
The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.

That tells me that the DataReceived event is raised before all 10 bytes are received.

If the 10 characters received will always end with an x''EE'', you could set the SerialPort.NewLine property to that value and use SerialPort.Readline to read the data. Nine characters would be returned and you could add back the x''EE'' if you wanted it.

From the Help file for SeralPort.ReadLine:
Note that while this method does not return the NewLine value, the NewLine value is removed from the input buffer

I suggest you try this
Also, you could set the SerialPort.ReadTimeOut property and then use SerialPort.ReadLine to read the data. SerialPort.ReadLine will timeout and you will have all ten characters in your receiving string. Set the SerialPort.ReadTimeOut property value depending on the speed of the asynchronous connection and the number of characters that you expect. Ten bits are transmitted for each character. For a 9600 bps connection, I would first try setting SerialPort.ReadTimeOut to 11 milliseconds.

Dim dataBuffer As String
SerialPort1.NewLine = vbCR  '' Or some other character that will not be in your data stream
SerialPort1.ReadTimeOut = 11 '' milliseconds
dataBuffer = SerialPort1.ReadLine(data)  ''read data from the buffer


在更新显示之前,您必须确保已收集所需的所有字节。在多次调用 DataReceived 事件处理程序之后,可能会发生这种情况。那就是你必须缓冲传入的数据。
Before updating the display, you have to make sure that all the bytes you need have been collected. This may happen after multiple calls to the DataReceived event handler. That is you have to buffer incoming data.


这篇关于不能通过rs232读取10个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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