Dim nBytes As Long nBytes=10 redim Bytes(nBytes-1) ... nBytes=20 redim Bytes(nBytes-1) ...
仅供参考: Private Sub tcpCAS_DataArrival(ByVal bytesTotal As Long) Dim i As Long Dim Total_Length As Long Dim iBuf() As Byte Dim lnx As String On Error Resume Next ' 收当前流 ReDim iBuf(bytesTotal - 1) tcpCAS.GetData iBuf 'log每个收到的字节 i = 0 lnx = "cas-->BYTE:" + Right("0000000" + Hex(i), 8) + "-" For i = 0 To bytesTotal - 1 lnx = lnx + " " + Right("0" + Hex(iBuf(i)), 2) If i Mod 16 = 15 Then logtofile lnx lnx = "cas-->BYTE:" + Right("0000000" + Hex(i + 1), 8) + "-" End If Next i = bytesTotal - 1 If i Mod 16 <> 15 Then logtofile lnx End If End Sub Private Sub logtofile(s As String) Dim f As Integer On Error Resume Next log s f = FreeFile() Open App.Path + "\VBS1.LOG" For Append As #f Print #f, Format(Now, "YYYY-MM-DD hh:mm:ss") + " " + s Close #f If FileLen(App.Path + "\VBS1.LOG") > MAXLOGFILESIZE Then Kill App.Path + "\VBS2.LOG" Name App.Path + "\VBS1.LOG" As App.Path + "\VBS2.LOG" End If End Sub
而且你传出的是字符串类型,你试试把 'MSComm1.Output = Text1.Text' 改成 MSComm1.Output = Cbyte(Text1.Text)
nBytes=10
redim Bytes(nBytes-1)
...
nBytes=20
redim Bytes(nBytes-1)
...
Private Sub tcpCAS_DataArrival(ByVal bytesTotal As Long)
Dim i As Long
Dim Total_Length As Long
Dim iBuf() As Byte
Dim lnx As String
On Error Resume Next
' 收当前流
ReDim iBuf(bytesTotal - 1)
tcpCAS.GetData iBuf
'log每个收到的字节
i = 0
lnx = "cas-->BYTE:" + Right("0000000" + Hex(i), 8) + "-"
For i = 0 To bytesTotal - 1
lnx = lnx + " " + Right("0" + Hex(iBuf(i)), 2)
If i Mod 16 = 15 Then
logtofile lnx
lnx = "cas-->BYTE:" + Right("0000000" + Hex(i + 1), 8) + "-"
End If
Next
i = bytesTotal - 1
If i Mod 16 <> 15 Then
logtofile lnx
End If
End Sub
Private Sub logtofile(s As String)
Dim f As Integer
On Error Resume Next
log s
f = FreeFile()
Open App.Path + "\VBS1.LOG" For Append As #f
Print #f, Format(Now, "YYYY-MM-DD hh:mm:ss") + " " + s
Close #f
If FileLen(App.Path + "\VBS1.LOG") > MAXLOGFILESIZE Then
Kill App.Path + "\VBS2.LOG"
Name App.Path + "\VBS1.LOG" As App.Path + "\VBS2.LOG"
End If
End Sub