您的位置:控制工程论坛网论坛 » 嵌入式系统 » vb通过rs-232串口进行通信--接收处理

wj321

wj321   |   当前状态:离线

总积分:1211  2025年可用积分:0

注册时间: 2003-05-12

最后登录时间: 2009-11-05

空间 发短消息加为好友

vb通过rs-232串口进行通信--接收处理

wj321  发表于 2008/11/13 20:46:36      913 查看 0 回复  [上一主题]  [下一主题]

手机阅读

vb通过rs-232串口进行通信--接收处理
2008-07-08 16:15

Option Explicit
Dim sTemp As String
Dim yTemp() As Byte
Dim Hsum As Integer
Dim strData As String
Dim intSj() As Integer
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary '二进读取制模式
MSComm1.RThreshold = 1
MSComm1.SThreshold = 0
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
Dim ss As Variant
Dim ww As String '定义临时变量
Dim i, a As Integer
Select Case MSComm1.CommEvent 'Select Case 语句分解事件
Case comEvReceive
ss = MSComm1.Input
sTemp = StrConv(ss, vbUnicode) '转换为Unicode
yTemp = StrConv(sTemp, vbFromUnicode) '转换为Byte
Hsum = UBound(yTemp)
Dim j As Integer
For j = 0 To Hsum '转换为16进制
If Len(Hex(yTemp(j))) = 1 Then
strData = strData & "0" & Hex(yTemp(j))
Else
strData = strData & Hex(yTemp(j))
End If
Next
zh '转换为Integer
End Select
End Sub

Public Sub zh()
Dim l As Integer
ReDim intSj((Len(strData) / 4) - 1)
For l = 1 To Len(strData) Step 4
intSj((l - 1) / 4) = Val("&H" & Mid(strData, l, 4))
Print intSj(l / 4)
Next l
End Sub

1楼 0 0 回复