无法从串行端口捕获数据 [英] Can't capture data from serial port

查看:80
本文介绍了无法从串行端口捕获数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我目前在一个项目中,该项目要求我通过串行COM端口从设备捕获2个电磁编码器的数据.我可以使用以下代码接收数据:

Hi, I'm currently on a project that requires me to capture data of 2 magnetic encoders from a device via a Serial COM port. I was able to receive data using the following code:

Private Sub COMport_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COMport.DataReceived inData(COMport.ReadExisting()) End Sub

私有子inData(ByVal [text]作为字符串) 如果captureClick> = 1且captureClick< = 21然后 captureClick = captureClick + 1 如果Me.testTxtbx.InvokeRequired然后 昏暗x作为新的SetTextCallBack(AddressOf inData) Me.Invoke(x,New Object(){{text)}) 别的 Me.testTxtbx.Text& = vbCrLf& captureClick& " ;:" & [文本] 万一 万一 结束Sub

Private Sub inData(ByVal [text] As String) If captureClick >= 1 And captureClick <= 21 Then captureClick = captureClick + 1 If Me.testTxtbx.InvokeRequired Then Dim x As New SetTextCallBack(AddressOf inData) Me.Invoke(x, New Object() {(text)}) Else Me.testTxtbx.Text &= vbCrLf & captureClick & ": " & [text] End If End If End Sub

从上面的代码中,我能够得到一些十六进制的结果:

From the code above i was able to get some hexadecimals as a result:

" x07000000322E3338346533125ABC49 x07000000342E3034326530EAC6237B x13000000656E635F6C3A2D32383520656E635F723A350A5DED53C7

"x07000000322E3338346533125ABC49x07000000342E3034326530EAC6237Bx13000000656E635F6C3A2D32383520656E635F723A350A5DED53C7

然后我尝试将这些十六进制转换为字符串,以便可以在RichTextBox上显示它们,我使用了以下代码:

Then I attempted to try to convert these hexadecimals to string so that I could display them on a RichTextBox, I used the following code:

Private Sub COMport_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COMport.DataReceived
        inData(COMport.ReadExisting())
    End Sub

    Dim com As String
    Dim out As String
    Private Sub inData(ByVal [text] As String)
        If captureClick >= 1 And captureClick <= 21 Then
            captureClick = captureClick + 1
            If Me.testTxtbx.InvokeRequired Then
                Dim x As New SetTextCallBack(AddressOf inData)
                Me.Invoke(x, New Object() {(text)})
            Else
                For x = 1 To Len(text) Step 2
                    com = Mid(text, x, 2)
                    out = out & Chr(Val("&h" & com))
                    Me.testTxtbx.Text = out
                Next

            End If
        End If
    End Sub

但是没有得到:

2.392e3
4.060e0
enc_l:-161 enc_r:-1

2.392e3
4.060e0
enc_l:-161 enc_r:-1

我无法在RichTextBox上显示任何内容.在某些情况下,它提示我说"

 I wasn't able to display anything on the RichTextBox. In some cases it prompted me an error saying "Make sure you have not released a resource before attempting to use it."

注意:在其他情况下,我使用的是1到21,因为设备在输出数据之前存在延迟,因此我试图捕获10个数据

Note: I'm using 1 to 21 in the if else because the device has a delay before it outputs the data and I am trying to capture 10 datas

推荐答案

我无法在RichTextBox上显示任何内容.在某些情况下,它提示我一个错误,提示"

 I wasn't able to display anything on the RichTextBox. In some cases it prompted me an error saying "Make sure you have not released a resource before attempting to use it."

您是否确定设备正在以十六进制字符串形式发送数据?

Are you quite sure that the device is sending the data as hexadecimal strings?

此代码:
                Me . testTxtbx . 文本 =

This code:
                   Me.testTxtbx.Text = out

替换该文本框中的文本,并在事件发生时将其与读取缓冲区中的所有内容替换.这可能不是您想要的,因为不能保证该事件发生在接收序列的任何特定部分. 您可能想将文本追加到文本框中,然后对其进行分析以检测完整的序列.该序列可以是任何以'x'开头并持续30个(例如)字符而没有另一个'x'的序列.你可以做这个分析 每次更新文本框时,都会删除所有已完成的序列或任何垃圾(例如,任何不以"x"开头的内容).

will replace the text in the text box with whatever happens to be in the read buffer when the event occurs.  This is probably not what you want, as there is no guarantee that the event occurs at any particular part of the receiving sequence.   You probably want to append the text to the text box, and then analyse it to detect a complete sequence.  That might be any sequence that starts with 'x' and continues for 30 (for example) characters without another 'x'. You can do this analysis each time the text box is updated, removing any completed sequence, or any garbage (anything that doesn't start with 'x', for example).

当您获得这样的序列时,请剥离'x'并将其转换.

When you get a sequence like that, strip the 'x' off and convert it.

结果可能不是文本-值如07和00看起来不像文本.也许需要再次转换为数字.这完全取决于设备的文档中关于正在创建的数据的说明.

The result might not be text - with values like 07 and 00 it doesn't look like text.  Perhaps another conversion to numeric is required.  It all depends on what the documentation for the device says about the data it is creating.

错误消息的含义完全取决于创建该错误代码的行.

The meaning of the error message depends on exactly which line of code creates it.


这篇关于无法从串行端口捕获数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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