发表于:2006/2/26 16:52:00
#0楼
求助一个关于VB开发串口通讯的问题!十万火急,请大家能够帮忙一下,小弟感激不尽!
最近,我跟导师开发一三维声源定位系统,采用VB编写PC机串口通信程序.实现与单片机的双向数据交换.目前出现了一些问题:我先把源码贴上:
Private Sub cmdClear_Click()//清除串口
Dim bytTemp(0) As Byte
ReDim bytReceiveByte(0)
intReceiveLen = 0
Call InputManage(bytTemp, 0)
Call GetDisplayText
Text1.Text = ""使接受显示字符串的文本框置空
End Sub
Private Sub cmdReceive_Click()//接受串口数据
frmmain.txtHexEditASCII.Text = ""
If blnReceiveFlag Then
If Not blnAutoSendFlag And Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
frmmain.cmdReceive.Caption = "开始接收"
Else
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
frmmain.ctrMSComm.InputLen = 0
frmmain.ctrMSComm.InputMode = 0
frmmain.ctrMSComm.InBufferCount = 0 '注释:一按这个接受按钮才对数据预存区进行清空
frmmain.ctrMSComm.RThreshold = 1
frmmain.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
Private Sub cmdSetting_Click()//串口设置
frmSetting.Show
frmSetting.txtPort.Text = str(intPort)
frmSetting.txtSetting.Text = strSet
frmSetting.txtTime.Text = str(intTime)
End Sub
Private Sub Command3_Click()//退出
End
End Sub
Private Sub ctrMSComm_OnComm()//On_Comm事件过程
Dim bytInput() As Byte
Dim intInputLen As Integer
frmmain.txtHexEditASCII.Text = ""
Select Case frmmain.ctrMSComm.CommEvent
Case comEvReceive
If blnReceiveFlag Then
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
'处理接收的代码
frmmain.ctrMSComm.InputMode = comInputModeBinary
intInputLen = frmmain.ctrMSComm.InBufferCount
frmmain.ctrMSComm.OutBufferCount = 0
ReDim bytInput(intInputLen)
'延时
Dim l As Long
l = Timer
While Abs(Timer - l) < 0.7 '延时0.1秒,用abs防止午夜时刻出错
DoEvents
Wend
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
bytInput = frmmain.ctrMSComm.Input
'frmmain.ctrMSComm.PortOpen = True
frmmain.ctrMSComm.InBufferCount = 0
Call InputManage(bytInput, intInputLen)
Call GetDisplayText
If Not blnAutoSendFlag And Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
End If
End Select
frmmain.ctrMSComm.InBufferCount = 0
'frmmain.txtHexEditASCII.Text = ""
' Call cmdClear_Click
'Call Form_Load
End Sub
'接收到字符后,与原数据保存在一起
'输入处理
'bytReceiveByte()
Public Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
On Error GoTo errorsetting
Dim n As Integer
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
'ReDim Preserve bytReceiveByte(intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
'bytReceiveByte(n - 1) = bytInput(n - 1)
Next n
errorsetting:
frmmain.ctrMSComm.InBufferCount = 0
intReceiveLen = intReceiveLen + intInputLenth
'intReceiveLen = intInputLenth
'frmmain.ctrMSComm.PortOpen = False
End Sub
Public Sub GetDisplayText()
Dim n, m As Double
Dim intValue As Double
Dim intHighHex As Double
Dim intLowHex As Double
Dim strSingleChr, s1, s As String
Dim strDisplayHex As String
Dim strDisplayASCII As String
strDisplayAddress = ""
strDisplayHex = ""
strDisplayASCII = ""
Dim intStart As Double
Dim intLenth As Double
frmmain.txtHexEditASCII.Text = ""
strASCII = ""
strHex = ""
strAddress = ""
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
strSingleChr = Chr(intValue)
strASCII = strASCII + strSingleChr
s1 = strSingleChr + s1
intHighHex = intValue 16
Text1.Text = intValue
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
frmmain.txtHexEditASCII.Text = strASCII
Next n
strASCII = ""
'读取整个字符串中的数字字符串
k = 0
n1 = 3
s = s1 '取字符串
For i = 1 To Len(s) '按顺序逐个字符判断
T = Mid(s, i, 1) '取i位置的一个字符
If T < "0" Or T > "9" Then '判是否数字
k = 0 '非数字,置k=0
Else
If k = 0 Then
n1 = n1 - 1 '指向列表框的下一行
'rece(n1) = t
If n1 >= 0 Then
Text2(n1).Text = T '存入列表框
k = 1
End If
Else '把数字加入到列表框当前行的末尾
Text2(n1).Text = Text2(n1).Text + T
End If
End If
Next i
' frmmain.txtHexEditASCII.Text = ""
'调用计算程序
Call cmdCalculate_Click
End Sub
Private Sub Form_Load()
blnAutoSendFlag = False
blnReceiveFlag = False
intReceiveLen = 0
ctrTimer.Enabled = False
TxtSend.Text = 1
intOutMode = 0
frmmain.cboHexASCII.Text = "按ASCII码"
intHexWidth = 8
intTime = 1000
intPort = 1
strSet = "300,n,8,1"
frmmain.ctrMSComm.InBufferSize = 1024
frmmain.ctrMSComm.OutBufferSize = 512
'frmmain.ctrMSComm.PortOpen = False
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = 1
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
'初始化串口
End If
'初始化显示窗口
'设置显示窗口位置尺寸
frmmain.fraHexEditBackground.Left = frmmain.TxtReceive.Left
frmmain.fraHexEditBackground.Top = frmmain.TxtReceive.Top
frmmain.fraHexEditBackground.Width = frmmain.TxtReceive.Width
frmmain.fraHexEditBackground.Height = frmmain.TxtReceive.Height
'显示初始化
'Call cmdClear_Click
End Sub
Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1
End If
ConvertHexChr = test
End Function
Private Sub receive_Click()
If blnReceiveFlag Then
If Not blnAutoSendFlag And Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
frmmain.cmdReceive.Caption = "开始接收"
Else
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
frmmain.ctrMSComm.InputLen = 0
frmmain.ctrMSComm.InputMode = 0
frmmain.ctrMSComm.InBufferCount = 0
frmmain.ctrMSComm.RThreshold = 1
frmmain.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
'发送
Private Sub cmdAutoSend_Click()
If blnAutoSendFlag Then
frmmain.ctrTimer.Enabled = False
If Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
frmmain.cmdAutoSend.Caption = "自动发送"
Else
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
frmmain.ctrTimer.Interval = intTime
frmmain.ctrTimer.Enabled = True
frmmain.cmdAutoSend.Caption = "停止发送"
End If
blnAutoSendFlag = Not blnAutoSendFlag
End Sub
'自动发送设置
Private Sub ctrTimer_Timer()
Dim longth As Integer
Dim bytSendByte()
Dim strSendText As String
strSendText = frmmain.TxtSend.Text
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
If intOutMode = 0 Then
frmmain.TxtReceive.Text = "ascii"
frmmain.ctrMSComm.Output = strSendText
Else
'longth = strHexToByteArray(strSendText, bytSendByte())
If longth > 0 Then
frmmain.ctrMSComm.Output = bytSendByte
End If
End If
End Sub
'自动发送里调用函数
Function strHexToByteArray(strText As String, bytByte() As Integer)
Dim HexData As Integer
Dim hstr As String * 1
Dim lstr As String * 1
Dim HighHexData As Integer
Dim LowHexData As Integer
Dim HexDataLen As Integer
Dim StringLen As Integer
Dim Account As Integer
strText = ""
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = ""
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = ""
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
'手动发送
Private Sub cmdManualSend_Click()
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
Call ctrTimer_Timer
If Not blnAutoSendFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
End Sub
Private Sub cboHexASCII_Click()//按ASCII显示
If frmmain.cboHexASCII.Text = "按ASCII码" Then
intOutMode = 0
Else
intOutMode = 1
End If
End Sub
Sub delay()
For i = 1 To 100
Next i
End Sub
//我们接受到的数据比如在第一次我们发送C1023a3456b9144a
给微机,我们把字符串里的数字给反序取出来分别给TEXT2(2).TEXT,TEXT2(1).TEXT,TEXT2(0).TEXT以及取第一个字符作为标志位,传到我的计算过程调用不同的算法.再把数值给计算出来.
现在已经能够进行多次连续计算.
但是如果我们第一次发C1023a3456b9144a给微机,我们调用C作为标志调用算法一计算出数值,我们再发e1023a3456b9144a,按照我们原来的思路应该是调用e为标志的算法计算出另外一组数据.现在的问题是我们第二次发过去,依然显示的是C1023a3456b9144a,调用C做为标志的算法.我们如果改动其中的数值,比如改为C2023a3456b1144a我们的程序即使连续发送也可以进行识别将数值给提取出来,但由于如果我们无法调用e为标志的算法,计算出来的数值是错误的.造成一种情况所见非所得!
注:标志字符,我们通过将传过来的数据赋给frmmain.txtHexEditASCII.Text再通过函数MID(frmmain.txtHexEditASCII.Text,1,1)j即取得首字符,也是我们的标志字符.
希望诸位大侠能够帮忙,小弟真的很急,已经熬了很久了,希望大家能够指点一二,小弟感激不尽!
最近,我跟导师开发一三维声源定位系统,采用VB编写PC机串口通信程序.实现与单片机的双向数据交换.目前出现了一些问题:我先把源码贴上:
Private Sub cmdClear_Click()//清除串口
Dim bytTemp(0) As Byte
ReDim bytReceiveByte(0)
intReceiveLen = 0
Call InputManage(bytTemp, 0)
Call GetDisplayText
Text1.Text = ""使接受显示字符串的文本框置空
End Sub
Private Sub cmdReceive_Click()//接受串口数据
frmmain.txtHexEditASCII.Text = ""
If blnReceiveFlag Then
If Not blnAutoSendFlag And Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
frmmain.cmdReceive.Caption = "开始接收"
Else
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
frmmain.ctrMSComm.InputLen = 0
frmmain.ctrMSComm.InputMode = 0
frmmain.ctrMSComm.InBufferCount = 0 '注释:一按这个接受按钮才对数据预存区进行清空
frmmain.ctrMSComm.RThreshold = 1
frmmain.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
Private Sub cmdSetting_Click()//串口设置
frmSetting.Show
frmSetting.txtPort.Text = str(intPort)
frmSetting.txtSetting.Text = strSet
frmSetting.txtTime.Text = str(intTime)
End Sub
Private Sub Command3_Click()//退出
End
End Sub
Private Sub ctrMSComm_OnComm()//On_Comm事件过程
Dim bytInput() As Byte
Dim intInputLen As Integer
frmmain.txtHexEditASCII.Text = ""
Select Case frmmain.ctrMSComm.CommEvent
Case comEvReceive
If blnReceiveFlag Then
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
'处理接收的代码
frmmain.ctrMSComm.InputMode = comInputModeBinary
intInputLen = frmmain.ctrMSComm.InBufferCount
frmmain.ctrMSComm.OutBufferCount = 0
ReDim bytInput(intInputLen)
'延时
Dim l As Long
l = Timer
While Abs(Timer - l) < 0.7 '延时0.1秒,用abs防止午夜时刻出错
DoEvents
Wend
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
bytInput = frmmain.ctrMSComm.Input
'frmmain.ctrMSComm.PortOpen = True
frmmain.ctrMSComm.InBufferCount = 0
Call InputManage(bytInput, intInputLen)
Call GetDisplayText
If Not blnAutoSendFlag And Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
End If
End Select
frmmain.ctrMSComm.InBufferCount = 0
'frmmain.txtHexEditASCII.Text = ""
' Call cmdClear_Click
'Call Form_Load
End Sub
'接收到字符后,与原数据保存在一起
'输入处理
'bytReceiveByte()
Public Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
On Error GoTo errorsetting
Dim n As Integer
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
'ReDim Preserve bytReceiveByte(intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
'bytReceiveByte(n - 1) = bytInput(n - 1)
Next n
errorsetting:
frmmain.ctrMSComm.InBufferCount = 0
intReceiveLen = intReceiveLen + intInputLenth
'intReceiveLen = intInputLenth
'frmmain.ctrMSComm.PortOpen = False
End Sub
Public Sub GetDisplayText()
Dim n, m As Double
Dim intValue As Double
Dim intHighHex As Double
Dim intLowHex As Double
Dim strSingleChr, s1, s As String
Dim strDisplayHex As String
Dim strDisplayASCII As String
strDisplayAddress = ""
strDisplayHex = ""
strDisplayASCII = ""
Dim intStart As Double
Dim intLenth As Double
frmmain.txtHexEditASCII.Text = ""
strASCII = ""
strHex = ""
strAddress = ""
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
strSingleChr = Chr(intValue)
strASCII = strASCII + strSingleChr
s1 = strSingleChr + s1
intHighHex = intValue 16
Text1.Text = intValue
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
frmmain.txtHexEditASCII.Text = strASCII
Next n
strASCII = ""
'读取整个字符串中的数字字符串
k = 0
n1 = 3
s = s1 '取字符串
For i = 1 To Len(s) '按顺序逐个字符判断
T = Mid(s, i, 1) '取i位置的一个字符
If T < "0" Or T > "9" Then '判是否数字
k = 0 '非数字,置k=0
Else
If k = 0 Then
n1 = n1 - 1 '指向列表框的下一行
'rece(n1) = t
If n1 >= 0 Then
Text2(n1).Text = T '存入列表框
k = 1
End If
Else '把数字加入到列表框当前行的末尾
Text2(n1).Text = Text2(n1).Text + T
End If
End If
Next i
' frmmain.txtHexEditASCII.Text = ""
'调用计算程序
Call cmdCalculate_Click
End Sub
Private Sub Form_Load()
blnAutoSendFlag = False
blnReceiveFlag = False
intReceiveLen = 0
ctrTimer.Enabled = False
TxtSend.Text = 1
intOutMode = 0
frmmain.cboHexASCII.Text = "按ASCII码"
intHexWidth = 8
intTime = 1000
intPort = 1
strSet = "300,n,8,1"
frmmain.ctrMSComm.InBufferSize = 1024
frmmain.ctrMSComm.OutBufferSize = 512
'frmmain.ctrMSComm.PortOpen = False
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = 1
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
'初始化串口
End If
'初始化显示窗口
'设置显示窗口位置尺寸
frmmain.fraHexEditBackground.Left = frmmain.TxtReceive.Left
frmmain.fraHexEditBackground.Top = frmmain.TxtReceive.Top
frmmain.fraHexEditBackground.Width = frmmain.TxtReceive.Width
frmmain.fraHexEditBackground.Height = frmmain.TxtReceive.Height
'显示初始化
'Call cmdClear_Click
End Sub
Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1
End If
ConvertHexChr = test
End Function
Private Sub receive_Click()
If blnReceiveFlag Then
If Not blnAutoSendFlag And Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
frmmain.cmdReceive.Caption = "开始接收"
Else
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
frmmain.ctrMSComm.InputLen = 0
frmmain.ctrMSComm.InputMode = 0
frmmain.ctrMSComm.InBufferCount = 0
frmmain.ctrMSComm.RThreshold = 1
frmmain.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
'发送
Private Sub cmdAutoSend_Click()
If blnAutoSendFlag Then
frmmain.ctrTimer.Enabled = False
If Not blnReceiveFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
frmmain.cmdAutoSend.Caption = "自动发送"
Else
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
frmmain.ctrTimer.Interval = intTime
frmmain.ctrTimer.Enabled = True
frmmain.cmdAutoSend.Caption = "停止发送"
End If
blnAutoSendFlag = Not blnAutoSendFlag
End Sub
'自动发送设置
Private Sub ctrTimer_Timer()
Dim longth As Integer
Dim bytSendByte()
Dim strSendText As String
strSendText = frmmain.TxtSend.Text
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
If intOutMode = 0 Then
frmmain.TxtReceive.Text = "ascii"
frmmain.ctrMSComm.Output = strSendText
Else
'longth = strHexToByteArray(strSendText, bytSendByte())
If longth > 0 Then
frmmain.ctrMSComm.Output = bytSendByte
End If
End If
End Sub
'自动发送里调用函数
Function strHexToByteArray(strText As String, bytByte() As Integer)
Dim HexData As Integer
Dim hstr As String * 1
Dim lstr As String * 1
Dim HighHexData As Integer
Dim LowHexData As Integer
Dim HexDataLen As Integer
Dim StringLen As Integer
Dim Account As Integer
strText = ""
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = ""
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = ""
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
'手动发送
Private Sub cmdManualSend_Click()
If Not frmmain.ctrMSComm.PortOpen Then
frmmain.ctrMSComm.CommPort = intPort
frmmain.ctrMSComm.Settings = strSet
frmmain.ctrMSComm.PortOpen = True
End If
Call ctrTimer_Timer
If Not blnAutoSendFlag Then
frmmain.ctrMSComm.PortOpen = False
End If
End Sub
Private Sub cboHexASCII_Click()//按ASCII显示
If frmmain.cboHexASCII.Text = "按ASCII码" Then
intOutMode = 0
Else
intOutMode = 1
End If
End Sub
Sub delay()
For i = 1 To 100
Next i
End Sub
//我们接受到的数据比如在第一次我们发送C1023a3456b9144a
给微机,我们把字符串里的数字给反序取出来分别给TEXT2(2).TEXT,TEXT2(1).TEXT,TEXT2(0).TEXT以及取第一个字符作为标志位,传到我的计算过程调用不同的算法.再把数值给计算出来.
现在已经能够进行多次连续计算.
但是如果我们第一次发C1023a3456b9144a给微机,我们调用C作为标志调用算法一计算出数值,我们再发e1023a3456b9144a,按照我们原来的思路应该是调用e为标志的算法计算出另外一组数据.现在的问题是我们第二次发过去,依然显示的是C1023a3456b9144a,调用C做为标志的算法.我们如果改动其中的数值,比如改为C2023a3456b1144a我们的程序即使连续发送也可以进行识别将数值给提取出来,但由于如果我们无法调用e为标志的算法,计算出来的数值是错误的.造成一种情况所见非所得!
注:标志字符,我们通过将传过来的数据赋给frmmain.txtHexEditASCII.Text再通过函数MID(frmmain.txtHexEditASCII.Text,1,1)j即取得首字符,也是我们的标志字符.
希望诸位大侠能够帮忙,小弟真的很急,已经熬了很久了,希望大家能够指点一二,小弟感激不尽!