Go Down

Topic: Send more than a byte on USB (Read 2283 times) previous topic - next topic

aalex

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
Alexandre Quessy

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.

splitting up values bigger than 255 into two bytes:

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


beltran

#3
Mar 16, 2006, 12:08 pm Last Edit: Mar 16, 2006, 12:11 pm by beltran Reason: 1
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.

tomek

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

aalex

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. :-)
Alexandre Quessy

beltran

#6
Apr 01, 2006, 11:54 am Last Edit: Apr 01, 2006, 11:55 am by beltran Reason: 1
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.


bod

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.  :(

beltran

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: [Select]
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: [Select]
//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: [Select]
//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.

Go Up