864
社区成员
发帖
与我相关
我的任务
分享Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strJ As String '校验位
Dim bytS1(1 To 1) As Byte, bytS2(1 To 1) As Byte, bytS3(1 To 1) As Byte, bytS4(1 To 1) As Byte, bytS5(1 To 1) As Byte, bytS6(1 To 1) As Byte, bytS7(1 To 1) As Byte, bytS8(1 To 1) As Byte, bytS9(1 To 1) As Byte, bytS10(1 To 1) As Byte, bytS11(1 To 1) As Byte, bytS12(1 To 1) As Byte, bytS13(1 To 1) As Byte, bytS14(1 To 1) As Byte, bytS15(1 To 1) As Byte, bytS16(1 To 1) As Byte, bytS17(1 To 1) As Byte, bytS18(1 To 1) As Byte, bytS19(1 To 1) As Byte, bytS20(1 To 1) As Byte '发送字节
Dim bytS21(1 To 1) As Byte, bytS22(1 To 1) As Byte, bytS23(1 To 1) As Byte, bytS24(1 To 1) As Byte, bytS25(1 To 1) As Byte, bytS26(1 To 1) As Byte, bytS27(1 To 1) As Byte, bytS28(1 To 1) As Byte, bytS29(1 To 1) As Byte, bytS30(1 To 1) As Byte, bytS31(1 To 1) As Byte, bytS32(1 To 1) As Byte, bytS33(1 To 1) As Byte, bytS34(1 To 1) As Byte, bytS35(1 To 1) As Byte, bytS36(1 To 1) As Byte, bytS37(1 To 1) As Byte, bytS38(1 To 1) As Byte, bytS39(1 To 1) As Byte, bytS40(1 To 1) As Byte '发送字节
Dim intTim As Integer
Dim bytGet() As Byte
Dim bytGetAll(0 To 100000) As Long 'byte
Dim intNumI As Integer
Dim i As Integer
Private Sub Form_Load()
On Error GoTo errHand
Timer1.Enabled = False
intTim = 1
If cmdOpen.Caption = "打 开" Then
cmdOpen.Caption = "关 闭"
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
MSComm1.CommPort = 1 'Right(Combo1.Text, 1)
MSComm1.Settings = "9600,N,8,1" 'Combo2.Text & strJ & Combo4 & Combo5 '波特率9600,N为奇校验,8个数据位,1个停止位
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0 '读取整个接收缓冲区内码或消除
If MSComm1.PortOpen = False Then '端口未打开
MSComm1.PortOpen = True
Pic.BackColor = vbRed
'Pic.Picture = LoadPicture(App.Path & "\red.jpg")
End If
Else
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
cmdOpen.Caption = "打 开"
Pic.BackColor = vbBlue
'Pic.Picture = LoadPicture(App.Path & "\blue.jpg")
End If
Exit Sub
errHand:
MsgBox Err.Description
End Sub
Private Sub cmdOpen_Click() '打开
On Error GoTo errHand
If cmdOpen.Caption = "打 开" Then
cmdOpen.Caption = "关 闭"
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
MSComm1.CommPort = 1 'Right(Combo1.Text, 1)
MSComm1.Settings = "9600,N,8,1" 'Combo2.Text & strJ & Combo4 & Combo5 '波特率9600,N为奇校验,8个数据位,1个停止位
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0 '读取整个接收缓冲区内码或消除
If MSComm1.PortOpen = False Then '端口未打开
MSComm1.PortOpen = True
Pic.BackColor = vbRed
'Pic.Picture = LoadPicture(App.Path & "\red.jpg")
End If
Else
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
cmdOpen.Caption = "打 开"
Pic.BackColor = vbBlue
'Pic.Picture = LoadPicture(App.Path & "\blue.jpg")
End If
Exit Sub
errHand:
Pic.BackColor = vbBlue
'Pic.Picture = LoadPicture(App.Path & "\blue.jpg")
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
MsgBox "可能的错误原因是:" + Err.Description
End Sub
Private Sub cmdF_Click() '发送
' Dim lngLen As Long
Dim strText As String
Dim bytGet() As Byte
Dim bytG(10000) As Byte
On Error GoTo errHand
If MSComm1.PortOpen = False Then '端口未打开
MSComm1.PortOpen = True
End If
intNumI = 0
txtRec.Text = ""
'statBar.Panels.Item(1) = "0 字节"
If txtSend = "" Then
Exit Sub
End If
strText = Replace(txtSend.Text, " ", "")
Dim i As Integer
For i = 1 To Len(strText) Step 2
bytS1(1) = "&H" & Mid(strText, i, 2)
' Debug.Print Mid(strText, i, 2)
MSComm1.Output = bytS1
Next
MSComm1.InBufferCount = 0 '清除发送缓冲区
MSComm1.OutBufferCount = 0 '清除接收缓冲区
'TimeDelay 200'080801
MSComm1.RThreshold = 1
Timer1.Enabled = True
Exit Sub
errHand:
MsgBox Err.Description
End Sub
Private Sub MSComm1_OnComm()
Dim bytGet() As Byte
Dim intDateLen As Long '数据块长度
Select Case MSComm1.CommEvent
Case comEvReceive
Timer1.Enabled = False
bytGet = MSComm1.Input
For i = 0 To UBound(bytGet)
If Len(Hex(bytGet(i))) = 1 Then
txtRec.Text = txtRec.Text + "0" + Hex(bytGet(i)) ' + " "
Else
txtRec.Text = txtRec.Text + Hex(bytGet(i)) '+ " "
End If
Next
End Select
Exit Sub
End Sub
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim strJ As String '校验位
Dim bytS1(1 To 1) As Byte, bytS2(1 To 1) As Byte, bytS3(1 To 1) As Byte, bytS4(1 To 1) As Byte, bytS5(1 To 1) As Byte, bytS6(1 To 1) As Byte, bytS7(1 To 1) As Byte, bytS8(1 To 1) As Byte, bytS9(1 To 1) As Byte, bytS10(1 To 1) As Byte, bytS11(1 To 1) As Byte, bytS12(1 To 1) As Byte, bytS13(1 To 1) As Byte, bytS14(1 To 1) As Byte, bytS15(1 To 1) As Byte, bytS16(1 To 1) As Byte, bytS17(1 To 1) As Byte, bytS18(1 To 1) As Byte, bytS19(1 To 1) As Byte, bytS20(1 To 1) As Byte '发送字节
Dim bytS21(1 To 1) As Byte, bytS22(1 To 1) As Byte, bytS23(1 To 1) As Byte, bytS24(1 To 1) As Byte, bytS25(1 To 1) As Byte, bytS26(1 To 1) As Byte, bytS27(1 To 1) As Byte, bytS28(1 To 1) As Byte, bytS29(1 To 1) As Byte, bytS30(1 To 1) As Byte, bytS31(1 To 1) As Byte, bytS32(1 To 1) As Byte, bytS33(1 To 1) As Byte, bytS34(1 To 1) As Byte, bytS35(1 To 1) As Byte, bytS36(1 To 1) As Byte, bytS37(1 To 1) As Byte, bytS38(1 To 1) As Byte, bytS39(1 To 1) As Byte, bytS40(1 To 1) As Byte '发送字节
Dim intTim As Integer
Dim bytGet() As Byte
Dim bytGetAll(0 To 100000) As Long 'byte
Dim intNumI As Integer
Dim i As Integer
Private Sub Form_Load()
On Error GoTo errHand
Timer1.Enabled = False
Select Case Combo3.Text
Case "无NONE"
strJ = "N"
Case "奇ODD"
strJ = "O"
Case "偶EVEN"
strJ = "E"
End Select
intTim = 1
If cmdOpen.Caption = "打 开" Then
cmdOpen.Caption = "关 闭"
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
MSComm1.CommPort = Right(Combo1.Text, 1)
MSComm1.Settings = "9600,N,8,1" 'Combo2.Text & strJ & Combo4 & Combo5 '波特率9600,N为奇校验,8个数据位,1个停止位
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0 '读取整个接收缓冲区内码或消除
If MSComm1.PortOpen = False Then '端口未打开
MSComm1.PortOpen = True
Pic.BackColor = vbRed
'Pic.Picture = LoadPicture(App.Path & "\red.jpg")
End If
Else
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
cmdOpen.Caption = "打 开"
Pic.BackColor = vbBlue
'Pic.Picture = LoadPicture(App.Path & "\blue.jpg")
End If
Exit Sub
errHand:
MsgBox Err.Description
End Sub
Private Sub cmdEmp_Click()
txtSend.Text = ""
End Sub
Private Sub cmdREmp_Click() '清空接收区
txtRec.Text = ""
End Sub
Private Sub Combo3_Click() '校验
On Error GoTo errHand
Select Case Combo3.Text
Case "无NONE"
strJ = "N"
Case "奇ODD"
strJ = "O"
Case "偶EVEN"
strJ = "E"
End Select
Exit Sub
errHand:
MsgBox "可能的错误原因是:" + Err.Description
End Sub
Private Sub cmdOpen_Click() '打开
On Error GoTo errHand
If cmdOpen.Caption = "打 开" Then
cmdOpen.Caption = "关 闭"
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
MSComm1.CommPort = Right(Combo1.Text, 1)
MSComm1.Settings = "9600,N,8,1" 'Combo2.Text & strJ & Combo4 & Combo5 '波特率9600,N为奇校验,8个数据位,1个停止位
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0 '读取整个接收缓冲区内码或消除
If MSComm1.PortOpen = False Then '端口未打开
MSComm1.PortOpen = True
Pic.BackColor = vbRed
'Pic.Picture = LoadPicture(App.Path & "\red.jpg")
End If
Else
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
cmdOpen.Caption = "打 开"
Pic.BackColor = vbBlue
'Pic.Picture = LoadPicture(App.Path & "\blue.jpg")
End If
Exit Sub
errHand:
Pic.BackColor = vbBlue
'Pic.Picture = LoadPicture(App.Path & "\blue.jpg")
If MSComm1.PortOpen = True Then '端口未打开
MSComm1.PortOpen = False
End If
MsgBox "可能的错误原因是:" + Err.Description
End Sub
Private Sub cmdF_Click() '发送
' Dim lngLen As Long
Dim strText As String
Dim bytGet() As Byte
Dim bytG(10000) As Byte
On Error GoTo errHand
If MSComm1.PortOpen = False Then '端口未打开
MSComm1.PortOpen = True
End If
intNumI = 0
txtRec.Text = ""
'statBar.Panels.Item(1) = "0 字节"
If txtSend = "" Then
Exit Sub
End If
strText = Replace(txtSend.Text, " ", "")
Dim i As Integer
For i = 1 To Len(strText) Step 2
bytS1(1) = "&H" & Mid(strText, i, 2)
' Debug.Print Mid(strText, i, 2)
MSComm1.Output = bytS1
Next
MSComm1.InBufferCount = 0 '清除发送缓冲区
MSComm1.OutBufferCount = 0 '清除接收缓冲区
'TimeDelay 200'080801
MSComm1.RThreshold = 1
Timer1.Enabled = True
Exit Sub
errHand:
MsgBox Err.Description
End Sub
Private Sub MSComm1_OnComm()
Dim intDateLen As Long '数据块长度
On Error GoTo errHand
Select Case MSComm1.CommEvent
Case comEvReceive
Timer1.Enabled = False
bytGet = MSComm1.Input
For i = 0 To UBound(bytGet)
bytGetAll(intNumI + i) = bytGet(i)
Next
intNumI = intNumI + UBound(bytGet) + 1
'statBar.Panels.Item(1) = CStr(intNumI) & " 字节"'080801
Timer1.Enabled = True
End Select
Exit Sub
errHand:
MsgBox "可能的错误描述:" + Err.Description
End Sub
Private Sub Timer1_Timer()
'On Error GoTo errHand
Timer1.Enabled = False
Dim i As Integer
For i = 0 To intNumI - 1
If Len(Hex(bytGetAll(i))) = 1 Then
txtRec.Text = txtRec.Text + "0" + Hex(bytGetAll(i)) ' + " "
Else
txtRec.Text = txtRec.Text + Hex(bytGetAll(i)) '+ " "
End If
'DoEvents'080801
Next
'txtRec.SelStart = Len(txtRec)
'txtRec.SelLength = 0
'txtRec.SetFocus
'Exit Sub
'errHand:
' MsgBox "可能的错误描述:" + Err.Description
End Sub