Display the serial port string in a label or text box.

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 18048 Views - Last Post: 20 December 2011 - 05:02 PM Rate Topic: -----

#1 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Display the serial port string in a label or text box.

Posted 19 December 2011 - 07:50 PM

I'm trying to read a string(what's going on) from my Arduino and display it in either a label or text box. but its not wanting to display the characters, or string i want it to. So I could really use some help.
Here's the code I have right now:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Dim incomingByte As String = ""

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        'Button1.BackColor = Color.Red
        Label1.BackColor = Color.Red
        Label1.Text = "The Port Is Closed!"
        ' Add any initialization after the InitializeComponent() call.

    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        With SerialPort1
            .Close()
            .PortName = "COM3"
            .BaudRate = 9600
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .DtrEnable = True
            .RtsEnable = True
            .ReceivedBytesThreshold = 1
        End With
        TextBox1.Text = SerialPort1.ReadChar()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SerialPort1.Open()
        Label1.BackColor = Color.ForestGreen
        Label1.Text = "The Port Is Open!"
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        SerialPort1.Write("A")
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        SerialPort1.Write("B")
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        SerialPort1.Write("C")
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        SerialPort1.Write("D")
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        SerialPort1.Write("E")
    End Sub

    Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        SerialPort1.Write("F")
    End Sub

    Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
        SerialPort1.Write("G")
    End Sub

    Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
        SerialPort1.Write("H")
    End Sub

End Class


And here is the code for my Arduino so far:
/*
*  Created: 12/19/2011 18:11
*   Author: Jonathan Stowe (hightekrednek2396)
*
*  Arduino Cube w/ Random Sequences
*
*/
int i;
int p;
int incoming;
long randomNum;

char str1[] = "case1";

void setup() {
 
  Serial.begin(9600);
  //for (int p; p < 14; p++);
  pinMode(2, OUTPUT);
  
}

void loop() {
  
  if (Serial.available() > 0) {
    incoming = Serial.read();
    
    switch(incoming) {
      case 'A':
        randomCase();
      case 'B':
        case1();
      case 'C':
        case2();
      case 'D':
        case3();
      case 'E':
        case4();
      case 'F':
        case5();
      case 'G':
        case6();
      case 'H':
        case7();
      default:
        delay(500);
    }
  }  
}

void randomCase() {
  randomNum = random(0, 8);
  
  switch(randomNum) {
    
    case 1:
      case1();
    case 2:
      case2();
    case 3:
      case3();
    case 4:
      case4();
    case 5:
      case5();
    case 6:
      case6();
    case 7:
      case7();
  }  
}

void case1() {
  
  Serial.print("case1");
  digitalWrite(2, HIGH);
  
}

void case2() {
  
  Serial.write("case2");
  Serial.write(str1);
  
}

void case3() {
  
  Serial.print("case3");
  
}

void case4() {
  
  Serial.print("case4");
  
}

void case5() {
  
  Serial.print("case5");
  
}

void case6() {
  
  
  
}

void case7() {
  
  
  
}

So if anyone could help me that would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Display the serial port string in a label or text box.

#2 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 08:23 PM

Because the only place you're actually reading from your serial port is in the load event, with TextBox1.Text = SerialPort1.ReadChar()
You should run a loop, a bit like the one I see in your other code (at the bottom).
Was This Post Helpful? 1
  • +
  • -

#3 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 08:31 PM

View PostjimmyBo, on 19 December 2011 - 10:23 PM, said:

Because the only place you're actually reading from your serial port is in the load event, with TextBox1.Text = SerialPort1.ReadChar()
You should run a loop, a bit like the one I see in your other code (at the bottom).


Ok now how would I do that I'm really new to VB and google isn't helping much tonight?
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 08:59 PM

Must just be your machine.
http://www.google.co...net&btnG=&mvs=0
Was This Post Helpful? 0
  • +
  • -

#5 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:01 PM

View PostjimmyBo, on 19 December 2011 - 10:59 PM, said:

Must just be your machine.
http://www.google.co...net&btnG=&mvs=0

Ok now how would i get the two to play nice and which loop would work better or at all
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:04 PM

Read the definitions of the loops in the results of the search URL I gave you - I can't make these decisions for you.
For Loop
Do While
Do Until
For Each

Loops are a very basic part of VB, you should learn about them before you try this :)
Was This Post Helpful? 0
  • +
  • -

#7 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:09 PM

View PostjimmyBo, on 19 December 2011 - 11:04 PM, said:

Read the definitions of the loops in the results of the search URL I gave you - I can't make these decisions for you.
For Loop
Do While
Do Until
For Each

Loops are a very basic part of VB, you should learn about them before you try this :)

Ok and do you know where i can get better definitions of serial port functions?
Was This Post Helpful? 0
  • +
  • -

#8 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:12 PM

Look at the MSDN docs on it:
http://msdn.microsof...serialport.aspx
Was This Post Helpful? 0
  • +
  • -

#9 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:15 PM

View PostjimmyBo, on 19 December 2011 - 11:12 PM, said:

Look at the MSDN docs on it:
http://msdn.microsof...serialport.aspx

Ok I've been through these and haven't found anything that shows how to fix my problem. But maybe I'll find something now.
Was This Post Helpful? 0
  • +
  • -

#10 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:20 PM

That's why I said loops. But now I don't think you have adequately explained your problem.
What data should you be getting, and what are you actually getting?
Was This Post Helpful? 0
  • +
  • -

#11 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:26 PM

View PostjimmyBo, on 19 December 2011 - 11:20 PM, said:

That's why I said loops. But now I don't think you have adequately explained your problem.
What data should you be getting, and what are you actually getting?

I should be getting a message from my Arduino saying which case is active, but I thought if I put it as a string then wrote it to the serial port then have my program read strings from the serial port it would work. But I don't think I have it setup right but I'm not sure what's wrong.
Was This Post Helpful? 0
  • +
  • -

#12 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:35 PM

You could use SerialPort.ReadLine, or ReadExisting - this last one reads all available data in the stream, the first one reads a line. There is also ReadTo which reads up till it reaches a specified value detected in the stream.
Was This Post Helpful? 1
  • +
  • -

#13 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:48 PM

View PostjimmyBo, on 19 December 2011 - 11:35 PM, said:

You could use SerialPort.ReadLine, or ReadExisting - this last one reads all available data in the stream, the first one reads a line. There is also ReadTo which reads up till it reaches a specified value detected in the stream.

 Private Sub serialRead()
        TextBox1.Text = SerialPort1.ReadExisting()
    End Sub

Thats what I put and i get case1case2case3case4 and it does that and thats not what i wanted.
then i put
   Private Sub serialRead()
        TextBox1.Text = SerialPort1.ReadChar()
    End Sub

And it gives me ascii characters everytime i press the button. thatd be fine cause i can decode that but id want it all on the same line. So now I'm stuck again.
Was This Post Helpful? 0
  • +
  • -

#14 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 09:53 PM

OK - use ReadLine instead. I mean, it's worth a shot and I can not actually try this because I am on a phone.
Was This Post Helpful? 1
  • +
  • -

#15 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 10:05 PM

View PostjimmyBo, on 19 December 2011 - 11:53 PM, said:

OK - use ReadLine instead. I mean, it's worth a shot and I can not actually try this because I am on a phone.

I tried Readline but 1) I dont think I set it up correctlt, 2) when I used it the program froze but taskmngr said it was responding correctly.

Ill work on this again tomorow after some sleep maybe.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2