serial communication
serial communication
serial communication
serial communication
serial communication
serial communication serial communication serial communication serial communication serial communication serial communication serial communication serial communication
serial communication serial communication
serial communication
Go Back  Xtreme Visual Basic Talk > > > serial communication


Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2011, 05:40 AM
andrebr andrebr is offline
Newcomer
 
Join Date: May 2010
Posts: 13
Default serial communication


Hi guys.
I have a problem with my serial communication. To my computer I've connected a Arduino board.
I send data from the board to my computer.
For data to appear, I need to send same data two times for my form to handle it.
But It apears in serial data view...

How could I solve this?

VB Code:
Code:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())    'Automatically called every time a data is received at the serialPort
        Thread.Sleep(1000)
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
**********************************************************
But here the data appears!?
            
            Me.rtbReceived.Text &= [text]
**********************************************************
        End If

        Dim myFile As String = [text]
        Dim aryTextFile() As String
        myFile = Replace(myFile, " ", "")
        aryTextFile = myFile.Split(",")

        'Sett inn sensorene her
        Dim i As Integer
        For i = 0 To UBound(aryTextFile)
            Dim countTextChar(i) As Integer
            Dim myLeft(i) As String
            Dim myInput(i) As String
            Dim myText(i) As String

            countTextChar(i) = Len(aryTextFile(i))
            myInput(i) = Microsoft.VisualBasic.Left(aryTextFile(i), 1)
            myLeft(i) = Microsoft.VisualBasic.Left(aryTextFile(i), 3)
**********************************************************
This is the data which will not show.
Why!?
            If myInput(i) = Chr(107) Then
                If myLeft(i) = "k1:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    k1.Text = myText(i) & PercentPrefix
                End If
                If myLeft(i) = "k2:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    k2.Text = myText(i) & PercentPrefix
                End If
            End If

            If myInput(i) = Chr(115) Then
                If myLeft(i) = "s1:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    s1.Text = myText(i) & TempPrefix
                End If
                If myLeft(i) = "s2:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    s2.Text = myText(i) & TempPrefix
                End If
            End If
        Next i
**********************************************************
    End Sub
Reply With Quote
  #2  
Old 04-26-2011, 10:18 PM
mkaras's Avatar
mkarasserial communication mkaras is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Mar 2004
Location: Beaverton, OR
Posts: 1,874
Default

When you say you have to send the data twice is it like A or B below?

A) You have to send 123456 123456 for your VB program to see 123456

B) You have to send 112233445566 for your VB program to see 123456

If is case B is there a possibility that the bytes coming in the serial port are being paired as double byte characters and then you are getting one byte from each pair?

Michael Karas
Reply With Quote
  #3  
Old 04-27-2011, 01:27 AM
andrebr andrebr is offline
Newcomer
 
Join Date: May 2010
Posts: 13
Angry

Hi mkaras.
It's like A.

I don't understand why!

Best,
Andy
Reply With Quote
  #4  
Old 04-27-2011, 08:16 AM
passel's Avatar
passelserial communication passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

I believe all that code after the "end if" needs to be inside the "else" block.
Your actual receive is in one thread, but you want to process that data in your GUI thread, so you copy the data into "Me.rtbReceived.Text &= [text]" in your GUI thread (inside the else case)
But you process the code after the if..else.. in both threads. The first time the data isn't in Me.rtbReceived.Text, and the second time it is.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
serial communication
serial communication
serial communication serial communication
serial communication
serial communication
serial communication serial communication serial communication serial communication serial communication serial communication serial communication
serial communication
serial communication
 
serial communication
serial communication
 
-->