急求大侠帮忙~Vb串口通讯
小弟刚学VB,做一个自发自收的项目,使用MSComm控件,采用二进制传输方式,实现自发自收。在接受数据时总是乱码,还有选择自动接收(AutoReceive)无效,请高人指点!!!!!以下是小弟的Code
————————————————————————————————————
Option Explicit '遇到未声明的变量名字时就发出错误警告
Dim OutByte() As Byte '定义OutByte为动态数组
Dim InByte() As Byte '定义InByte为动态数组
————————————————————————————————————
Private Sub CmdSend_Click()
Dim Buf$, i% '定义Buf为字符串型变量,i为整型变量
Buf = Trim(TextSend.Text) '将文本框中的字符串移除空白后给Buf
OutByte = Buf
Buf = MSComm1.Input '清空输入缓冲区
MSComm1.Output = OutByte '将转换后的数据放入输出缓冲区
End Sub
——————————————————————————————————
Private Sub CmdExit_Click()
End
End Sub
——————————————————————————————————
Private Sub CmdReceive_Click()
Dim i%, Buf$ '定义i为整型变量,Buf为字符串变量
Buf = "" 'Buf初始为空
InByte = MSComm1.Input '缓冲区的数据给InByte动态数组
For i = LBound(InByte) To UBound(InByte) '采用循环读取接收缓冲区中的数据放入Buf变量中
Buf = Buf + Str(InByte(i)) + Chr(32) 'Str函数是将数值类型转换成字符类型
Next i '循环返回或结束
Buf = StrConv(Buf, vbFromUnicode) '将ASCII数组c转换为String的函数
TextReceive.Text = Buf '把接收到的数据在输出文本框中显示
End Sub
——————————————————————————————————————
Private Sub Form_Load()
MSComm1.PortOpen = True '打开端口
End Sub
Private Sub AutoReceive_Change()
Timer1.Enabled = Not Timer1.Enabled '启动定时器,进入自动接收模式
End Sub
——————————————————————————————————————
Private Sub Timer1_Timer()
If MSComm1.InBufferCount > 0 Then
TextReceive.Text = TextReceive.Text + MSComm1.Input '定时的从输入缓冲区读取字符
End If
End Sub
——————————————————————————————————————
大侠们~帮忙啊~
Option Explicit
Private Sub CmdSend_Click()
Dim Buf$
Buf = Trim(TextSend.Text)
MSComm1.Output = Buf
End Sub
Private Sub CmdExit_Click()
End
End Sub
Private Sub Form_Load()
MSComm1.PortOpen = True
End Sub
Private Sub AutoReceive_Change()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Timer()
If MSComm1.InBufferCount > 0 Then
TextReceive.Text = TextReceive.Text + MSComm1.Input
End If
End Sub
Dim Buf$ '定义i为整型变量,Buf为字符串变量
Buf = "" 'Buf初始为空
InByte = MSComm1.Input '缓冲区的数据给InByte动态数组
Buf = StrConv(InByte , vbFromUnicode) '将ASCII数组c转换为String的函数
TextReceive.Text = Buf '把接收到的数据在输出文本框中显示
---------------------
Dim Buf$ '定义i为整型变量,Buf为字符串变量
Buf = "" 'Buf初始为空
InByte = MSComm1.Input '缓冲区的数据给InByte动态数组
Buf = StrConv(InByte , vbUnicode) '将ASCII数组c转换为String的函数
TextReceive.Text = Buf '把接收到的数据在输出文本框中显示
http://download.csdn.net/source/1262066 看看这个:
http://download.csdn.net/source/1262066
http://download.csdn.net/source/1498637
http://download.csdn.net/source/1498644
想学习一下!!!!!!! 数据类型要处理好,不然接收的都是错的,我前一段碰到类似问题。
补充:VB , COM/DCOM/COM+