通过rs232从Cell计数器接收数据

时间:2019-08-19 22:28:16

标签: vb.net serial-port

我有一台细胞计数机,在测试结束后通过rs232电缆自动发送数据。我尝试使用以下代码接收该数据,但是尽管多次更改了BaudRate值,但我什么也没得到。有什么问题吗?

我的代码:

Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel

Public Class ComReadWrite
    Dim myPorts As Array
    Dim txtline As String
    Dim txtchar As String
    Dim txtbyte As String
    Dim txtexisting As String
    Delegate Sub setTextCallBack(ByVal txt As String)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myPorts = IO.Ports.SerialPort.GetPortNames()
        portnamecombo.Items.AddRange(myPorts)
        WriteButton.Enabled = False
        CloseButton.Enabled = False
        BaudRateBox.Items.Add(110)
        BaudRateBox.Items.Add(300)
        BaudRateBox.Items.Add(600)
        BaudRateBox.Items.Add(1200)
        BaudRateBox.Items.Add(2400)
        BaudRateBox.Items.Add(4800)
        BaudRateBox.Items.Add(9600) 'Populate the baudratebox Combo box to common baud rates used 
        BaudRateBox.Items.Add(14400)
        BaudRateBox.Items.Add(19200)
        BaudRateBox.Items.Add(38400)
        BaudRateBox.Items.Add(57600)
        BaudRateBox.Items.Add(115200)
        BaudRateBox.Items.Add(128000)
        BaudRateBox.Items.Add(256000)
        BaudRateBox.Text = 38400
    End Sub

    Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
        SerialPort1.PortName = portnamecombo.Text
        SerialPort1.BaudRate = BaudRateBox.Text
        SerialPort1.ReadTimeout = 500

        SerialPort1.Parity = Parity.None
        SerialPort1.DataBits = 8
        SerialPort1.StopBits = StopBits.One

        SerialPort1.Open()

        Start.Enabled = False
        WriteButton.Enabled = True
        CloseButton.Enabled = True
    End Sub

    Private Sub WriteButton_Click(sender As Object, e As EventArgs) Handles WriteButton.Click
        SerialPort1.Write(Chr(6))
    End Sub

    Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
        SerialPort1.Close()
        Start.Enabled = True
        WriteButton.Enabled = False
        CloseButton.Enabled = False
    End Sub

    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            RecievedText(SerialPort1.ReadExisting())

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub RecievedText(ByVal txt As String)
        If Me.ReadBox.InvokeRequired Then
            Dim x As New setTextCallBack(AddressOf RecievedText)
            Me.Invoke(x, New Object() {(txt)})
        Else

            Me.ReadBox.Text &= txt
        End If
    End Sub
End Class

0 个答案:

没有答案