首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

串口接收16进制数据后,再把接到的数据以16进制发送!该如何解决

2012-03-26 
串口接收16进制数据后,再把接到的数据以16进制发送!Dim str As StringDim BytReceived() As ByteDim bytSe

串口接收16进制数据后,再把接到的数据以16进制发送!
Dim str As String
Dim BytReceived() As Byte
Dim bytSendByte() As Byte  

Private Sub MSComm1_OnComm()
  Dim strBuff As String
  Select Case MSComm1.CommEvent
  Case 2
  MSComm1.InputLen = 0
  strBuff = MSComm1.Input
  BytReceived() = strBuff
  jieshou
  End Select

  Dim sj() As Byte
  Dim sj_Txt As String
  Dim q As Integer
  sj_Txt = str
  If Len(str) Mod 2 = 0 And Len(str) <> 0 Then 
  ReDim sj(Len(sj_Txt) / 2 - 1)
  For q = 0 To Len(sj_Txt) - 1 Step 2
  sj(q / 2) = Val("&H" & Mid(sj_Txt, q + 1, 2))
  Next
  MSComm1.Output = sj
  str = ""
  End If
end sub

Public Function jieshou()
  Dim i As Integer
  For i = 0 To UBound(BytReceived)
  If Len(Hex(BytReceived(i))) = 1 Then
  str = str & "0" & Hex(BytReceived(i))
  Else
  str = str & Hex(BytReceived(i))
  End If
  Next
  Text1.Text = str
End Function

Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary  
MSComm1.InBufferCount = 0  
MSComm1.OutBufferCount = 0 
MSComm1.RThreshold = 1  
MSComm1.InBufferSize = 1024 
MSComm1.PortOpen = True
end sub

接收的数据在text1.text中显示,发送好像错了...错在哪儿了...

[解决办法]
看看你的代码,不知道你来回折腾啥?
---------------------------------------------
你折腾半天与以代码有什么区别?

VB code
Private Sub MSComm1_OnComm()  Dim strBuff() As byte  Select Case MSComm1.CommEvent  Case 2  MSComm1.InputLen = 0  strBuff = MSComm1.Input  BytReceived() = strBuff  jieshou  End Select  MSComm1.Output = strBuff  End Ifend sub
[解决办法]
VB code
Option Explicit    Dim str As String    Dim BytReceived() As Byte    Dim bytSendByte() As Byte    Dim sj_Txt As StringPrivate Sub MSComm1_OnComm()    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                str = str & "0" & Hex(BytReceived(i))            Else                str = str & Hex(BytReceived(i))            End If            Next            Text1.Text = str            If Mid(str, 1, 2) = "AA" And Len(str) = 22 Then                sj_Txt = str                Timer1.Enabled = True                str = ""            End If    End SelectEnd SubPrivate Sub Form_Load()    MSComm1.CommPort = 1    MSComm1.Settings = "9600,n,8,1"    MSComm1.InputMode = comInputModeBinary    MSComm1.InBufferCount = 0    MSComm1.OutBufferCount = 0    MSComm1.RThreshold = 1    MSComm1.InBufferSize = 1024    MSComm1.PortOpen = True    Timer1.Enabled = False    Timer1.Interval = 3000End SubPrivate Sub Timer1_Timer()    Dim sj() As Byte    Dim q As Integer    If Len(sj_Txt) Mod 2 = 0 And Len(sj_Txt) <> 0 Then        ReDim sj(Len(sj_Txt) / 2 - 1)        For q = 0 To Len(sj_Txt) - 1 Step 2            sj(q / 2) = Val("&H" & Mid(sj_Txt, q + 1, 2))        Next        MSComm1.Output = sj        str = ""    End If    Timer1.Enabled = FalseEnd Sub
[解决办法]
探讨
14楼 估计就是你说的接收后发送延迟问题...单片机设置的延迟是三秒....
可是按代码这三秒是没有发送过去...这个要怎么设置...用串口调试
我事先把要发给单片机的数据写好然后单片机发送过来我立马按发送,调试成功....

试问大家要怎么实现把接收后的代码马上发送...

热点排行