VB 6.0 MSCOMM串口通讯的问题
我想用VB开发一个软件,来控制我们的设备,设备接COM1口一下是设备自带的说明:设备说明:串口类型:RS-232波特率:9600命令切换1屏:010208F6命令切换2屏...
我想用VB开发一个软件,来控制我们的设备,设备接COM1口一下是设备自带的说明:
设备说明:串口类型:RS-232 波特率:9600 命令切换1屏:01 02 08 F6
命令切换2屏:01 02 06 f8 获取状态信息:01 02 02 FC(命令为十六进制)
麻烦高手帮我解答一下如何连接,如何发送指令,如何接受信息,万分感谢!
我以前开发过一个类似功能的软件,但是设备不一样!我用相同的方法,试过很多此,设备一点反应都没有!!!
zdingyun 你好!我按照你的方法试了,可是设备还是没有什么反应!麻烦你想一下还有其他的可能性吗?或者你把邮箱留给我,我把完整的说明给你发过去! 展开
设备说明:串口类型:RS-232 波特率:9600 命令切换1屏:01 02 08 F6
命令切换2屏:01 02 06 f8 获取状态信息:01 02 02 FC(命令为十六进制)
麻烦高手帮我解答一下如何连接,如何发送指令,如何接受信息,万分感谢!
我以前开发过一个类似功能的软件,但是设备不一样!我用相同的方法,试过很多此,设备一点反应都没有!!!
zdingyun 你好!我按照你的方法试了,可是设备还是没有什么反应!麻烦你想一下还有其他的可能性吗?或者你把邮箱留给我,我把完整的说明给你发过去! 展开
3个回答
展开全部
麻烦高手帮我解答一下如何连接:
使用RS232口连接,电脑的RS232口与下位机的RS232口谅解方式:
2-3
3-2
交叉连接
各自口的
4-6
7-8
短接
你发送须按BYTE数组执行,代码如下供参考:
根据你发的协议询问命令有修改
Option Explicit
Dim strData As String
Private Sub Command1_Click() '采用二进制发送
Dim strSj(3) As Byte
strSj(0) = &H1
strSj(1) = &H2
strSj(2) = &H2
strSj(3) = 256 -(strSj(1) + strSj(2)) Mod 256
MSComm1.Output = strSj
End Sub
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1" '暂定无校验,数据位8位,停止位1位
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim BytReceived() As Byte
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
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
Text1 = strData
'按通信协议写接收数据处理代码
End Select
End Sub
问题补充:请发消息给我。
使用RS232口连接,电脑的RS232口与下位机的RS232口谅解方式:
2-3
3-2
交叉连接
各自口的
4-6
7-8
短接
你发送须按BYTE数组执行,代码如下供参考:
根据你发的协议询问命令有修改
Option Explicit
Dim strData As String
Private Sub Command1_Click() '采用二进制发送
Dim strSj(3) As Byte
strSj(0) = &H1
strSj(1) = &H2
strSj(2) = &H2
strSj(3) = 256 -(strSj(1) + strSj(2)) Mod 256
MSComm1.Output = strSj
End Sub
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1" '暂定无校验,数据位8位,停止位1位
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim BytReceived() As Byte
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
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
Text1 = strData
'按通信协议写接收数据处理代码
End Select
End Sub
问题补充:请发消息给我。
展开全部
有个朋友到是这方面的高手,需要的话帮你问下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是一个向仪表发送指令的程序,通过串口控制仪表的 通过mscomm发送的, 我刚写完的,你试试看应该好用,在我电脑上调试过的,
Option Explicit
Dim byteInput() As Byte
Dim strdata, c, a() As String
Dim BytReceived() As Byte
Dim strBuff, b As String
Dim j As Integer, e As String
Private Sub Command1_Click()
'MSComm1.OutBufferCount = 0 '...清空输出寄存器
Dim a(0 To 3) As Byte
a(0) = &H1
a(1) = &H2
a(2) = &H6
a(3) = &f8
MSComm1.Output = a
End Sub
Private Sub Form_Load() '窗体载入事件
MSComm1.CommPort = 1 '...使用Com1口
MSComm1.InBufferSize = 1024 '"设置接收缓冲区的字节长度
MSComm1.InputMode = 1 '"输入模式为binary
MSComm1.RThreshold = 1 '"控件收到数据时将触发OnComm事件
MSComm1.Settings = "4800,n,8,1" '...设置通讯参数
MSComm1.InBufferSize = 1024 '"设置接收缓冲区的字节长度
' MSComm1.PortOpen = True '...打开串口
MSComm1.InputLen = 1 '"读取input接收缓冲区全部字节
End Sub
Private Sub MSComm1_OnComm() '有数据传送时触发
Dim strBuff, b As String
Dim j As Integer, e As String
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive '...有接受事件发生
'indata = MSComm1.Input
'...注意!要通过MSComm控件发送或接收二进制数据必须用Variant类型的变量对二进
'...制Byte类型的变量进行转换!
'bte = AscB(indata)
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
For i = 1 To Len(strdata) Step 2
b = b & Mid(strdata, i, 2) & " "
Next
c = Trim(b)
a = Split(c)
For j = LBound(a) To UBound(a)
e = e & Chr(CDec("&H" & a(j)))
Next
Text1.Text = vbCrLf & e
'Text1.Text = e
MSComm1.InBufferCount = 0 ' 清空接收缓冲区
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
Option Explicit
Dim byteInput() As Byte
Dim strdata, c, a() As String
Dim BytReceived() As Byte
Dim strBuff, b As String
Dim j As Integer, e As String
Private Sub Command1_Click()
'MSComm1.OutBufferCount = 0 '...清空输出寄存器
Dim a(0 To 3) As Byte
a(0) = &H1
a(1) = &H2
a(2) = &H6
a(3) = &f8
MSComm1.Output = a
End Sub
Private Sub Form_Load() '窗体载入事件
MSComm1.CommPort = 1 '...使用Com1口
MSComm1.InBufferSize = 1024 '"设置接收缓冲区的字节长度
MSComm1.InputMode = 1 '"输入模式为binary
MSComm1.RThreshold = 1 '"控件收到数据时将触发OnComm事件
MSComm1.Settings = "4800,n,8,1" '...设置通讯参数
MSComm1.InBufferSize = 1024 '"设置接收缓冲区的字节长度
' MSComm1.PortOpen = True '...打开串口
MSComm1.InputLen = 1 '"读取input接收缓冲区全部字节
End Sub
Private Sub MSComm1_OnComm() '有数据传送时触发
Dim strBuff, b As String
Dim j As Integer, e As String
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive '...有接受事件发生
'indata = MSComm1.Input
'...注意!要通过MSComm控件发送或接收二进制数据必须用Variant类型的变量对二进
'...制Byte类型的变量进行转换!
'bte = AscB(indata)
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
For i = 1 To Len(strdata) Step 2
b = b & Mid(strdata, i, 2) & " "
Next
c = Trim(b)
a = Split(c)
For j = LBound(a) To UBound(a)
e = e & Chr(CDec("&H" & a(j)))
Next
Text1.Text = vbCrLf & e
'Text1.Text = e
MSComm1.InBufferCount = 0 ' 清空接收缓冲区
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询