标题: 原创-visual basic net串口样板架构 [打印本页]

作者: xjbworld    时间: 2023-9-5 11:41
标题: 原创-visual basic net串口样板架构
visual basicnet其实比visual C#更好用,作为嵌入式开发人员,用basic入门更简单,可以很快做出调试产品工具。附件是完整项目
这个是电脑串口寻找子函数
   Private Sub com_channel_get(ByVal getcom1 As Integer)

        Dim getcom As Integer
        Dim ports As String() = SerialPort.GetPortNames() '必须用命名空间,用SerialPort,获取计算机的有效串口
        Dim port As String

        For Each port In ports
            Console.WriteLine(port)
        Next port

        ComboBox1.Items.Clear()
        ComboBox1.Text = ""
        For Each port In ports
            ComboBox1.Items.Add(port) '向combobox中添加项
        Next port
        getcom = ComboBox1.Items.Count '读ComboBox1的数值


        Dim comnum As Integer
        Dim textcom As String
        ComboBox1.Enabled = False


        '     Exit Sub

        Dim mBaudRate As Integer
        Dim mParity As IO.Ports.Parity
        Dim mDataBit As Integer
        Dim mStopbit As IO.Ports.StopBits
        Dim mPortName As String = "COM1"


        mBaudRate = 9600   '比特率
        mParity = Parity.None  '校验位检查设定
        mDataBit = 8    '数据位设定值
        mStopbit = StopBits.One  '停止位设定值



        For comnum = 0 To getcom - 1 Step 1
            ComboBox1.SelectedIndex() = comnum '第一个端口为默认
            mPortName = ComboBox1.GetItemText(ComboBox1.SelectedItem) '欲开启的通讯端口  
            '     mPortName = "COM3"
        Next comnum

        RS232 = New IO.Ports.SerialPort(mPortName, mBaudRate, mParity, mDataBit, mStopbit)

        If RS232.IsOpen = True Then  '尚未开启
            RS232.Close()  '开启通讯端口
        End If

        Threading.Thread.Sleep(50)

        Try
            If RS232.IsOpen = False Then  '尚未开启
                RS232.Open()  '开启通讯端口
                RS232.ReceivedBytesThreshold = 1        '设置引发事件的门限值
            End If
        Catch
            Try

                If RS232.IsOpen = True Then  '尚未开启
                    RS232.Close()  '开启通讯端口
                End If
            Catch
            End Try
        End Try

        Exit Sub

        Dim hexBytes(3) As Byte

        hexBytes(0) = &H6A
        'RS232.Write(hexBytes, 0, 1)
        RS232.Write("AT+DISC" + Chr(&HD) + Chr(&HA))
        Threading.Thread.Sleep(500)

        '   Dim rxdata As String = RS232_Data(3)
        Label1.Text = "通信未连接"
        Label1.ForeColor = Color.Red
        Dim textcompare As String = ""
        If (Len(gl_string) > 4) Then
            textcompare = Mid(gl_string, 1, 5)
        End If
        'ERROR OR OK+DISC
        If ((textcompare = "ERROR") Or (textcompare = "OK+DI")) Then
            Label1.Text = "通信已连接"
            Label1.ForeColor = Color.Green
            setrxnum = 4

        End If
        '    If (RS232.BytesToRead >= 5) Then
        'setrxnum = 8 '打开串口自动读取
        '   Exit For
        '    End If


        If RS232.IsOpen = True Then  '尚未开启
            RS232.Close()  '开启通讯端口
        End If


    End Sub
prj_Vbnet.zip (509.89 KB, 下载次数: 1)




作者: heicad    时间: 2024-1-22 02:43
好资料,51黑有你更精彩!!!




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1