863
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Dim strData As String
Private Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
End Sub
Private Sub Command1_Click() '发送2进制字节数据流
Dim sj(7) As Byte '14 04 00 02 00 01 92 CF
sj(0) = &H14
sj(1) = &H4
sj(2) = &H0
sj(3) = &H2
sj(4) = &H0
sj(5) = &H1
sj(6) = &H92
sj(7) = &HCF
MSComm1.Output = sj
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
'数据处理代码
End Select
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
End Function
mscDemo.CommPort = 1
mscDemo.Settings = "9600,n,8,1"
mscDemo.InputMode = comInputModeBinary
mscDemo.PortOpen = True
Private Sub mscDemo_OnComm()
If mscDemo.InBufferCount > 0 Then
SCOM_FirstRev = mscDemo.Input
If RichBox_Show = True Then
If Check2.Value = 1 Then
RichTextBox1.Text = RichTextBox1.Text + ParaDecode(SCOM_FirstRev, True)
Else
RichTextBox1.Text = RichTextBox1.Text + ParaDecode(SCOM_FirstRev, False)
End If
End If
SCOM_TotRecBatch = SCOM_TotRec - SCOM_intBatch * 10000
If Check1.Value = 1 And SCOM_TotRecBatch > 10000 Then
RichTextBox1.Text = ""
SCOM_intBatch = SCOM_intBatch + 1
End If
Label13.Caption = "已收:" + Str$(SCOM_TotRec) + " 字节"
End If
End Sub