VB.NET 串口访问之二

VB.NET 串口访问之二,第1张

概述  界面如上面 VB.NET 串口异步访问 所示,新的程序如下。 Imports SystemImports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.DrawingImports System.LinqImports System.Tex

界面如上面

VB.NET 串口异步访问

所示,新的程序如下。

imports Systemimports System.Collections.Genericimports System.ComponentModelimports System.Dataimports System.Drawingimports System.linqimports System.Textimports System.IO.Portsimports System.Threadingimports System.Text.RegularExpressionsPublic Class Form1    WithEvents Comm As SerialPort = New SerialPort    Private Builder As StringBuilder = New StringBuilder '避免在事件处理方法中反复的创建,所以定义到外面    Private ReceiveCount As Long = 0     '接收计数    Private SendCount As Long = 0        '发送计数    Private Listening As Boolean = False  '是否没有执行完invoke相关 *** 作     Private Closingg As Boolean = False     '是否正在关闭串口,执行Application.DoEvents,并阻止再次invoke       Public Delegate Sub UpdateData(ByVal mByte() As Byte)    Public Sub ShowData(ByVal mByte() As Byte)        Console.Writeline(mByte)        ReceiveCount += mByte.Length          '统计字节总数        Builder.Clear()                       '清除字符串构造器的内容         Console.Writeline("Main1() invoke on thread{0}.",Thread.CurrentThread.ManagedThreadID)        If CheckBoxHex.Checked Then            For Each b As Byte In mByte                Builder.Append(b.ToString("X2") + " ")            Next        Else            Builder.Append(EnCoding.ASCII.GetString(mByte))        End If        TxtGet.AppendText(Builder.ToString)        labelGetCount.Text = "Get:" + ReceiveCount.ToString    End Sub    '**************************************************************    '委派子程序    '处理上述通信端口的接收事件    '由于欲将数据显示到接收文字框中,因此必须检查是否由另外的Thread    '所呼叫的,若是,则必须先建立委派对象    'Invoke用于在拥有控制项基础视窗控制代码的执行绪上执行委派    '**************************************************************    Private Sub displayText(ByVal comData() As Byte)        '如果呼叫txtReceive的是另外的执行绪,传回True        Try            If Me.TxtGet.Invokerequired Then                '利用委派型别建立委派对象,并指定委派的函数                Dim d As New UpdateData(AddressOf ShowData)                '用大括号 {} 括住初始值,藉以初始化阵列的值。                Me.Invoke(d,New Object() {comData})  '以指定的引数清单叫用函数            Else  '相同的执行绪                ShowData(comData)   '将收到的数据填入接收文字框中            End If        Catch ex As Exception            MsgBox(ex.ToString)        Finally        End Try    End Sub      Private Sub Form1_Load(sender As System.Object,e As System.EventArgs) Handles MyBase.Load        '初始化下拉串口名称列表框        Dim Ports() As String = SerialPort.GetPortnames        Array.sort(Ports)        ComboPortname.Items.AddRange(Ports)        ComboPortname.Selectedindex = IIf(ComboPortname.Items.Count > 0,-1)        ComboBaudrate.Selectedindex = ComboBaudrate.Items.IndexOf("9600")        '初始化Serialport对象        Comm.Newline = vbCrLf        Comm.RtsEnable = True        'AddHandler Obj.Ev_Event,AddressOf EventHandler        'RemoveHandler Obj.Ev_Event,AddressOf EventHandler        'AddHandler Comm.DataReceived,AddressOf Comm_DataReceived        BtnXreset.PerformClick()    End Sub    Private Sub Comm_DataReceived(sender As Object,e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Comm.DataReceived        If Closingg Then Return '如果正在关闭,忽略 *** 作,直接返回,尽快的完成串口监听线程的一次循环           Try            Listening = True                    '设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。            Dim n As Long = Comm.BytesToRead    '先记录下来,避免某种原因,人为的原因, *** 作几次之间时间长,缓存不一致               Dim Buf(n - 1) As Byte              '声明一个临时数组存储当前来的串口数据             Comm.Read(Buf,n)                '读取缓冲数据            Console.Writeline("Main0() invoke on thread{0}.",Thread.CurrentThread.ManagedThreadID)            displayText(Buf)            'Dim b As UpdateData = AddressOf ShowData            'b(Buf)   '同步线程之间传递数据时,必须考虑textBox不在同一线程        Catch ex As Exception            MsgBox(ex.Message)        Finally            Listening = False                    '我用完了,ui可以关闭串口了。        End Try    End Sub    Private Sub BtnXOpen_Click(sender As System.Object,e As System.EventArgs) Handles BtnXOpen.Click        '根据当前串口对象,来判断 *** 作         If Comm.IsOpen Then            Closingg = True '            While Listening                Application.DoEvents()            End While            '打开时点击,则关闭串口            Comm.Close()            Closingg = False        Else            Comm.Portname = ComboPortname.Text            Comm.Baudrate = Integer.Parse(ComboBaudrate.Text)            Try                Comm.open()            Catch ex As Exception                '捕获到异常信息,创建一个新的comm对象,之前的不能用了。                 Comm = New SerialPort                '现实异常信息给客户。                 MessageBox.Show(ex.Message)            End Try        End If        '设置按钮的状态           BtnXOpen.Text = IIf(Comm.IsOpen,"Close","Open")        BtnXOpen.Enabled = Comm.IsOpen    End Sub    '动态的修改获取文本框是否支持自动换行。    Private Sub CheckBoxNewlineGet_CheckedChanged(sender As System.Object,e As System.EventArgs) Handles CheckBoxNewlineGet.CheckedChanged        TxtGet.WorDWrap = CheckBoxNewlineGet.Checked    End Sub    Private Sub BtnXSend_Click(sender As System.Object,e As System.EventArgs) Handles BtnXSend.Click        Dim n As Integer = 0  '定义一个变量,记录发送了几个字节         If checkBoxHexSend.Checked Then   '16进制发送             '我们不管规则了。如果写错了一些,我们允许的,只用正则得到有效的十六进制数               Dim Mc As MatchCollection = Regex.Matches(TxtSend.Text.Trim,"(?i)[/da-f]{2}")   '"(?i)[/da-f]{2}"            Dim buf As List(Of Byte) = New List(Of Byte)            '依次添加到列表中               For Each m As Match In Mc                '  buf.Add(Byte.Parse(m.Value))                buf.Add(Byte.Parse(m.Value,System.Globalization.NumberStyles.Hexnumber))            Next            '转换列表为数组后发送              Comm.Write(buf.ToArray,buf.Count)            n = buf.Count        Else                             'ascii编码直接发送             '包含换行符            If checkBoxNewlinesend.Checked Then                Comm.Writeline(TxtSend.Text)                n = TxtSend.Text.Length + 2            Else                Comm.Write(TxtSend.Text)                n = TxtSend.Text.Length            End If        End If        SendCount += n    '累加发送字节数         labelSendCount.Text = "Send:" + SendCount.ToString    End Sub    Private Sub BtnXreset_Click(sender As System.Object,e As System.EventArgs) Handles BtnXreset.Click        '复位接受和发送的字节数计数器并更新界面。        SendCount = 0        ReceiveCount = 0        labelGetCount.Text = "Get:0"        labelSendCount.Text = "Send:0"        Builder.Clear()    End Sub    Private Sub BtxClear_Click(sender As System.Object,e As System.EventArgs) Handles BtxClear.Click        TxtGet.Text = ""        Builder.Clear()    End SubEnd Class

,程序如下

总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存