End Sub'往串口发送数据 Private Sub DownloadData(ByRef bytData() As Byte) On Error Resume Next MSComm1.Output = bytData '发送数据 End Sub'自动接收串口返回数据 Private Sub MSComm1_OnComm() On Error Resume Next Dim BytReceived() As Byte Dim strBuff As Variant Dim i As Integer
Select Case MSComm1.CommEvent Case 2 If m_bln = True Then MSComm1.InputLen = 0 strBuff = MSComm1.Input '必须用 Variant 变量接收二进制值 '否则中文系统将其改为“?”= Chr(&H3F) BytReceived() = strBuff For i = 0 To UBound(BytReceived) m_intCount = m_intCount + 1 m_strResult(i) = Right("0" & Hex(BytReceived(i)), 2) & " " Next Else MSComm1.InputLen = 0 strBuff = MSComm1.Input '必须用 Variant 变量接收二进制值 '否则中文系统将其改为“?”= Chr(&H3F) BytReceived() = strBuff End Select End Sub
新建VB工程,由工程菜单选部件项,在Microsoft Comm Control 6.0部件前打钩确定,VB的工具箱中就有MSCOMM控件,添加到窗体.下列代码可按2进制接收数据,处理为16进制字符串显示数据. 是否能用于条码枪扫描未试过.Option Explicit Dim inData As String Dim a() As Byte Private Sub Form_Load() MSComm1.Settings = "9600,n,8,1" MSComm1.RThreshold = 1 MSComm1.InputMode = comInputModeBinary '二进制接收 MSComm1.PortOpen = True End Sub Private Sub MsComm1_OnComm() Dim intInputLen As Integer Dim i As Integer Select Case MSComm1.CommEvent Case comEvReceive '此处添加处理接收的代码 intInputLen = MSComm1.InBufferCount ReDim a(intInputLen) a = MSComm1.Input For i = 0 To UBound(a) If Len(Hex(a(i))) = 1 Then inData = inData & "0" & Hex(a(i)) Else inData = inData & Hex(a(i)) End If Next Text2 = inData End Select End Sub
5F 的已给了你正确的答案了, 你还要再转换, 转换后就是你要的数据了Dim aa$, bb$ Private Sub Form_Load() aa = "393738373330323038333133390A" '0A得到Chr(10)就是换行 bb = "" For i = 1 To Len(aa) Step 2 bb = bb & Chr(Val("&H" & Mid(aa, i, 2))) Next i MsgBox bb End Sub
1.你试着以下面代码改一下测试你的数据(因你的数据看起来应该是以文字TextMode方式接收) 2. .RThreshold = 14 我算一下你每一笔的长度是14位 3.数据有重复,那是因为你有时会接收不全,你要以长度或某个位置"应该"是什么来做判断,再决定要不要这笔数据. 4.以下图来说,我的汽车衡,每笔12位数,第1字符是chr(2)为启始符STX,第2--8字符共7个字符是我要的重量,第9--11字符是序号我也不要, 第12位就是我的ETX终止符chr(3),你的是chr(10), 所以我也不要,你看我的 Me.Caption就知道了 '添加 Command1 List1 Mscomm1Option Explicit Dim RcvData$ Private Sub Form_Load() With MSComm1 .CommPort = 1 .Settings = "9600,n,8,1" .InputLen = 0 .InBufferSize = 1 .RThreshold = 14 .InputMode = comInputModeText End With Command1.Caption = "开始接收" Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2 End SubPrivate Sub Form_Unload(Cancel As Integer) If MSComm1.PortOpen Then MSComm1.PortOpen = False End End SubPrivate Sub Command1_Click() If Command1.Caption = "开始接收" Then List1.Clear If Not MSComm1.PortOpen Then MSComm1.PortOpen = True MSComm1.InBufferCount = 0 MSComm1.DTREnable = True Else If MSComm1.PortOpen Then MSComm1.PortOpen = False MSComm1.DTREnable = False End If Command1.Caption = IIf(Command1.Caption = "开始接收", "停 止", "开始接收") End SubPrivate Static Sub MSComm1_OnComm() Select Case MSComm1.CommEvent Case comEvReceive '接收缓冲区收到Rthreshold 个字符时触发 RcvData = MSComm1.Input 'If len(RcvData)=14 Then '你自己试看 'If Mid(RcvData, 14, 1) = Chr(10) Then '你自己试看 List1.AddItem RcvData List1.Selected(List1.ListCount - 1) = True 'Me.Caption = CStr(Val(Mid(RcvData, 2, 7))) & " Kg" 'End If 'End If End Select End Sub 效果图: http://p.blog.csdn.net/images/p_blog_csdn_net/cbm666/366646/o_RS232.jpg
在窗体上拖放一个MSComm控件,选中点右键,点属性,里面可以设置需要的传输的波特率,数据位,停止位,校验,打开那个串口等。也可以用其CommPort (设置并返回通讯端口号),Settings (以字符串的形式设置并返回波特率、奇偶校验、数据位、停止位)。
在窗体上方一个Timer控件,设置好Timer控件属性后可以这样使用
Private Sub Timer1_Timer()
If MSComm1.InBufferCount >= 10 Then ‘10代表缓冲区存储的长度
TEXT1.Text = MSComm1.Input ’将缓冲区中的数据读入到TEXT1.Text
endif
End Sub可以试试这样使用,这个是最基本的应用,自己得完善一下,比如先检测下,数据是否异常等等。
实例的话MSDN本身带有几个实例.用处都很大.
如果涉及到多协议会比较麻烦点.
如果单片机是一次返回一个字节就不用修改.
程序是自动接收串口返回数据使用方法:
先初始化Call InitMscomm(1)'1代码串口1
然后发送数据Call DownloadData(这里一个byte数组)
接收数据是自动的.你只要看着TEXT1有变化就行了
通信协使用的9600,M,8,2'初始化MSCOMM控件
Public Sub InitMscomm(ByVal intCom As Integer) MSComm1.CommPort = intCom
MSComm1.Settings = "9600,M,8,2"
MSComm1.InputMode = comInputModeBinary
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.SThreshold = 1
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub'往串口发送数据
Private Sub DownloadData(ByRef bytData() As Byte)
On Error Resume Next
MSComm1.Output = bytData '发送数据
End Sub'自动接收串口返回数据
Private Sub MSComm1_OnComm()
On Error Resume Next
Dim BytReceived() As Byte
Dim strBuff As Variant
Dim i As Integer
Select Case MSComm1.CommEvent
Case 2
If m_bln = True Then
MSComm1.InputLen = 0
strBuff = MSComm1.Input '必须用 Variant 变量接收二进制值
'否则中文系统将其改为“?”= Chr(&H3F)
BytReceived() = strBuff
For i = 0 To UBound(BytReceived)
m_intCount = m_intCount + 1
m_strResult(i) = Right("0" & Hex(BytReceived(i)), 2) & " "
Next
Else
MSComm1.InputLen = 0
strBuff = MSComm1.Input '必须用 Variant 变量接收二进制值
'否则中文系统将其改为“?”= Chr(&H3F)
BytReceived() = strBuff
End Select
End Sub
Dim inData As String
Dim a() As Byte Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.InputMode = comInputModeBinary '二进制接收
MSComm1.PortOpen = True
End Sub Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
'此处添加处理接收的代码
intInputLen = MSComm1.InBufferCount
ReDim a(intInputLen)
a = MSComm1.Input
For i = 0 To UBound(a)
If Len(Hex(a(i))) = 1 Then
inData = inData & "0" & Hex(a(i))
Else
inData = inData & Hex(a(i))
End If
Next
Text2 = inData
End Select
End Sub
Private Sub Form_Load()
aa = "393738373330323038333133390A" '0A得到Chr(10)就是换行
bb = ""
For i = 1 To Len(aa) Step 2
bb = bb & Chr(Val("&H" & Mid(aa, i, 2)))
Next i
MsgBox bb
End Sub
2. .RThreshold = 14 我算一下你每一笔的长度是14位
3.数据有重复,那是因为你有时会接收不全,你要以长度或某个位置"应该"是什么来做判断,再决定要不要这笔数据.
4.以下图来说,我的汽车衡,每笔12位数,第1字符是chr(2)为启始符STX,第2--8字符共7个字符是我要的重量,第9--11字符是序号我也不要, 第12位就是我的ETX终止符chr(3),你的是chr(10), 所以我也不要,你看我的 Me.Caption就知道了
'添加 Command1 List1 Mscomm1Option Explicit
Dim RcvData$
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InputLen = 0
.InBufferSize = 1
.RThreshold = 14
.InputMode = comInputModeText
End With
Command1.Caption = "开始接收"
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
End SubPrivate Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then MSComm1.PortOpen = False
End
End SubPrivate Sub Command1_Click()
If Command1.Caption = "开始接收" Then
List1.Clear
If Not MSComm1.PortOpen Then MSComm1.PortOpen = True
MSComm1.InBufferCount = 0
MSComm1.DTREnable = True
Else
If MSComm1.PortOpen Then MSComm1.PortOpen = False
MSComm1.DTREnable = False
End If
Command1.Caption = IIf(Command1.Caption = "开始接收", "停 止", "开始接收")
End SubPrivate Static Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive '接收缓冲区收到Rthreshold 个字符时触发
RcvData = MSComm1.Input
'If len(RcvData)=14 Then '你自己试看
'If Mid(RcvData, 14, 1) = Chr(10) Then '你自己试看
List1.AddItem RcvData
List1.Selected(List1.ListCount - 1) = True
'Me.Caption = CStr(Val(Mid(RcvData, 2, 7))) & " Kg"
'End If
'End If
End Select
End Sub
效果图:
http://p.blog.csdn.net/images/p_blog_csdn_net/cbm666/366646/o_RS232.jpg
.InputMode = comInputModeBinary右边是以Text模式接收, 你可以看到 2开始 3 结束
.InputMode = comInputModeText