Pages: [1]   Go Down
Author Topic: Send more than a byte on USB  (Read 2065 times)
0 Members and 1 Guest are viewing this topic.
Montréal, Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Alexandre Quessy
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I need to send more than a byte to my computer via USB. My Arduino is interfaced to Pure Data. There are 2 reasons why I would need more than a byte to store data :

  • I need selectors : I am gonne route the informations if it is this or that sensor that sends it
  • I need more precision, especially that the analog pins are 10 bits !

So, I thought I would define 0x0000 as a EOF long code. The first char that wouldn't be 0x0 would be a message, but then, what if I want to send 0? This issue must have been discussed somwhere else. Thanks,

aalex
Logged

Alexandre Quessy

Bremen, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I´m trying to achieve the same, but have not found a perfect solution so far..

To send values bigger than 255 over the serial connection, it might be best to write the big value into several bytes (i believe printInteger() does that, but i´m not quite sure) on the receiver side you could use this function to combine two bytes, converting them back to an integer:

(myByte1 * 256) + myByte2 = someInt

(more info (but in german) can be found here: http://www.mikrocontroller.net/articles/Digitaltechnik#Endianness)

finding out which is the first byte and making sure all values are encoded into two bytes is still an issue and i´m sure there´s more elegant ways to solve this, but maybe it helps someone anyways.
Logged

Bremen, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

splitting up values bigger than 255 into two bytes:

http://www.tigoe.net/pcomp/code/archives/arduino/000744.shtml

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure if you really need this,
but If you need to send strings
here you have some code I wrote for Arduino
http://www.progetto25zero1.com/b/tools/Arduino

the serialCom_handeling_multiple_bytes example should also be among the examples available with Arduino0003

b.
« Last Edit: March 16, 2006, 06:11:35 am by beltran » Logged

malmö / berlin
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
fhp>--vs--<k3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

mhhh, i don*t get your problem, why does printInteger not work ???
Logged

Montréal, Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Alexandre Quessy
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, it's about sending almost strings, knowing where is the beginning, where is the end. Hmm, yeah, the delay(10) is an option. Also, this is an option too :

void printNewLine() {
  printByte(13);
  printByte(10);
}

I mean almost string because I want to send (and receive !!) :
 * an int as a selector
 * let say a double (two bytes) would be ok
 * an end of line (or a pause)

There must be a universal way to do this in the microcontrollers' world. :-)
Logged

Alexandre Quessy

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hey sorry for asking
but did you check my examples????
http://www.progetto25zero1.com/b/tools/Arduino

in the serialCom_handeling_multiple_bytes_04 you can find 2 reusable functions
printSerialString() and readSerialString()
that to me seem to be exactly what you are looking for.

you can then change the code to insert special chars at the end or in between strings to make your own protocol.

if you can't understand the code there I could explain it a bit better..
just tell me

cheers
b.

« Last Edit: April 01, 2006, 04:55:08 am by beltran » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
I love YaBB 1G - SP1!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i've been trying to incorporate this readserialstring function to the MAX7219 code, so i can send a string of bytes from processing, to arduino to output to an led matrix, but so far i've had no luck.

do you have any tips on this? i'll post what code i have done when i get home, stuck in work just now.  smiley-sad
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, I'll try to explain you the concept part..if you then tell me more precisely what you really need I can help in the details of twisting this function to your own needs.

1st - declaration of the array
Code:
char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;
                     // -> you must state how long the array will be else it won't work properly
this is important to store long information coming from the serial buffer all in one variable.
if you know the length of your string you can change the number 100 with watever suits you best:
you said that you need 2 bytes = 2x8bit = 16 bits + the endofline or a separator chars
just put that number instead of 100
BEWARE of the type declaration, here the array is declared as a "char", but you can make it an int if you want just numbers to be stored in the array

2nd - the read string function
Code:
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
    int i = 0;                         // the progressive array index so that every bit is stored in another index of the array
    if(serialAvailable()) {             // this allows to execute the function only if there really is data in the serial buffer
       while (serialAvailable()){       // we've cheched that there is data in the buffer...just loop through it untill there are bits in it
          strArray[i] = serialRead();       // store in the array declared at the beginning at the specified index the first available bit of the serial buffer... when you read it the value will be automatically removed from the serial buffer
          i++;                   //increment the index counter so that the next value will be stored in the next array index
       }                         //return to the while loop
        strArray[i] = 13             //optional: now the while loop is finished, hence there is no more data in the serial buffer. you can now eventually add by hand some bits to your array. this is how you create your own protocol. store a carriage return or end of line or whatever else.
    }      
}
this is a stripped down version of example number 4 but you can also refer to the previous examples, maybe example 1, for the most simple integration of the same concept in the loop function


3rd - usage in the loop function
Code:
//read the serial port and create a string out of what you read
  readSerialString(serInString);
- just put into your loop in the moment when you know that there might be info in your serial buffer
- pass it the name of the array declared at the beginning
and the trick is done!
right after that line your "serInString" will be loaded with all the needed data... (only if there actually was data in the serial buffer)

hope this helps..
if you tell us exactly what your string should look like I might be able to help in reusing the function for your pourposes.
b.
Logged

Pages: [1]   Go Up
Jump to: