在Visual Basic 2013中读取或接收来自串行端口RS232的数据(visual studio .NET) [英] Reading or receiving data from serial port RS232 in visual basic 2013 (visual studio .NET)

查看:134
本文介绍了在Visual Basic 2013中读取或接收来自串行端口RS232的数据(visual studio .NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试从称重机获得重量。



在使用COM1在Windows XP中使用超级终端阅读时,我们得到正确的值。但是在我的笔记本电脑上使用USB转COM端口转换器电缆在Visual Basic 2013中读取数据(在Windows 10 Pro中),我们得到的值不同,如x,?,3和&等等。请提出解决方案,以便我们可以获得正确的数据通过代码。



我们的代码是:



We are trying getting weight from weighing machine.

While reading in Hyper-terminal in windows XP using COM1 we getting the right values. But reading data in Visual Basic 2013 (in Windows 10 Pro) on my laptop using USB to COM port convertor cable we are getting different values like x, ?, 3, &, etc. Kindly suggest the solution so that we can get the right data through code.

Our Code is:

With Me.mvSerialPort
    .PortName = "COM11"          ' as shown in device manager
    .BaudRate = 2400
    .Parity = Parity.None
    .StopBits = StopBits.One
    .DataBits = 8
    .Handshake = Handshake.None
    .DtrEnable = True
    .RtsEnable = True
    .NewLine = vbCrLf
    .ReadTimeout = 1000
    .WriteTimeout = 1000
End With

'Function Code
    Private Function ReadCOM() As String
        Do
            If Me.mvThread_Stop = True Then Exit Do
            Try
                Try
                    If Me.mvSerialPort.IsOpen Then
                        Me.mvSerialPort.Close()
                    End If
                Catch ex As Exception
                    '
                End Try

                Try
                    Me.mvSerialPort.Open()
                    If Me.mvSerialPort.IsOpen Then
                        'Thread.Sleep(200)
                        Thread.Sleep(Val(Me.TextBox4.Text))
                        Dim svMessage As String = ""
                        Try
                            'svMessage = Me.mvSerialPort.ReadLine()
                            svMessage = Me.mvSerialPort.ReadExisting
                            updateStatus(svMessage)
                        Catch ex As Exception
                            'Me.mvThread_Stop = True
                        End Try
                    End If
                    Me.mvSerialPort.Close()
                Catch ex As Exception
                    Me.mvSerialPort.Close()
                    '
                End Try
            Catch ex As TimeoutException
                'updateStatus(ex.ToString)
            End Try
        Loop
        Return ""
    End Function

    Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
    Public Sub updateStatus(ByVal newStatus As String)
        Try
            If Me.InvokeRequired Then
                Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
                Me.Invoke(upbd, New Object() {newStatus})
            Else
                Dim svSplit() As String = Split(newStatus, " ")
                Me.TextBox4.Text = svSplit(svSplit.Count - 1)
                RichTextBox1.Text = newStatus & vbCrLf & RichTextBox1.Text
            End If
        Catch ex As Exception
            '
        End Try
    End Sub





我尝试了什么:



我的问题中提到的代码



What I have tried:

The code as mentioned in my question

推荐答案

Make确保代码中的串行端口设置与Hyperteminal通信时使用的相同。此外,我首先尝试在笔记本电脑上使用 Hyperterminal (为了确保正确处理串行通信)。

一旦以上几点都可以,使用调试器查看串口上的输入数据。
Make sure the serial port settings in your code are the same used while communicating with Hyperteminal. Also I would first try to use the Hyperterminal on the laptop too (in order to be sure the serial communication is handled properly).
Once the above points are OK, use the debugger to look at incoming data on the serial port.


规模制造商通常在输出的权重字符串的开头,结尾或两者都包含无关数据。我通常做的是将我收到的比例数据输出为多种格式,以便我可以看到字符的顺序。一旦我想象出字符,我就可以快速确定我需要做什么来提取重量。



以下是一个刻度控制器的一个示例。



十进制结果= 2-32-32-32-32 -32-32-48-48-76-71-32-13-10-2-32-32-32-32-32-32-48-48-76-71-32-13-10-2-32 -32-32-32-32-32-48-48-76-71-32-13-10-2-32-32-32-32-32-32-48



十进制文本结果= STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0-LG-SPACE-CR-LF-STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE- 0-0-LG-SPACE-CR-LF-STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0-LG-SPACE-CR-LF-STX-SPACE-SPACE-SPACE-SPACE-空间 - SPACE-0



原始结果= 0LG

00LG

00LG

00LG

00LG



一旦我输出了字符串,我确定了一个单独的序列,在这种情况下我t将是(使用上面文本中的十进制文本结果)

STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0-LG-SPACE-CR-LF



既然我有确切的序列,我可以简单地从数据中提取一个序列,一旦我只有一个序列而不是简单地提取重量字符串

来自一个序列,在这种情况下将是1到8的字符,它们将在下面



SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0



在这种情况下,刻度读数为00,但如果读数为300,则重量输出将如下所示。



SPACE-SPACE-SPACE-SPACE-SPACE-3-0-0



我希望这有帮助......
Scale manufacturers often include irrelevant data either at the beginning, ending or both of the outputted weight string. What I usually do is to output the scale data that I receive into multiple formats so that I can visualize the sequence of the characters, Once I visualize the characters I can quickly determine what I need to do to extract the weight only.

Below is one example from one scale controller.

Decimal Results = 2-32-32-32-32-32-32-48-48-76-71-32-13-10-2-32-32-32-32-32-32-48-48-76-71-32-13-10-2-32-32-32-32-32-32-48-48-76-71-32-13-10-2-32-32-32-32-32-32-48

Decimal Text Results = STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0-L-G-SPACE-CR-LF-STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0-L-G-SPACE-CR-LF-STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0-L-G-SPACE-CR-LF-STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0

Raw Results = 0LG
00LG
00LG
00LG
00LG

Once I have the outputted string I determine one individual sequence in this case it would be (Using Decimal Text Results from the text above)
STX-SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0-L-G-SPACE-CR-LF

Now since I have the exact sequence I can simply extract the one sequence from the data, once I have just one sequence than I simply extract the weight string
from the one sequence which in this case would be characters 1 thru 8 which would be below

SPACE-SPACE-SPACE-SPACE-SPACE-SPACE-0-0

In this case the scale read 00 but if it read 300 the weight output would look like the below.

SPACE-SPACE-SPACE-SPACE-SPACE-3-0-0

I hope this helps...


这篇关于在Visual Basic 2013中读取或接收来自串行端口RS232的数据(visual studio .NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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