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

串口通讯232VB程序多字节发送改编

2012-06-21 
串口通信232VB程序多字节发送改编现在下面的这个程序是单字节发送,比如发送的是01他接收的就是01.现在我要

串口通信232VB程序多字节发送改编
现在下面的这个程序是单字节发送,比如发送的是01他接收的就是01.现在我要求要多字节发送,比如发送的是010203对应的接收就是010203,这个程序怎么改都改不对,谢谢了。
Private Sub Form_Load()

If MSComm1.PortOpen = True Then
  MSComm1.PortOpen = False
Else
End If

Combo1.AddItem "COM1"
Combo1.AddItem "COM2"
Combo1.AddItem "COM3"
Combo1.ListIndex = 2

Combo3.AddItem "4"
Combo3.AddItem "5"
Combo3.AddItem "6"
Combo3.AddItem "7"
Combo3.AddItem "8"


MSComm1.CommPort = Combo1.ListIndex + 1
MSComm1.Settings = "9600,n,8,1"
ComOpen.Caption = "´ò¿ª´®¿Ú"
Shape1.FillColor = &HFFFFC0

End Sub

Private Sub ComOpen_Click()
On Error GoTo uerror
 
 If ComOpen.Caption = "¹Ø±Õ´®¿Ú" Then
  MSComm1.PortOpen = False
  ComOpen.Caption = "´ò¿ª´®¿Ú"
  Shape1.FillColor = &HFFFFC0
 Else
   
  MSComm1.PortOpen = True
  ComOpen.Caption = "¹Ø±Õ´®¿Ú"
  Shape1.FillColor = &HFF
 End If
 Exit Sub
 

uerror:
  msg$ = "ÎÞЧ¶Ë¿ÚºÅ"
  Title$ = "´®¿Úµ÷ÊÔÖúÊÖ"
  X = MsgBox(msg$, 48, Title$)
End Sub

Private Sub MSComm1_OnComm()
  Dim BytReceived() As Byte
  Dim i As Integer
  Dim strBuff As String
  Select Case MSComm1.CommEvent
  Case 2
  Cls
   
  MSComm1.InputLen = 0 '0±íʾ¶ÁÈ뻺³åÇøÈ«²¿ÄÚÈÝ
  strBuff = MSComm1.Input '¶ÁÈëµ½»º³åÇø
   
  BytReceived() = strBuff '¶þ½øÖƽÓÊÕģʽ
  For i = 0 To UBound(BytReceived)
  If Len(Hex(BytReceived(i))) = 1 Then
  strData = strData & "0" & Hex(BytReceived(i)) & " " 'Èç¹ûÖ»ÓÐÒ»¸ö×Ö?û£¬Ôòǰ²¹0£¬ÈçFÏÔʾ0F£¬×îºó²¹¿Õ¸ñ
  Else '?½±ãÏÔʾ¹Û²ìÈ磺 00 0F FE
  strData = strData & Hex(BytReceived(i)) & " "
  End If
  Next
  TextReceive = TextReceive & strData
  strData = ""
   
  End Select
End Sub

Private Sub ComSend1_Click()
Dim Temp(0) As Byte
Dim strBuff As String
  Temp(0) = "&H" & TextSend
  MSComm1.Output = Temp
If MSComm1.PortOpen = False Then
  MsgBox "Çë´ò¿ª´®¿Ú"
End If

On Error GoTo uerror
MSComm1.Output = strBuff

uerror:

End Sub
   
Private Sub ComClean1_Click()

TextSend.Text = ""

End Sub

Private Sub Timer1_Timer()

Call ComSend1_Click



End Sub


Private Sub Combo1_Click()

If MSComm1.PortOpen = True Then
  MSComm1.PortOpen = False
 End If
MSComm1.CommPort = Combo1.ListIndex + 1

End Sub

Private Sub ComClean2_Click()

TextReceive.Text = ""

End Sub


[解决办法]
http://download.csdn.net/source/1262066
[解决办法]
把要发的数据组成一数组连续发不就行了!
[解决办法]
触发oncomm事件与MSComm1.RThreshold设置相关。

热点排行