imports System.IOimports System.IO.Portsimports System.Runtime.Remoting.Messaging '这个不忘记imports System.TextDelegate Function CommOp(ByVal pval As String,ByVal pDelay As Integer) As StringModule Module1 Public WithEvents Rs232 As SerialPort Sub Main() Rs232 = New SerialPort("COM1",9600,Parity.None,8,StopBits.One) If Rs232.IsOpen Then Exit Sub Rs232.open() Console.Writeline("*******执行串口 *** 作*********") Dim c As CommOp = New CommOp(AddressOf CommOpWrite) c.BeginInvoke("ssssssss",50,nothing,nothing) ' Dim itfAR As IAsyncResult = c.BeginInvoke("ssssssss",New AsyncCallback(AddressOf CommOpComplete)," *** 作完成") '继续其他工作 Console.Readline() End Sub ' Sub CommOpComplete(ByVal itfAR As IAsyncResult) Console.Writeline("已经完成数据处理") Dim ar As AsyncResult = CType(itfAR,AsyncResult) Dim c As CommOp = CType(ar.AsyncDelegate,CommOp) Console.Writeline("输出的结果{0}",c.EndInvoke(itfAR).ToString) '接受返回的信息 Dim Msg As String = CType(itfAR.AsyncState,String) Console.Writeline(Msg) End Sub Function CommOpWrite(ByVal pVal As String,ByVal pDelay As Integer) As String Rs232.Write(pVal) Threading.Thread.Sleep(pDelay) Dim buf() As Byte Dim Builder As StringBuilder = New StringBuilder '避免在事件处理方法中反复的创建,所以定义到外面 '输入模块的命令是@01,返回>0F00 +vbcrlf Dim TT As Integer Dim bTimeOver As Boolean = False TT = Environment.TickCount bTimeOver = False Do If Rs232.BytesToRead >= 5 Then Exit Do If Environment.TickCount - TT > 1000 Then bTimeOver = True Loop Until bTimeOver If bTimeOver Then Return String.Empty Else Dim n As Integer = Rs232.BytesToRead ReDim buf(n - 1) Rs232.Read(buf,n) For Each b As Byte In buf Builder.Append(b.ToString("X2") + " ") Next Console.Writeline(Builder.ToString) Return Builder.ToString End If End FunctionEnd Module总结
以上是内存溢出为你收集整理的VB.NET 串口访问之四(绝密级)全部内容,希望文章能够帮你解决VB.NET 串口访问之四(绝密级)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)