Go Down

Topic: Reading SerialPort with C# (Read 55489 times) previous topic - next topic

HugoSanta

Hello guys!
I'm trying to use a potentiometer for monitoring the voltage in the analog inputs. I've made and interface in C# and I can show the values of the converter between 0-1023 that right!
The problem is that I want to show in a label the value of the actual voltage in the analog input. Where 0 is equal to 0 volts and 1023 is equal to 5 volts. When I read the data form serial port is a string and when I show it in a label there's no porbem cause it is already a STRING. However, when I want to convert it onto an integer for manage tthe value to do the operation of convertion into a value of VOLTS C# launch an error.
I don't now why and I'd like some help you! :)

ARDUINO CODE:
Code: [Select]
void setup() {
 Serial.begin(9600);
}

void loop() {
 int sensorValue = analogRead(A0);
 Serial.println(sensorValue, DEC);
 delay(200);
}


C# CODE:
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports; //PERMITE EL USO DE LOS PUERTOS.

namespace PWM_LED
{
   public partial class Form1 : Form
   {
        public Form1()
       {
           InitializeComponent();
           serialPort1.Open();
       }
       

       private void timer1_Tick(object sender, EventArgs e)
       {
           string POT = serialPort1.ReadExisting(); //changing between 0-1023
           label3.Text = POT;
           int VALUE = Convert.ToInt32(POT); // convert to int for making operations.
           label5.Text = Convert.ToString(VALUE * (5/1023)); //shows the voltage value
           //pot=1023 the voltage showed is 5, pot=0 the voltage showed is 0.
       }




   }
}

PaulS

Quote
C# launch an error.

What error?

Does
Code: [Select]
 int VALUE = Convert.ToInt32(POT); // convert to int for making operations.
 VALUE *= 5.0;
 VALUE /= 1023.0;      
 label5.Text = VALUE.ToString(); //shows the voltage value

work any better?
The art of getting good answers lies in asking good questions.

HugoSanta

Hi PaulS

I tried your code and it launches the same as mine.
The warning is :

Code: [Select]
FormatException was unhandled.
Input string was not in a correct format.

This is the line colored by the C# as a result of the warning

int VALUE=Converto.ToInt32(POT);

PaulS

You want to be using Int32.Parse() to convert a string to an integer.
The art of getting good answers lies in asking good questions.

HugoSanta

I tried your way and C# launched the same messange :S

PaulS

Well, then, clearly the problem is in the string that you are passing to the functions. We can't see what is in the string.
The art of getting good answers lies in asking good questions.

bld

#6
Dec 26, 2010, 05:49 pm Last Edit: Dec 26, 2010, 06:22 pm by bld Reason: 1
This can be done a smarter way instead of the timer

Code: [Select]

namespace Read_serial
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();

           serialPort1.PortName = "COM4";
           serialPort1.BaudRate = 9600;
           serialPort1.DtrEnable = true;
           serialPort1.Open();

           serialPort1.DataReceived += serialPort1_DataReceived;
       }

       private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
       {
           string line = serialPort1.ReadLine();
           this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
       }

       private delegate void LineReceivedEvent(string line);
       private void LineReceived(string line)
       {
           //What to do with the received line here
           label1.Text = line;

           progressBar1.Value = int.Parse(line);
       }
   }
}

With this it will execute the serialPort1_DataReceived when it received something from the port, just like the interrupt would do. :)

And if you just send a value to it (0-255) it will set the progressBar1 to that value...

Another thing I noticed from making my RF stuff and reading the serial there, is that when you start the program, it resets the arduino, when it starts up it can result in some garbage received by the C# program, to take care of that I put a "startup" message in the setup() and then "print|value|value|value" in the message that should be processed by the C#, all I then do is to check if the first block is "print" and if it is, then look at the rest of the message.

http://bld.is-a-geek.com/2010/12/26/read-serial-from-an-arduino/
captain-slow.dk | non contagious!

HugoSanta

Hello guy!
I'm going to try your way. However the main problem is to convert the string to an integer for handling the value and shows the VOLTAGE in a label.


Any ideas?

PaulS

Quote
However the main problem is to convert the string to an integer for handling the value and shows the VOLTAGE in a label.

The first step is to receive the string, and print it out. Since you are having trouble converting the string to an integer, the problem must be that the string does not contain just numeric characters.
The art of getting good answers lies in asking good questions.

bld

Quote
Any ideas?

Yes, as I already said in my last reply

Quote
Another thing I noticed from making my RF stuff and reading the serial there, is that when you start the program, it resets the arduino, when it starts up it can result in some garbage received by the C# program, to take care of that I put a "startup" message in the setup() and then "print|value|value|value" in the message that should be processed by the C#, all I then do is to check if the first block is "print" and if it is, then look at the rest of the message.
captain-slow.dk | non contagious!

HugoSanta

Hey guyss!!
I got it!
Thanks a lot for your time and ideasss :)
Here's that I do.

Aquí les dejo el código.

ARDUINO:
Code: [Select]
void setup()
{
 Serial.begin(9600);
 
}
void loop()
{
 int value=map(analogRead(0),0,1023,0,255);
 Serial.println(value);
 delay(250);
}


CODIGO C#:
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Read_serial
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
           serialPort1.Open();
           serialPort1.DataReceived += serialPort1_DataReceived;        
       }


       private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
       {
           string POT = serialPort1.ReadLine();
           this.BeginInvoke(new LineReceivedEvent(LineReceived), POT);
       }

       private delegate void LineReceivedEvent(string POT);

       private void LineReceived(string POT)
       {
           //What to do with the received line here
           int voltaje; double aux;
           voltaje = int.Parse(POT);
           label1.Text = POT;
           aux = voltaje * (5.0 / 255.0);
           string valor = Convert.ToString(aux);
           try
           {
               string valor2dec = valor.Substring(0, 4);
               label2.Text = valor2dec + " v";
           }
           catch
           {
               label2.Text = valor+".00 v";
           }
               progressBar1.Value = voltaje;
         
       }
   }
}

Go Up