Go Down

Topic: Serial.print(); (Read 697 times) previous topic - next topic

cbrandy

Hi.
I have a really strange question.
Inside a function I have in my program, I have a "Serial.print(value);".
I send this value via serial port to av vb soft.
Here come the strange part:
I get the value in a serial window. But when I use the data to appear in a textbox, I need to send the data like:
"Serial.print(value);Serial.print(value);".
I need to send the data two times for this to work.

Annyone?
-Andy

robtillaart

Without code from sender and receiving it is very difficult to tell.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

cbrandy

Hi robtillaart.

Arduino code:
Code: [Select]

void printSerialString (){
  if(serInIndx > 0){
     String myData;
     //Serial.println(serInIndx);                                // serInIndx er antall tall / bokstaver som er sendt i seriellport.
                                                                 // serInString er text'en i serial data!
     for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++){    //loop through all bytes in the array and print them out
       myData += serInString[serOutIndx];
     }
     
     //Serial.print(myData);Serial.print(myData);
     
  int myDataLength = myData.length();
  String myPrefix  = myData.substring(0,3);
  if (myData.substring(0,1) == "k") {
    /* her leses input fra serieldata av, og behandler disse dataene */
    if (myPrefix == "k1:") {
      String value = myData.substring(3,myDataLength);
      String k1 = value;
*******************************************************************************************************
I need to print twice for value appear in VB "k1.Text = myText(i) & PercentPrefix"

      Serial.print(myPrefix);
      Serial.print(k1);
      Serial.print(myPrefix);
      Serial.print(k1);
*******************************************************************************************************     
     Serial.println();
     myData = "";
     serOutIndx = 0;
     serInIndx  = 0;
   
   }
}


VB code:
Code: [Select]

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
            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)

            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


I think this is strange...

Go Up