Pages: [1] 2   Go Down
Author Topic: Sending a variable over serial to arduino.  (Read 1100 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25782
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25782
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

basic echo:

i send a "2"
Code:
LDCommPort.AvailablePorts()
LDCommPort.OpenPort (3,9600)
LDCommPort.TXByte  (2)
TextWindow.WriteLine (LDCommPort .RXByte () )

i receive a 50

Code:

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

}


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

}

else{
}

}



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25782
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

'2' == 0x32 == 5010
Logged

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 04, 2012, 02:43:14 pm by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25782
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: