VB串口通信
1.怎么发送这串数,怎么接收这串数?
2.编写一个测试程序,模拟单片机发出来的数据,
要求:间隔100ms时间,发送一次这样的数据,然后让上位机接收,
应该如何去编写?
请各位大侠帮帮忙啊,小女子将不甚感激!万分感谢! 展开
以前做工程用的,你修改一下,可能对你有用,你看看吧
'======================串口初始化=======================
Private Sub Comm_initial(Port As Byte, BaudRate As String, ParityBit As String, DataBit As Integer, StopBit As Integer)
On Error GoTo ErrorTrap
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False ' 串口若已打开,则先关闭
MSComm1.CommPort = Port ' 设置端口号
MSComm1.Settings = BaudRate & "," & ParityBit & "," & DataBit & "," & StopBit ' 设置波特率、校验位、数据位、停止位
MSComm1.InBufferSize = 1024 ' 设置接收缓冲区为1024字节
MSComm1.OutBufferSize = 4096 ' 设置发送缓冲区为4096字节
MSComm1.InBufferCount = 0 ' 清空输入缓冲区
MSComm1.OutBufferCount = 0 ' 清空输出缓冲区
MSComm1.SThreshold = 1 ' 发送缓冲区被清空时,触发OnComm事件
MSComm1.RThreshold = 1 ' 接收缓冲区每接收到一字节数据时,触发OnComm事件
MSComm1.PortOpen = True '打开串口
If MSComm1.PortOpen = True Then
txtstatus.Text = "STATUS:" & cboport.Text & " OPEND," & cbobps.Text & "," & Left(CboParity.Text, 1) & "," & Cbobit.Text & "," & cbostop.Text
Else
txtstatus.Text = "STATUS:COM Port Cloced"
End If
Exit Sub
ErrorTrap:
Select Case Err.Number
Case comPortAlreadyOpen
MsgBox "串口冲突,请更改串口号", 48, "公司车辆管理系统"
CloseCom
Case Else
MsgBox "没有发现此串口,请确认连接", 48, "公司车辆管理系统"
CloseCom
End Select
Err.Clear
End Sub
Private Sub cmdclose_Click()
Call CloseCom
End Sub
Private Sub cmdset_Click()
Call Comm_initial(Val(Mid(cboport.Text, 4, 1)), cbobps.Text, Left(CboParity.Text, 1), Val(Cbobit.Text), Val(cbostop.Text))
End Sub
Private Sub Form_Load()
Call Comm_initial(Val(Mid(cboport.Text, 4, 1)), cbobps.Text, Left(CboParity.Text, 1), Val(Cbobit.Text), Val(cbostop.Text)) '串口初始化
MSComm1.InputMode = comInputModeText '通过文本方式读取数据
End Sub
'=========================数据接收子程序=========================
Private Sub dataReceive()
Dim strByte() As String '数据分割用数组
Dim receiveData As String ' 接收数据暂存
Dim i As Integer
If (MSComm1.InBufferCount > 0) Then
receiveData = "" ' 清接收数据暂存
receiveData = MSComm1.Input '接收数据
'txtreceive.Text = txtreceive & receiveData
'===============数据在相应文本框里显示=======
strByte = Split(receiveData)
For i = 0 To UBound(strByte)
txt1(i).Text = strByte(i)
Next i
txt1(8).Text = Now
End If
End Sub
'====================OnComm事件===========================
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent ' 设置oncomm事件,读取片机内存的值
Case comEvReceive
Call dataReceive
Case Else
End Select
End Sub
'====================关闭串口子程序=====================
Private Sub CloseCom()
On Error GoTo Err
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False ' 串口若已打开,则关闭
txtstatus.Text = "STATUS:COM Port Cloced"
Err:
End Sub
2010-07-19
VB串口通信编程源码206个,多种行业多种应用...
VB串口通信技术文章160多篇,详尽地对VB串口通信的诠释...
VB串口通信电子书籍16部,工业通信名家著作,经典细致...
VB串口通信控件16个,简化串口通信编码,提高开发效率...
VB串口调试工具80个,应用于各种串口调试与校验计算...
VB串口视频教程46个,两套完整视频教程,讲解直观,更快掌握...
程序员发展类文章和书籍12部,帮助您的成长,健康和发展....
超级赠品一:OSI网络模型教程48M
超级赠品二:串口硬件层知识大全资料包24M
你在淘宝搜索“太阳雨VB串口资料”就可以找到,希望可以帮助到你。
Dim bytSj() As Byte
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
ReDim bytSj(1 To 6)
bytSj(1) = &H25
bytSj(2) = &H1E
bytSj(3) = &H0
bytSj(4) = &H2A
bytSj(5) = &H86
bytSj(6) = &H89
MSComm1.Output = bytSj
End Sub
赋值 给 数组元素
发送 时 直接 mscomm1.output = OutData
推荐 看 书
Visual_Basic与_RS-232_串行通信控制
经典入门书
祝你顺利