Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here's what I'm trying to do:

C# code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    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)
        {
            //int i = serialPort1.ReadByte();
            //string val = Convert.ToString(i);
            string val = serialPort1.ReadLine();
            int i = Convert.ToInt32(val); ;
            this.BeginInvoke(new LineReceivedEvent(LineReceived), val);
        }
        private delegate void LineReceivedEvent(string val);
        private void LineReceived(string val)
       {
           label1.Text = val;
           //int valu = Int32.Parse(val);

       }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


the Arduino code:

C++
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
int LED=4;
int Ent=A1;
boolean a=false;
int threshold=8;
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
}
void loop() {
  // read the analog in value:
  sensorValue = analogRead(Ent);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:

Serial.print("\n ");
Serial.print(outputValue);

}


What I have tried:

I have a potentiometer plugged to an Arduino, and I want to send the value to Visual Studio, and make it appear on the interface as label text.
After that, I want to make an if condition to make a decision based on the value I obtained from the Arduino.

For that, I need to read the value from the Serial Port as a string to show it on the interface, and then convert it into an integer in order to use it in the if condition.
If I read the value from the Serial Port as a string, it shows the correct value as obtained by the Arduino software, but when I convert it to an integer I get an error message "Input string was not in a correct format".

I tried to read the value from the Serial Port as integer and then convert it to String, this works with no errors, but it displays the wrong value comparing to that shown by the Arduino software.
Any help please ?
Posted
Updated 12-Feb-21 21:43pm
v2
Comments
Ralf Meier 12-Feb-21 18:19pm    
OK ...
How does your String-Value look like when you read it from the serialport ?
How does your Interger-Value look like when you read it directly from the Serialport ? What is the difference between that value and the one which is send by the Arduino ? ... and what does the Arduimno send ?
You see - that is to less Information you gave to us ...?

1 solution

Hi,

strings don't care about their content.
When converting a string to a number using the Convert class or the Parse/TryParse methods of numeric types, then such method expects an exact number, no more, no less.

And you have
Serial.print("\n ");
Serial.print(outputValue);

which clearly contains a space, something conversion methods do not understand.

I suggest you:
(1) change your arduino code to not include a space, and to send the newline AFTER te value, not before;
(2) and then to change your C# code to use int.TryParse (or int.Parse inside a try-catch block), so you can survive a badly formed number, which may occur when something goes wrong in the data transmission, something I/O should always allow for.

Sending \n after the value is relevant for the first value (your receiver currently gets an empty line when the system starts up); and it makes sure the last value sent (assuming your peripheral might be modified later to make it shut up at some point) is also received.

:)
 
Share this answer
 

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