在visual basic中接收和控制串行设备数据 [英] Receive and control serial device data in visual basic

查看:70
本文介绍了在visual basic中接收和控制串行设备数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我有一个串口设备,它以38400的波特率向我发送数据,我得到它像?@ D00014C000 000 。我可以看到数据进入ritchtextbox但是我想要做的是使用列表框中字符串中的某些字符。

例如,我希望 listbox3 中出现 14C 字符。我尝试了子串 mid 功能但是listbox lidnt正常工作并丢失了字符或混淆了它们。这是我的代码。有什么建议吗?

Hello everyone!! I have a serial device which sends me data at the baudrate of 38400 and i get it like this "?@D00014C000 000".I can see data comes in on a ritchtextbox but what im trying to do is to use some characters from the string in a list box.
For example i want characters "14C" appears in the listbox3. I tried the substring and mid function but listbox lidnt work properly and losses characters or confuse them . Here is my code. Any suggestions please??






我尝试了什么:





What I have tried:

<pre>
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports

Public Class frmMain
    Dim myPort As Array
    Delegate Sub SetTextCallback(ByVal [text] As String)
  

    'Serial Port Receiving Code Starts Here ....
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())

    End Sub
    'Serial Port Receiving Code Ends Here ....

    'Serial Port Receiving Code(Invoke) Starts Here ....
    Private Sub ReceivedText(ByVal [text] As String)
        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})

           

        Else
            Me.rtbReceived.Text &= [text]
              Dim fine As String = Mid([text], 7, 3)
            Dim list As Integer = ListBox3.Items.Add(fine)
        End If

    End Sub


End Class

推荐答案

Quote:

Me.rtbReceived.Text& = [text ]

Dim fine As String = Mid([text],7,3)

Dim list As Integer = ListBox3.Items.Add(fine)

Me.rtbReceived.Text &= [text]
Dim fine As String = Mid([text], 7, 3)
Dim list As Integer = ListBox3.Items.Add(fine)



问题是你正在添加到列表中,而文本框字符串可能仍然不完整(你正在逐步建立它)。



你必须首先检查收到的字符串是否完整,然后提取子字符串。


The problem is you are adding to the list while the text box string might be still incomplete (you are building it incrementally).

You have to first check if the received string is complete and then extract the substring.


这篇关于在visual basic中接收和控制串行设备数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆