VB串口传数据,0不知道怎么跑来的程序如下:
Private Sub openclosecmd_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
openclosecmd.Caption = "打开串口"
'open_close.Caption = "打开串口"
Label3.ForeColor = RGB(0, 0, 0)
Label3.Caption = "已关闭"
Shape1.FillColor = RGB(255, 255, 0)
Shape1.FillStyle = 0
Exit Sub
End If
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
openclosecmd.Caption = "关闭串口"
'open_close.Caption = "关闭串口"
Label3.ForeColor = RGB(255, 0, 0)
Label3.Caption = "已开启"
Shape1.FillColor = RGB(255, 0, 0)
Shape1.FillStyle = 0
Exit Sub
End If
End Sub
Private Sub Command3_Click(Index As Integer)
Dim sendData1(1) As Byte
Dim X As String
Select Case Index
Case 0
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H1
MSComm1.Output = sendData1
Case 1
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H2
MSComm1.Output = sendData1
Case 2
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H3
MSComm1.Output = sendData1
Case 3
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H4
MSComm1.Output = sendData1
Case 4
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H5
MSComm1.Output = sendData1
Case 5
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H6
MSComm1.Output = sendData1
Case 6
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H7
MSComm1.Output = sendData1
Case 7
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H8
MSComm1.Output = sendData1
Case 8
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H9
MSComm1.Output = sendData1
Case 9
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &HA
MSComm1.Output = sendData1
Case 10
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &HB
MSComm1.Output = sendData1
Case 11
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &HC
MSComm1.Output = sendData1
Case 12
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &HD
MSComm1.Output = sendData1
Case 13
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &HE
MSComm1.Output = sendData1
Case 14
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &HF
MSComm1.Output = sendData1
Case 15
If MSComm1.PortOpen = False Then
X = MsgBox("串口未打开,请打开串口", 16)
Exit Sub
End If
MSComm1.OutBufferCount = 0
sendData1(1) = &H10
MSComm1.Output = sendData1
End Select
End Sub
Private Sub Form_Load()
Dim sendData1(1) As Byte
Shape1.FillStyle = 0
Shape1.FillColor = RGB(255, 255, 0)
MSComm1.CommPort = 1 '设置串口为com4
MSComm1.Settings = "9600,n,8,1" '通讯参数 波特率 奇偶校验 数据位 停止位
' MSComm1.InputMode = comInputModeBinary '二进制接收
'MSComm1.PortOpen = True '打开串口
'MSComm1.InBufferCount = 0 '清空接收缓冲区
'MSComm1.RThreshold = 1 '缓冲区中接收到一个字符,就产生一次OnComm事件
MSComm1.OutBufferSize = 1
End Sub |