基于16进制收发的代码: Option Explicit Dim BytReceived() As Byte Dim strData As String Dim lenInput As Integer Dim bytSendByte() As Byte '发送二进制数据 Dim strSendText As String '发送文本数据 Dim blnAutoSendFlag As Boolean Dim openFlag As Boolean Private Sub cmdRecv_Click() Dim bytDataShow() As Byte Dim varData As Variant Dim strData As String Dim i As Integer
For i = 0 To UBound(bytDataShow) - 1 strData = strData + Chr(bytDataShow(i)) Next i
Text3.Text = strData End SubPrivate Sub cmdOpen_Click() On Error GoTo erruser If openFlag Then cmdOpen.Caption = "打开串口" MSComm1.PortOpen = False '打开端口 Timer2.Enabled = False Shape1.FillColor = vbRed Label5 = "关闭" Else cmdOpen.Caption = "关闭串口" Shape1.FillColor = vbGreen Label5 = "打开" MSComm1.PortOpen = True '打开端口 If blnAutoSendFlag = True Then Timer2.Enabled = True End If End If openFlag = Not openFlag erruser:End SubPrivate Sub cmdSend_Click() Dim sj() As Byte Dim sj_Txt As String Dim i As Integer sj_Txt = TxtSend 'TxtSend = "800A00113135323634389794" ReDim sj(Len(sj_Txt) / 2 - 1) For i = 0 To Len(sj_Txt) - 1 Step 2 sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2)) Next MSComm1.Output = sj End Sub '字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数Private Sub cmdAutoSend_Click() If blnAutoSendFlag Then Me.Timer2.Enabled = False Me.cmdAutoSend.Caption = "自动发送" Else Me.Timer2.Enabled = True Me.cmdAutoSend.Caption = "停止发送"
End If blnAutoSendFlag = Not blnAutoSendFlag End SubPrivate Sub Command1_Click() Dim sj_Txt As String sj_Txt = TxtSend If MSComm1.PortOpen = True Then MSComm1.Output = sj_Txt End If End SubPrivate Sub MSComm1_OnComm() On Error Resume Next Dim strBuff As String Text1 = "" Select Case MSComm1.CommEvent Case 2 MSComm1.InputLen = 0 strBuff = MSComm1.Input BytReceived() = strBuff jieshou LblJieshou = Text1 lenInput = Len(LblJieshou) Text3 = lenInput \ 2 Text2 = Mid(LblJieshou, 1, 2) '数据处理代码 If Mid(LblJieshou, 1, 2) = "FF" Then Text4 = Mid(LblJieshou, 5, 4) End If End Select Timer1_Timer End Sub Private Sub Form_Load() Dim port As Integer port = 1 MSComm1.CommPort = port 'COM端口 MSComm1.Settings = "9600,n,8,1" MSComm1.InputMode = comInputModeBinary '采用二进制传输 MSComm1.InBufferCount = 0 '清空接受缓冲区 MSComm1.OutBufferCount = 0 '清空传输缓冲区 MSComm1.RThreshold = 1 '产生MSComm事件 MSComm1.InBufferSize = 1024 Text1 = "" Text3 = "" LblJieshou = "" Text4 = "" TxtSend = "" Timer1.Interval = 1000 Label4 = "端口:COM" & port Label5 = "关闭" End SubPrivate Sub Timer1_Timer() strData = "" End SubPublic Sub jieshou1() '接收处理为16进制 Dim i As Integer For i = 0 To UBound(BytReceived) If Len(Hex(BytReceived(i))) = 1 Then strData = strData & "0" & Hex(BytReceived(i)) Else strData = strData & Hex(BytReceived(i)) End If Next Text1 = strData End SubPrivate Sub Timer2_Timer() cmdSend_Click End Sub Public Function jieshou() '接收数据处理为16进制 Dim i As Integer For i = 0 To UBound(BytReceived) If Len(Hex(BytReceived(i))) = 1 Then strData = strData & "0" & Hex(BytReceived(i)) Else strData = strData & Hex(BytReceived(i)) End If Next Text1 = strData End Function
Option Explicit
Dim BytReceived() As Byte
Dim strData As String
Dim lenInput As Integer
Dim bytSendByte() As Byte '发送二进制数据
Dim strSendText As String '发送文本数据
Dim blnAutoSendFlag As Boolean
Dim openFlag As Boolean
Private Sub cmdRecv_Click()
Dim bytDataShow() As Byte
Dim varData As Variant
Dim strData As String
Dim i As Integer
MSComm1.InputLen = 0
varData = MSComm1.Input
bytDataShow = varData
For i = 0 To UBound(bytDataShow) - 1
strData = strData + Chr(bytDataShow(i))
Next i
Text3.Text = strData
End SubPrivate Sub cmdOpen_Click()
On Error GoTo erruser
If openFlag Then
cmdOpen.Caption = "打开串口"
MSComm1.PortOpen = False '打开端口
Timer2.Enabled = False
Shape1.FillColor = vbRed
Label5 = "关闭"
Else
cmdOpen.Caption = "关闭串口"
Shape1.FillColor = vbGreen
Label5 = "打开"
MSComm1.PortOpen = True '打开端口
If blnAutoSendFlag = True Then
Timer2.Enabled = True
End If
End If
openFlag = Not openFlag
erruser:End SubPrivate Sub cmdSend_Click()
Dim sj() As Byte
Dim sj_Txt As String
Dim i As Integer
sj_Txt = TxtSend
'TxtSend = "800A00113135323634389794"
ReDim sj(Len(sj_Txt) / 2 - 1)
For i = 0 To Len(sj_Txt) - 1 Step 2
sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))
Next
MSComm1.Output = sj
End Sub
'字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数Private Sub cmdAutoSend_Click()
If blnAutoSendFlag Then
Me.Timer2.Enabled = False
Me.cmdAutoSend.Caption = "自动发送"
Else
Me.Timer2.Enabled = True
Me.cmdAutoSend.Caption = "停止发送"
End If
blnAutoSendFlag = Not blnAutoSendFlag
End SubPrivate Sub Command1_Click()
Dim sj_Txt As String
sj_Txt = TxtSend
If MSComm1.PortOpen = True Then
MSComm1.Output = sj_Txt
End If
End SubPrivate Sub MSComm1_OnComm()
On Error Resume Next
Dim strBuff As String
Text1 = ""
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
LblJieshou = Text1
lenInput = Len(LblJieshou)
Text3 = lenInput \ 2
Text2 = Mid(LblJieshou, 1, 2)
'数据处理代码
If Mid(LblJieshou, 1, 2) = "FF" Then
Text4 = Mid(LblJieshou, 5, 4)
End If
End Select
Timer1_Timer
End Sub
Private Sub Form_Load()
Dim port As Integer
port = 1
MSComm1.CommPort = port 'COM端口
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
Text1 = ""
Text3 = ""
LblJieshou = ""
Text4 = ""
TxtSend = ""
Timer1.Interval = 1000
Label4 = "端口:COM" & port
Label5 = "关闭"
End SubPrivate Sub Timer1_Timer()
strData = ""
End SubPublic Sub jieshou1() '接收处理为16进制
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
Text1 = strData
End SubPrivate Sub Timer2_Timer()
cmdSend_Click
End Sub
Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
Text1 = strData
End Function
留油箱或者到这里留言
http://post.baidu.com/f?kz=205203232
[email protected]
[email protected]