Sending a variable over serial to arduino.

I was messing around with small basic (the easier intro version of vb) and noticed and extension that allow serial communication! Now i want to send number to the arduino via serial port.

the code for small basic is simple:

LDCommPort.AvailablePorts ()
LDCommPort.OpenPort(3,9600) // set up communication
LDCommPort.TXString(1234) //number to send

Now im stumped on how to program the arduino.

I tried a simple program such as:

int X = 0;
void setup()
{
    Serial.begin(9600);
}
void loop()
{
    if (Serial.available() > 0)
    {

        X = Serial.read();
       Serial.println( X);
 delay(1000);
}


 }

but instead of a an output of "1234" it splits it up as 1 then 2 then 3 then 4. How can I receive this as a single integer?

How can I receive this as a single integer?

Save the characters in an array that you keep NULL terminated. When the last character has been received, call atoi() to convert the string to an int.

Of course, you'll see that the tricky part is knowing when the last character has been received.

There are any number of ways to determine that. Since the student failed to do any research before posting, the ways are left as an exercise for the student.

You asked this same question earlier. Why are you posting the same question?

ASCII to integer conversion comes up here all the time; do a quick search, there are lots of ways to skin this particular cat.

Yes its the same question, but i deleted that one as i think i wasnt being clear enough..

“LDCommPort.TXString(1234) //number to send”

Look for another function that writes bytes, instead of something that clearly says “string” in it. This function is sending a string one character at a time, not numbers.

adilmalik: but instead of a an output of "1234" it splits it up as 1 then 2 then 3 then 4. How can I receive this as a single integer?

Start the IDE, go to FILE, SKETCHBOOK, then EXAMPLES. Then look in 04.COMMUNICATION for an example called ReadASCIIString.

There you go.

"LDCommPort.TXString(1234) //number to send"

Look for another function that writes bytes, instead of something that clearly says "string" in it. This function is sending a string one character at a time, not numbers.

There is an option to send bytes at a time. But I actually don't understand the difference :/

Start the IDE, go to FILE, SKETCHBOOK, then EXAMPLES. Then look in 04.COMMUNICATION for an example called ReadASCIIString.

There you go.

Thankyou! ill look into that right away.

Start the IDE, go to FILE, SKETCHBOOK, then EXAMPLES. Then look in 04.COMMUNICATION for an example called ReadASCIIString.

There you go.

I just tried it but it doesnt seem to work. I sent 3 values like : 32,255,123, to the serial port but i dont receive a hex value like the program should send.

Don't tell us what you don't receive, tell us what you do receive. And what you send, and how.

adilmalik:

Start the IDE, go to FILE, SKETCHBOOK, then EXAMPLES. Then look in 04.COMMUNICATION for an example called ReadASCIIString.

There you go.

I just tried it but it doesnt seem to work. I sent 3 values like : 32,255,123, to the serial port but i dont receive a hex value like the program should send.

Computers only know binary. If you want them to convert something and display it as decimal or hexadecimal for your benefit then they will gladly do that. But the information is always sent in binary. Arduino only gets binary from the PC. Arduino has no way of knowing whether you coded that number on the PC side using decimal or hexadecimal or binary or what.

basic echo:

i send a "2"

LDCommPort.AvailablePorts()
LDCommPort.OpenPort (3,9600)
LDCommPort.TXByte  (2)
TextWindow.WriteLine (LDCommPort .RXByte () )

i receive a 50

void setup() {
Serial.begin(9600); // set serial speed

}


void loop(){
if (Serial.available() > 0){
int val = Serial.read() ; 
Serial.print(val);

}

else{
}

}

‘2’ == 0x32 == 5010

Your PC program sent 0x02. Which was stored as an integer on the Arduino. The Arduino then printed the number 2 in ASCII (that's what Serial.print does), which is ASCII 50. Serial.write(val) would send 0x02.

Edit: Oops, got trigger happy with the 0x

the number 2 in ASCII (that's what Serial.print does), which is ASCII 0x50.

No.

the number 2 in ASCII (that's what Serial.print does), which is ASCII 0x50.

The [u]character[/u] 2 has an ASCII value of 50.

Im suddenly getting confused with all this byte, char, string, null =(. Is there something i can read to understand this more thoroughly?

Is there something i can read to understand this more thoroughly?

Practice, practice, practice. Experiment and learn. That's the best way.

Yes thats the way i do it. Ive never studied programming in my life..till now. All i know is from experiments, i wrote a program that generates codes to drive motors, but i cant send the commands to the arduino via serial, thus this post.

Some simple servo test code you can try using the serial monitor (captures serial character string, parses captured string into two new character strings, converts new strings into numbers, …). You might modify this code to verify you can make the motors move via serial commands.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}