Private Sub Command1_Click()
End
End Sub
Private Sub Command2_Click()
Winsockserver.SendData Text4.Text + ":" + textsend.Text
textget.Text = textget.Text + vbCrLf + Text4.Text + ":" + textsend.Text + " " + Str(Time)
textsend.Text = ""
End Sub
Private Sub Form_Load()
Command2.Visible = False
textsend.Visible = False
Text4.Visible = False
textget.Visible = False
Label1.Visible = False
Winsockserver.LocalPort = 1001
Winsockserver.Listen
End Sub
Private Sub textsend_Change()
'Winsockserver.SendData textsend.text
End Sub
Private Sub textsend_KeyPress(KeyAscii As Integer)
'If KeyAscii = 13 Then
'Winsockserver.SendData textsend.text
'textsend.text = ""
If KeyAscii = 13 Then
Winsockserver.SendData textsend.Text
' Text1.text = Text1.text + vbCrLf + Text4.text + ": " + Text2.text + " " + Str(Time)
textsend.Text = ""
End If
End Sub
Private Sub Timer1_Timer()
If Text4.Text = "" Then Text4.Text = "no name"
End Sub
Private Sub Winsockserver_Close()
Winsockserver.Close
End
End Sub
Private Sub Winsockserver_ConnectionRequest(ByVal requestID As Long)
Command2.Visible = True
textsend.Visible = True
textget.Visible = True
Text4.Visible = True
Label1.Visible = True
If Winsockserver.State <>sckClosed Then Winsockserver.Close
Winsockserver.Accept requestID
End Sub
Private Sub Winsockserver_DataArrival(ByVal bytesTotal As Long)
Dim tmpstr As String
Winsockserver.GetData tmpstr
'textget.text = textget + tmpstr + textsend
textget.Text = textget.Text + vbCrLf + tmpstr + " " + Str(Time)
End Sub
'(1)Command1:退出按钮;
'(2)textsend:发送数据文本框;
'(3)Winsockserver: 服务器Winsock;
'(4)textget :接收数据文本框。
一个clien
我也是自己摸索开发出来的每一款VBtoCOM通讯,有具体的思路如下:
给你推荐一个工具“com串口测试工具 ComTone V1.0 中文绿色版”
1、打开你的噪音计的测试软件,调整好串口号、通讯频率等等,我用温度计举例说明。
开始查询后有返回数值,这个Receive:就是返回的数值000304012200004b05,
打开串口监视精灵,监视软件的com口事件:
这里面的Write是测试软件发出的查询指令,read是设备返回的数据指令
Private Sub Command1_Click() '发送指令If Not MSComm1.PortOpen Then
MSComm1.CommPort = 7 '串口为7
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InputMode = comInputModeBinary '二进制接收
MSComm1.InputLen = 0 '读取接收缓冲区的所有字符
MSComm1.PortOpen = True '打开串口
MSComm1.RTSEnable = False '置为发送状态
End If
Dim pu() As Byte
Dim strdata As String
Dim crc_js() As Byte
ReDim pu(7) '这个数组是8位的查询指令
pu(0) = "&H00" '温度计地址
pu(1) = "&H03" '查询指令
pu(2) = "&H00" '2、3为温度计地址
pu(3) = "&H00" '2、3为温度计地址
pu(4) = "&H00" '4、5为读取寄存器长度
pu(5) = "&H02" '4、5为读取寄存器长度
pu(6) = "&HC5" '6、7为CRC校验码 因为我们不知道设备的CRC校验规则所以用测试软件产生的校验码
pu(7) = "&HDA" '6、7为CRC校验码
MSComm1.Output = pu
'不做任何事情,仅仅允许其它应用程序处理它们的事件。
DoEvents
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.RThreshold = 9 '所要接收的数据长度,我们通过COM检测精灵看到了回传数据一共是9字节
MSComm1.RTSEnable = True '转为接收状态
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then
MSComm1.PortOpen = False '关闭串口
End If
Timer1.Enabled = False
End Sub
Private Sub MSComm1_OnComm() 'COM事件
Dim PA() As Byte
Dim PB As String
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0 '读取接收缓冲区的所有字符
PB = MSComm1.Input
PA() = PB
For i = 0 To UBound(PA())
'Print "PA(" & i & ")" PA(i)
If Len(Hex(PA(i))) = 1 Then
strdata = strdata & "0" & Hex(PA(i))
Else
strdata = strdata & Hex(PA(i))
End If
Next
'回传的数据串:000304012600000AC4,这9字节根据通讯协议我们进行拆分
'00为协议内回传机号,03为读命令,04为返回的数据长度 0126 为我要的温度数据为十六进制表达,下面我进行数据处理
wd = CLng("&H" & Left(Right(strdata, 12), 4)) / 10 & "℃" '根据通讯协议换算成温度
Text1 = Text1 & vbCrLf & strdata & " " & wd
strdata = ""
MSComm1.PortOpen = False '关闭串口
End Select
End Sub
Private Sub Timer1_Timer()
Call Command1_Click
End Sub
这是我的测试结果。
下面是通讯协议
这个是我的软件用检测精灵检测的结果
串口接收
可以放在MScomm控件的
OnComm
事件里
推荐看书
Visual_Basic与_RS-232_串行通信控制
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)