十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
不知道你是不是用51单片机往串口发数据,是的话那是因为51的SBUF最多存放8个字节,所以你发12个字节会分成两次发完。还有在接收的时候最好定义好通信协议,比如加一个侦头,一个侦尾,通过这两个字节的内容来接收数据。
成都创新互联为企业级客户提高一站式互联网+设计服务,主要包括网站设计、成都网站建设、重庆APP开发公司、微信平台小程序开发、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。
老兄,提问还是给点悬赏分嘛,不要吝啬哪点分,知识是无价的,你给得越多,回答你的人才会越多。
'vb.net的串口接收数据要用到委托
Delegate Sub SetTextCallback(ByVal InputString As String) '声明一个委派类,并声明符合函数参数有一个,而其型态是字符串
Private Sub ShowString(ByVal comData As String)
txt_Re.Text = comData '将收到的数据入接收文字框中--- txt_Re.Text 是接收用的文本框
txt_Re.SelectionStart = txt_Re.Text.Length
txt_Re.ScrollToCaret()
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived‘自动接收事件
Dim inData As String = SerialPort1.ReadExisting
Dim d As New SetTextCallback(AddressOf ShowString)
Invoke(d, inData)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '发送指令
SerialPort1.Write("123")
End Sub
使用委托方式处理:
Public Class Form1
Dim mSTR As String
Dim strData As String
Dim strDat As String
Dim mRecvByte() As Byte
Dim Hexsj As String
Dim form2 As Form
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim mSize As Integer = Me.SerialPort1.BytesToRead
ReDim mRecvByte(mSize - 1)
SerialPort1.Read(mRecvByte, 0, mSize)
BeginInvoke(New EventHandler(AddressOf ONMLoadA), SerialPort1.BytesToRead()) '使用委托方式显示接收到的字符串
End Sub
Sub ONMLoadA(ByVal sender As System.Object, ByVal e As System.EventArgs) '委托
strDat = ""
TextBox1.Text = ""
Dim i As Integer
For i = 0 To UBound(mRecvByte) 'mSize - 1
strData = strData IIf(mRecvByte(i) 15, Hex(mRecvByte(i)), "0" Hex(mRecvByte(i))) " "
Next
Dim sj As Byte
For i = 1 To Len(strData) Step 3 '处理为ASCII字符
sj = Val("H" Mid(strData, i, 2))
If sj 32 Or sj 128 Then '当接收字节中有Chr(0)时,其后字符被切割
strDat = strDat "."
Else
strDat = strDat Chr(sj)
End If
Next
TextBox1.Text = strDat '显示字符
TextBox2.Text = strData '显示为16进制
TextBox3.Text = Len(strData) / 3
End Sub
你可以这样操作:
Form1.BeginInvoke(Sub()
'一些操作
End Sub)
这样主线程就会放下手里的事情并执行Sub里的操作了
不是很明白你的题意
strHex = strHex + [String].Format("{0:X2} "
这里的意思是把每个字节数据转换成了十六进制,每个字节占两个字符
如果你串口收到的4个字节数据:43,27,56,200
那么你的结果是:2B1B38C8
即receivebytes.Text="2B1B38C8"
不足两位的补0
如果你串口收到的4个字节数据:3,27,56,200
那么你的结果是:031B38C8
即receivebytes.Text="031B38C8"
首先:
textbox里没有显示,是因为SerialPort1和TextBox2不是同一线程创建的,需要跨线程操作。需要用到委托,这样才能显示出来。
其次:
我觉得用串口的接收数据事件更好一些。
下面代码供参考:
'----------------------
'串口接收数据事件,其实比用定时器更好,
'触发事件的条件可以自己在form_load中设置ReceivedBytesThreshold属性数值,默认为ReceivedBytesThreshold=1
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim strRecvData As String = ""
strRecvData = SerialPort1.ReadExisting
Call disPlayComData(strRecvData)
End Sub
Delegate Sub callback(ByVal strT As String) '定义委托
Sub showString(ByVal comdata As String) '显示结果
Me.TextBox1.Text = "结果:" comdata
End Sub
Sub disPlayComData(ByVal strTmp As String) '判定是否为跨线程
If Me.TextBox1.InvokeRequired Then
Dim d As New callback(AddressOf showString)
Me.Invoke(d, New Object() {strTmp})
Else
Me.TextBox1.Text = strTmp
End If
End Sub