16,554
社区成员
发帖
与我相关
我的任务
分享
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AxMSComm1.Settings = "9600,N,8,1"
AxMSComm1.RThreshold = 1
AxMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary
AxMSComm1.PortOpen = True
End Sub
Private Sub AxMSComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxMSComm1.OnComm
Dim bytInput() As Byte
Dim intInputLen As Integer
Select Case AxMSComm1.CommEvent
'Case comEvReceive
Case 2
'此处添加处理接收的代码
'AxMSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = AxMSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = AxMSComm1.Input
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
'If Mid(strData, 17, 2) = "20" And Mid(strData, 19, 2) = "20" And Len(strData) = 20 Then
TextBox1.Text = strData
strData = ""
'End If
End Select
End Sub
Public Class Form1
Dim mSTR As String
Dim strData As String
Dim strDat As String
'Dim mSize1 As Integer
Dim mRecvByte() As Byte
Dim Hexsj As String
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim mSize As Integer = Me.SerialPort1.BytesToRead
'mSize1 = mSize
ReDim mRecvByte(mSize - 1)
SerialPort1.Read(mRecvByte, 0, mSize)
'mSTR = Encoding.Default.GetString(mRecvByte, 0, mSize)
BeginInvoke(New EventHandler(AddressOf ONMLoadA), SerialPort1.BytesToRead()) '使用委托方式显示接收到的字符串
End Sub
Sub ONMLoadA(ByVal sender As System.Object, ByVal e As System.EventArgs) '委托
'TextBox4.Text = mSTR
Dim i As Integer
For i = 0 To UBound(mRecvByte) 'mSize - 1
strData = strData & IIf(mRecvByte(i) > 15, Hex(mRecvByte(i)), "0" & Hex(mRecvByte(i)))
Next
Dim sj As Byte
For i = 1 To Len(strData) Step 2 '处理为ASCII字符
sj = Val("&H" & Mid(strData, i, 2))
If sj < 32 Or sj > 128 Then '当接收字节中有Chr(0)时,其后字符被切割
strDat = strDat & "."
Else
strDat = strDat & Chr(sj)
End If
Next
TextBox1.Text = strDat '显示字符
TextBox2.Text = strData '显示为16进制
TextBox3.Text = Len(strData) / 2
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.BaudRate = 9600 '波特率设为
SerialPort1.PortName = "com1" '端口为串口
SerialPort1.ReceivedBytesThreshold = 1
If SerialPort1.IsOpen = False Then SerialPort1.Open() '打开串口
Hexsj = "002702013524200239892700020020010201E9030D07"
TxtSend.Text = Hexsj
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strData = ""
strDat = ""
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim bytHex() As Byte
ReDim bytHex((Len(Hexsj) \ 2) - 1)
Dim i As Integer
For i = 1 To Len(Hexsj) Step 2
bytHex((i - 1) / 2) = Val("&H" & Mid(Hexsj, i, 2))
Next
SerialPort1.Write(bytHex, 0, bytHex.Length)
End Sub
End Class