VB利用串口与单片机通信

来源:百度知道 编辑:UC知道 时间:2024/07/08 04:18:58
利用VB串口和单片机通信,把一些Text(比如text1.text)传送给单片机然后还要把单片机发送的接收上来。通信协议如下:

——————————————————————————————
系统采用主从通信,上位PC机为主机,下位机为从机,通信由主机发起;
通信格式(19200,N,8,1),即波特率19200bps,无校验位,8位数据位,1位停止位.
帧结构:帧首+命令码+数据码+帧尾
通信过程中,如果上位PC机等待回应的时间超过200MS,认为通信出现故障,上位PC机应重发命令.
帧首: 1字节,取值0XBB,代表一帧的开始;
命令码:1字节,取值0XF0~0XFE,代表广播机要执行的命令;上位机发送命令帧时,状态码规定为00;
数据码: 若无特殊说明为16进制,长度不定,根据命令而改变.
帧尾: 16进制,1字节,取值0XEE,代表一帧的结束
——————————————————————————————

请问怎么写?以前没怎么用过VB,这次任务压下来,来不及了,希望高手给指点指点!先给100分,成功通过后再给100分,单片机那边不用想太多,只要把VB这写好就行了。。。这次被人阴了,晕死。多谢!!

Option Explicit
Dim strData As String
Dim sjSEnd() As Byte

'输入处理为16进制字符串
Public Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
Dim n As Integer
ReDim bytReceiveByte(intInputLenth)
For n = 0 To intInputLenth - 1 Step 1
strData = strData & Hex(bytInput(n))
Next n
Text2 = strData
End Sub

Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
MSComm1.Settings = "19200,N,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
Text1 = "" '在Text1中写16进制指令
Timer1.Interval = 200
Timer1.Enabled = False
End Sub

Private Sub MSComm1_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer
If (MSComm1.CommEvent = comEvReceive) Then
MSComm1.InputMode = comInputModeBinary
intInputLen = MSComm1.InBufferCount
If intInputLen > 0 Then