Arduino Serial Communication Char instead of int?


Edit Are there other ways for doing this ( incomingByte = Serial.read();) I mean can I transfer just Integers or are Strings/char possible as well in a way?


I would like the CompactRIO to communicate with an Arduino. I've tested my LV code through a Loopback, it worked this way but not properly with the Arduino setup. A "112233" command turns to 103 103 51 51 102 102 on the arduino. What happened?

I do think there is a problem with the format String Labview - Int arduino I've tried it with a char but, it didn't work with the serial.read(). Is there another option?

int incomingByte = 0; // for incoming serial data

void setup() {
 Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

 // send data only when you receive data:
 if (Serial.available() > 0) {
 // read the incoming byte:
 incomingByte = Serial.read();

 // say what you got:
 Serial.print("I received: ");
 Serial.println(incomingByte, DEC);
                
               
                delay(100);
 }
}

I mean can I transfer just Integers or are Strings/char possible as well in a way?

All that you can transmit is bytes. Whether those bytes are binary data that makes up an int or a float, or are characters that make up a string, which might be a representation of an int or a float, is up to you to determine.

What happened?

How can 112233 possibly be an int? What was sending the data? I'd guess that the sender converted the int to a string, "112233", and you somehow mangled the sending or the receiving. I'd have expected you to receive 51, 51, 52, 52, 53, 53.

What would possess anyone to use a type in a name, where the type of the variable being named does not match the type in the name?

The below code is for servo control but you can try it with your setup to see if the results are more like you desire.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer
    myservo.write(n);
    readString="";
  } 
}