Vs08 c++ receiving messages from arduino

I wish you a good day people. I'd like to aska question concerning comminucation between arduino and VS2008 c++.

I started making a project as it is indicated in playground as a simple window forms app. I've imported serialPort object and made a small program where arduino lights LEDs up when it receives letters a, b, c. Nothing fancy but i'm just a starter.

Then i tried to make my VS app listen to what arduino has to say and encountered a series of problems. At the end i menaged to get some data to VS off of arduino board but data is fragmented...

I'm using example file AnalogInOutSerial which reads the state of potentiometer and dims the diode using PWM, sending scaled data back through the USB. When i open the serial monitor data lokks like this:

sensor = 0 output = 0

but then i try to read data to the visual studio i get fragmented words and numbers... sometimes i get it right but that is a coincidence i guess. i get combination od first two words i get sensor with two ss i get just the end of the word :S This is my code for receiving data it is activated with a windows button

this->serialPort1->PortName = "COM3";
this->serialPort1->Open();
String^ message = this->serialPort1->ReadLine();
this->serialPort1->Close();
this->label1->Text = message;

What does your sending code look like? The ReadLine() function is reading up to the carriage return. Are you sending a carriage return?

In the VB app, where have you set the baud rate for the serial port? Does it match the baud rate of the Arduino?

Are you aware that opening (and closing) a serial connection causes the Arduino to reset?

this is the program i used for sending the data from the board so this is what i am sending (copied from the example)

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

I'm pretty new to this kind of programming so i might have done

some other stupid mistake :S it is probably made for serial monitor (which btw displays the code correctly)

and about reseting the port i didn't know about that. I made a program where leds are turned on and off using VS2008 and i opened and closed ports before and after using and i leds didn't go down :S

as the boeard is sending the signals constantly(in a loop) it seems like my VS program doesn't know then to start, or where is the beggining so it reads from the middle or sth :S

i should get sth like this:

sensor = 0 output = 0

and i get it at serial monitor but my program receives:

0
output =

or

nsor =
0
ou

and so on. i'm really puzzled with this. my baud rate is 9600 as default.

The code on the Arduino is made for writing to the serial port. Since it displays output correctly in the Serial Monitor, and IS printing the trailing carriage return, the problem is not with the Arduino.

It is, therefore, in your C++ application. Your C++ application needs to have a button that opens the serial port, and another to close it (or close it in the FormClosing event). Then, the send data button callback should not open and close the serial port.

Try making this change, and see if that results in better reception of the data.

Thanks Paul that actually helped. i've made a separate control to open and close the port, and a simple handshake method. My program sends 'rrq'(read request) string and when the board receives it it sends back the values obtained from the sensor. The data is received perfectly but another problem spung up :S

i added another command on the same window form which sends wrq(write request) string and after it number 225. wrq was supposed to be a string which selects what should the board do and 225 is pwm duty cycle. this is my sending code:

function that reads sensors:

            this->serialPort1->Write( "rrq" );
            String^ message = this->serialPort1->ReadLine();
            this->label1->Text = message;
            message = this->serialPort1->ReadLine();
            this->label2->Text = message;

function that sends wrq and pwm dc:

            this->serialPort1->Write( "wrq" );
            this->serialPort1->Write("255");

i have two versions of code on ard board one with the switch case, the other with two if functions:

switch case:

  if (Serial.available() > 0) {
    inByte = Serial.read();
    switch (inByte)
    {
      case 'rrq':
        firstSensor = analogRead(A0);
        secondSensor = firstSensor/4;
        Serial.println(firstSensor);
        Serial.println(secondSensor);              
      break;
      case 'a':
        info=Serial.read();   
        analogWrite(9,info);
        delay(1000);
        analogWrite(9,0);
      break;
  }

two ifs

    inByte = Serial.read();
    if (inByte = 'rrq')
    {
      firstSensor = analogRead(A0);
      delay(10);
      secondSensor = firstSensor/4;
      Serial.println(firstSensor);
      Serial.println(secondSensor);              
    }
    if (inByte = 'wrq')
    {
      info=Serial.read();   
      analogWrite(10,info);
      delay(1000);
      analogWrite(10,0); 
    }

when i put switch case wrq function works well but rrq function blocks when i try to run it. i've set a breakpoint t check if program receives anything from the board and value of String message is

on the other hand with two ifs rrq function gets the data correctly but it constantly lights up the diode connected to the pwm port. wrq function runs but instead of lighting up diode and shutting it down after one second it lights it uo strond then dimms it then lights it up again. changes in the LED brightness allways occurs when TX LED on board lightens up ut the program code doesn't send anything :S

The Serial.read function returns one character from the serial port. You are then trying to compare that single character to the single character 'rrq' or the single character 'wrq'.

'rrq' and 'wrq' are not single characters.

when i put switch case wrq function works well but rrq function blocks when i try to run it.

Your switch has cases for the single character 'rrq' and the single character 'a'. There is no case for the single character 'wrq', so is is impossible for your "wrq" case to be working.

Again, 'rrq' is not a single character, so it is not possible for the single character that was read to ever match it, so the 'rrq' case is never executed.

The value that would be stored in info if that Serial.read() ever was called, when you send "225" will not be 225. It will be '2' (which is not the same as 2).

You need to read all the serial data sent, store it one character at a time, until the end of a packet is encountered, then determine what to do based on the whole string.

w..r.......q...2.2.........5

where the dots represent a time delay.

The Arduino might make a million passes through loop between when the w arrives and the 5 arrives, depending on your baud rate.

You are not sending any kind of start of packet marker or end of packet markers, so you will have a tough time knowing when a complete packet has arrive.

You have two choices. You could send the data in fixed size packets - always 6 characters, for instance, and not read any serial data until all the data has arrived.

Or, you can add start and end markers to the data.

Then, you need to change the code on the Arduino to read strings. I'm tired of re-typing the code over and over again to do this, so you get to search the forum for a previous post where I explain how to do it. Use the string "started && ended" to do the search.

tnx it helped i got it correctly now