Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

VB
<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
Posted
Updated 13-Nov-17 23:37pm
v3
Comments
Richard MacCutchan 13-Nov-17 6:52am    
"listbox lidnt work properly and losses characters or confuse them"
What does that mean?
Παλαβρατζής Βασίλης 13-Nov-17 8:45am    
It means that my device sends the data with the baud rate of 38400 and this data up to my code stored correctly, line by line in the Richtextbox. What i m trying to do is to "fish" from each line some characters and put them on a listbox. Lets say from the whole string "?@D00014C000 000" i need 3 of characters "14C". Up to my code again, what i get back from the list box lines is something like : 14C 14C 14C 00 D00 14C 000..... like confusing the data i asked for

1 solution

Quote:
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.
 
Share this answer
 
Comments
Παλαβρατζής Βασίλης 13-Nov-17 7:28am    
Could you please help me with that check ?
Παλαβρατζής Βασίλης 13-Nov-17 8:02am    
i tried this one but nothing happend

Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub

Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
If Me.RichTextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBox2.Text &= [text] 'append text
DataGridView1.Rows.Insert(0, [text])
Dim fine As String = Mid([text], 7, 3)
If DataGridView1.CurrentRow.IsNewRow = True Then

DataGridView2.Rows.Insert(0, fine)

End If
End If
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900