vb.net 串口事件 控制 怎么通过串口返回的数距 在不同的窗体显示 15
比如:返回“1”则在窗体1的textbox1上显示1返回“2”则在窗体2的textbox1上显示2返回“3”则在窗体3的textbox1上显示3ImportsSystem...
比如:返回“1”则在窗体1的textbox1上显示1
返回“2”则在窗体2的textbox1上显示2
返回“3”则在窗体3的textbox1上显示3
Imports System
Imports System.Text
Imports System.IO.Ports
Public Class ClARMComm
Public Event AcceptData(ByVal Barcode As String, ByVal Material As String, ByVal Product As String)
WithEvents ARMPort As New SerialPort '定义条形码端口
Dim BarcodeCommData As String
Public Sub PortOpen(ByVal MyCOMPort As String, ByVal MyBaudRates As Integer, ByVal MyDatabit As Short, _
ByVal MyParity As System.IO.Ports.Parity, ByVal MyStopBites As System.IO.Ports.StopBits) '打开打印机端口
Try
ARMPort.PortName = MyCOMPort
ARMPort.BaudRate = MyBaudRates
ARMPort.DataBits = MyDatabit
ARMPort.Parity = MyParity
ARMPort.StopBits = MyStopBites
ARMPort.ReadBufferSize = 1024
ARMPort.WriteBufferSize = 1024
ARMPort.Encoding = Encoding.GetEncoding(0)
ARMPort.Open()
ARMPort.ReceivedBytesThreshold = 1
ARMPort.DiscardInBuffer()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataArrival(ByVal send As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles ARMPort.DataReceived
Dim strIncoming As String '接收的字符串
Dim ReciveBytes As Integer '缓冲区字符长度
Dim Bites() As Byte = Nothing
Dim PCCommand As String = "" '串口通讯命令
If ARMPort.BytesToRead > 0 Then
Threading.Thread.Sleep(100) '延时0.1S 读取缓冲区数据
ReciveBytes = ARMPort.BytesToRead '缓冲区字符长度
ReDim Bites(0 To ReciveBytes - 1)
ARMPort.Read(Bites, 0, ReciveBytes) '读取缓冲区所有的数据
ARMPort.DiscardInBuffer() '清除缓冲区的数据
End If
Dim oEncoder As New System.Text.UTF8Encoding
strIncoming = oEncoder.GetString(Bites, 0, UBound(Bites)) '将二进制数组转化为字符串
Select Case strIncoming
这里不会了 。我尝试 产生数据事件 可是不行哦
end select
end sub 展开
返回“2”则在窗体2的textbox1上显示2
返回“3”则在窗体3的textbox1上显示3
Imports System
Imports System.Text
Imports System.IO.Ports
Public Class ClARMComm
Public Event AcceptData(ByVal Barcode As String, ByVal Material As String, ByVal Product As String)
WithEvents ARMPort As New SerialPort '定义条形码端口
Dim BarcodeCommData As String
Public Sub PortOpen(ByVal MyCOMPort As String, ByVal MyBaudRates As Integer, ByVal MyDatabit As Short, _
ByVal MyParity As System.IO.Ports.Parity, ByVal MyStopBites As System.IO.Ports.StopBits) '打开打印机端口
Try
ARMPort.PortName = MyCOMPort
ARMPort.BaudRate = MyBaudRates
ARMPort.DataBits = MyDatabit
ARMPort.Parity = MyParity
ARMPort.StopBits = MyStopBites
ARMPort.ReadBufferSize = 1024
ARMPort.WriteBufferSize = 1024
ARMPort.Encoding = Encoding.GetEncoding(0)
ARMPort.Open()
ARMPort.ReceivedBytesThreshold = 1
ARMPort.DiscardInBuffer()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataArrival(ByVal send As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles ARMPort.DataReceived
Dim strIncoming As String '接收的字符串
Dim ReciveBytes As Integer '缓冲区字符长度
Dim Bites() As Byte = Nothing
Dim PCCommand As String = "" '串口通讯命令
If ARMPort.BytesToRead > 0 Then
Threading.Thread.Sleep(100) '延时0.1S 读取缓冲区数据
ReciveBytes = ARMPort.BytesToRead '缓冲区字符长度
ReDim Bites(0 To ReciveBytes - 1)
ARMPort.Read(Bites, 0, ReciveBytes) '读取缓冲区所有的数据
ARMPort.DiscardInBuffer() '清除缓冲区的数据
End If
Dim oEncoder As New System.Text.UTF8Encoding
strIncoming = oEncoder.GetString(Bites, 0, UBound(Bites)) '将二进制数组转化为字符串
Select Case strIncoming
这里不会了 。我尝试 产生数据事件 可是不行哦
end select
end sub 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询