Author Topic: Sending multi digit serial numbers to the arduino.  (Read 7941 times)

0 Members and 1 Guest are viewing this topic.

Offline Mcfly

  • Contributor
  • Posts: 18
  • Country: us
Sending multi digit serial numbers to the arduino.
« on: June 27, 2012, 05:39:15 am »
Im working on my widget, and i need a way to send an 8 bit binary number to the arduino.  I know how to send a single digit number to the arduino, but I'm running into ascii problems and who knows what else.  Has anyone ever done this before, and can point me in the right direction?
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4522
  • Country: au
  • Question Everything... Except This Statement
Re: Sending multi digit serial numbers to the arduino.
« Reply #1 on: June 27, 2012, 07:36:11 am »
when you mean binary, why would ascii be involved? you send it as a byte, arduino accept bytes, its the conversion to decimal or what ever standard you want to follow, if not decimal or hex might require a look up table,
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 2054
  • Country: us
Re: Sending multi digit serial numbers to the arduino.
« Reply #2 on: June 27, 2012, 07:44:24 am »
The easiest, most portable, and most generic way is to convert your number to a decimal ASCII string.  You use sprintf() or the equivalent on your host and use the int() conversion function on the arduino.  If you are using native C on your microcontroller you want the strtod() function.

The only real downside to that is that it isn't very efficient.  A 2 byte word can take up to 6 characters.  This is not a problem for most applications, but if you need high throughput or low latency and are stuck with the slow RS232 port you can do better with binary data.
 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 5940
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: Sending multi digit serial numbers to the arduino.
« Reply #3 on: June 28, 2012, 04:54:28 am »
Just send it on the line. 8 bits (one byte) is the standard "unit" size on RS-232 so you don't need to do any framing. Things get much more complicated when sending sequences of multiple bytes. A good way of framing uses a signal that cannot possibly be a data value. In RS-232, it can be done by sending a "break".
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline electrode

  • Regular Contributor
  • *
  • Posts: 141
  • Country: au
Re: Sending multi digit serial numbers to the arduino.
« Reply #4 on: June 28, 2012, 09:35:52 am »
Not enough info in question. Give us an example of what you're trying, what you want, and what you're getting.

Communication should be trivial. I use pySerial.
 

Offline bsiswoyo

  • Contributor
  • Posts: 7
  • Country: id
    • Bambang Siswoyo
Re: Sending multi digit serial numbers to the arduino.
« Reply #5 on: August 09, 2012, 01:03:52 am »
Im working on my widget, and i need a way to send an 8 bit binary number to the arduino.  I know how to send a single digit number to the arduino, but I'm running into ascii problems and who knows what else.  Has anyone ever done this before, and can point me in the right direction?

please visit my blog about this:

http://bsiswoyo.lecture.ub.ac.id/2012/08/arduino-tutorial-how-to-turn-onoff-8-led-using-visual-basic/

best regard
bambang siswoyo
Bambang Siswoyo
 

jucole

  • Guest
Re: Sending multi digit serial numbers to the arduino.
« Reply #6 on: August 14, 2012, 01:23:54 pm »
Quote
please visit my blog about this:
http://bsiswoyo.lecture.ub.ac.id/2012/08/arduino-tutorial-how-to-turn-onoff-8-led-using-visual-basic/

hi, are you sure the source code is in the file "Source_LEDWidget_Arduino.rar" ?
 

jucole

  • Guest
Re: Sending multi digit serial numbers to the arduino.
« Reply #7 on: August 14, 2012, 01:58:19 pm »
I've never used Arduino only Microchip but a quick Google around and it looks really easy using the Serial.Read like ... "incomingByte = Serial.read();"
Just make sure that if you're using VB6 etc that you're actually sending the number as a byte and not a byte sequence of ascii values.
 

Offline bsiswoyo

  • Contributor
  • Posts: 7
  • Country: id
    • Bambang Siswoyo
Re: Sending multi digit serial numbers to the arduino.
« Reply #8 on: August 17, 2012, 10:59:13 am »
hi jucole.

Yes, there are a setup file and source code freely can be downloaded.
Bambang Siswoyo
 

jucole

  • Guest
Re: Sending multi digit serial numbers to the arduino.
« Reply #9 on: August 17, 2012, 06:50:45 pm »
hi jucole.
Yes, there are a setup file and source code freely can be downloaded.

hi , I see the Arduino code and the vb6 setup exe etc,  but the vb6 code "Form1.frm" is not in the rar file "Source_LEDWidget_Arduino.rar" ?
 

Offline jeremy

  • Frequent Contributor
  • **
  • Posts: 934
  • Country: au
Re: Sending multi digit serial numbers to the arduino.
« Reply #10 on: September 09, 2012, 10:48:05 am »
This is a pretty sneaky problem, I'm sure there is a libc way to do it but I usually just

Code: [Select]
uint32_t get_number(char size) {

uint32_t output_number = 0;

for (char i=0; i<size; i++) {
// get some characters, convert ascii into the actual number, add it in
// note that '0' is 48, the ascii code for the zero character
    output_number += Serial.read() - '0';

    output_number *= 10; // shift everything up by one digit
}
}

This assumes that you send digits in the most significant to least significant order ie if you want to send 1234 you would send '1', '2', '3', '4'. So to get an 8 digit number you would just go

Code: [Select]
uint32_t  num = get_number(8);
But using a 32 bit integer on an 8 bit micro is really inefficient, you should just store it in an array and process it from there.

Code: [Select]
#define NUM_SIZE 8

char buffer[NUM_SIZE];
for (i=0; i<NUM_SIZE; i++) buffer[i] = Serial.read() - '0';

Good luck!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf