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

api 串口通信,该怎么处理

2012-03-17 
api 串口通信Function WriteCOM32(COMString As String) As IntegerOn Error GoTo handelwritelptDim RetB

api 串口通信
Function WriteCOM32(COMString As String) As Integer
On Error GoTo handelwritelpt
  Dim RetBytes As Long, LenVal As Long
  Dim retval As Long
   
  If Len(COMString) > 255 Then
  WriteCOM32 Left$(COMString, 255)
  WriteCOM32 Right$(COMString, Len(COMString) - 255)
  Exit Function
  End If
   
  For LenVal = 0 To Len(COMString) - 1 Step 2
' bRead(LenVal) = Asc(Mid$(COMString, LenVal + 1, 1))
  bRead(LenVal) = Val("&H" & Mid(COMString, LenVal + 1, 2))
  Debug.Print bRead(LenVal)
  Next LenVal
' bRead(LenVal) = 0
   
  retval = WriteFile(ComNum, bRead(0), Len(COMString), RetBytes, 0)
' FlushComm
  WriteCOM32 = RetBytes
   
handelwritelpt:
  Exit Function
End Function
这是api串口发送,如我要发送16进制0011,用其它串口调试工具接收的却是00 00 11 00呢? 
如果将改为 retval = WriteFile(ComNum, bRead(0), Len(COMString)/2, RetBytes, 0)旧只能接收到00 00了



[解决办法]
retval = WriteFile(ComNum, bRead(0), Len(COMString), RetBytes, 0)
改成
retval = WriteFile(ComNum, bRead(0), UBound( bRead) + 1 , RetBytes, 0)
试试

热点排行