Go Down

Topic: Sending a variable over serial to arduino. (Read 1 time) previous topic - next topic

adilmalik

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:

Code: [Select]

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:

Code: [Select]

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?

PaulS

Quote
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.

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

adilmalik

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

James C4S

"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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Wolfiesden



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.

adilmalik

Quote

"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 :/

Quote

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.


adilmalik

Quote

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.

AWOL

Don't tell us what you don't receive, tell us what you do receive.
And what you send, and how.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Delta_G


Quote

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.

adilmalik

basic echo:

i send a "2"
Code: [Select]

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


i receive a 50

Code: [Select]


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

}


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

}

else{
}

}





AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

James C4S

#12
Jul 04, 2012, 09:40 pm Last Edit: Jul 04, 2012, 09:43 pm by James C4S Reason: 1
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
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

AWOL

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

No.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

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

The character 2 has an ASCII value of 50.

Go Up