VB.net上位机实现串口通讯(入门)

-1、第一天上班刚开始学的时候写的,记录分享一手

软件:vs2019/2022,别的版本我没试过

硬件:usb to ttl的转接器,一根母母线

第一次写文章,比较随意,没那么详细,有什么建议或疑问评论

下一篇准备写个控制接收一个小旋钮动作的上位机,什么时候写看我什么时候闲的发慌

0、功能

  PC自发自收,把USB TO TTL的rxd和txd短路,下拉列表框选择串口,在文本框1中输入文本,点击发送,发送给串口接受并反馈回PC显示在文本框2上。

(有个bug,你必须选对串口,如果你插了别的什么东西,你选错了串口,ok,GG,选对就没事你可以考虑一下如何优化代码解决这个问题,正好可以锻炼一下你的熟练度,哥们我懒得改了)

(怕有人不懂短路,贴个图,就这么接个母母线的意思)

1、窗口界面

2、用到的控件

  3、代码
(1)配置串口函数 
 Sub PortStart() '配置串口
     Call PortClose() 
     Try
     SerialPort1.PortName = ComboBox1.SelectedItem
'计算机串口设置 X,是串口号。可以使用下列列表框选择。

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

     SerialPort1.BaudRate = 9600 '波特率设置
     SerialPort1.DataBits = 8 '数据位设置
     SerialPort1.StopBits = StopBits.One '停止位设置
     SerialPort1.Encoding = Encoding.UTF8
     SerialPort1.DtrEnable = True
     SerialPort1.ReadTimeout = 500 '超时时间
     SerialPort1.NewLine = "1" '行结束符合
 End Sub
 (2)计算机串口读取函数
    Sub GetSerialPortNames() '计算机串口读取
        For Each sp As String In My.Computer.Ports.SerialPortNames
            ComboBox1.Items.Add(sp)
        Next

        If ComboBox1.Items.Count >= 1 Then

            ComboBox1.SelectedIndex() = 0

            ComboBox1.Text = ComboBox1.SelectedItem.ToString

        Else

            MsgBox("请插入USB转串口线!", MsgBoxStyle.Information, "提示!")

            End

        End If
    End Sub
(3) 打开串口函数
    Sub PortOpen()
        Try
            Call PortStart()
            If Not SerialPort1.IsOpen Then
                SerialPort1.Open()
            End If
        Catch ex As UnauthorizedAccessException
            MsgBox(“串口被占用或串口错误!”, MsgBoxStyle.Information, “提示!”)
        End Try

    End Sub
(4)关闭串口函数
    Sub PortClose()
        Try
            SerialPort1.Close()
        Catch ex As Exception
            MsgBox("串口未打开或串口异常!", MsgBoxStyle.Information, "提示!")
        End Try
    End Sub
(5)窗体加载事件
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call GetSerialPortNames()

    End Sub
(6)下拉列表框选择事件
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Call PortOpen()
    End Sub
(7)发送按钮点击事件
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            ' 将文本框中的内容转换为字节数组
            Dim sendData As Byte() = Encoding.ASCII.GetBytes(TextBox1.Text)

            ' 发送数据
            SerialPort1.Write(sendData, 0, sendData.Length)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
(8)串口数据接收事件
    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            ' 读取接收缓冲区中的数据
            Dim receiveData As String
            receiveData = SerialPort1.ReadLine

            ' 将接收到的数据显示在文本框中
            Me.Invoke(Sub() TextBox2.AppendText(receiveData))

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

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值