VB.NET连扫码q

VB.NET连扫码q,第1张

概述程序是从网上copy的。还没有真正看串口的使用方式,已经成功连接并读取数据。 收获:   1) combox.selectedindex=0    '选择combox里面的Item的索引 2)在Form1中加入serial1或使用 dim serialport1 as serialport 3)扫码q上读取的值为Char, Textbox里面的最大的值为int32。不知道为什么一开始扫到的值在te

程序是从网上copy的。还没有真正看串口的使用方式,已经成功连接并读取数据。

收获: 1) comBox.selectedindex=0'选择comBox里面的Item的索引

2)在Form1中加入serial1或使用 dim serialport1 as serialport

3)扫码q上读取的值为Char, TextBox里面的最大的值为int32。不知道为什么一开始扫到的值在textBox分成了两行,后来不知道扫码q怎么了就变成了一行,疑问!!

4)读取电脑上的串口serialport.getportnames()直接得到设备连到了那个串口,如果串口上只连了一个设备的话。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 imports System imports System.IO.Ports Public Class Form1 Private Sub Form1_Load( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles MyBase .Load '获取计算机有效串口 Dim ports As String () = SerialPort.GetPortnames() '必须用命名空间,用SerialPort,获取计算机的有效串口 Dim port As String For Each port In ports portnameBox.Items.Add(port) '向comboBox中添加项 Next port '初始化界面 baudrateBox.Selectedindex = 2 portnameBox.Selectedindex = 0 Serial_Port1() '初始化串口 Label3.Text = SerialPort1.IsOpen statuslabel.Text = "串口未连接" statuslabel.Forecolor = color.Red sendBox.Text = "123" ' baudrateBox.Text = baudrateBox.Items(0) 注释和不注释的地方可以替换 'portnameBox.Text = portnameBox.Items(0) End Sub Private Sub Serial_Port1() '设置串口参数 SerialPort1.Baudrate = Val(baudrateBox.Text) '波特率 SerialPort1.Portname = portnameBox.Text '串口名称 SerialPort1.DataBits = 7 '数据位 SerialPort1.StopBits = IO.Ports.StopBits.One '停止位 SerialPort1.Parity = IO.Ports.Parity.None '校验位 End Sub '关闭串口连接 Private Sub closebtn_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles closebtn.Click Try SerialPort1.Close() '关闭串口 Label3.Text = SerialPort1.IsOpen If SerialPort1.IsOpen = False Then Statuslabel.Text = "串口未连接" Statuslabel.Forecolor = color.Red receiveBox.Text = "" receivebytes.Text = "" End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub '打开串口连接 Private Sub openbtn_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles openbtn.Click Try SerialPort1.open() '打开串口 Label3.Text = SerialPort1.IsOpen If SerialPort1.IsOpen = True Then statuslabel.Text = "串口已连接" statuslabel.Forecolor = color.Green End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub '发送数据 Private Sub button1_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles closebtn.Click Try SerialPort1.Write(sendBox.Text) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub '触发接收事件 Public Sub Sp_DataReceived( ByVal sender As Object , ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Me .Invoke( New EventHandler( AddressOf Sp_Receiving)) '调用接收数据函数 End Sub '接收数据 Private Sub Sp_Receiving( ByVal sender As Object , ByVal e As EventArgs) Dim strIncoming As String Try receivebytes.Text = Str(Val(receivebytes.Text) + SerialPort1.BytesToRead) If SerialPort1.BytesToRead > 0 Then Threading.Thread.Sleep(100) '添加的延时 strIncoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据 SerialPort1.discardInBuffer() receiveBox.Text = strIncoming ListBox1.Items.Add(strIncoming) End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub End Class
总结

以上是内存溢出为你收集整理的VB.NET连扫码q全部内容,希望文章能够帮你解决VB.NET连扫码q所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1260687.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存