We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Array of bits, not bytes + serial with Processing
Page Index Toggle Pages: 1
Array of bits, not bytes + serial with Processing (Read 1409 times)
Array of bits, not bytes + serial with Processing
Mar 18th, 2008, 10:35pm
 
Hi there,

I try to figure out how I can send an array of 0's and 1's from Processing to Wiring with the serial library. What I want is to send 32 bits in an array and parse those bits in Wiring. I got it to work with sending those bits as bytes in Processing from a array with bytes (byte[] = {1,0,0,1,0,1,0,1,0,0,0,0,1,1,0,1,0,0,1,1,1,0,1,0,0,1,1,1,1,0,0,1};). For my application this takes to long, because Processing sends only about five of those arrays in one second (5x32 bytes = 160 bytes * 8 = 1280 bits). I dont'know why it can't communicate faster, even if I try to set the baudrate at 19200 in both Wiring and Processing program.

Anyway... It is not efficient to send bytes for values that can be send in bits. Is it possible to send bits through the serial port with Processing? The serial library only writes these datatypes: byte, char, int, bytes[], String. Not bits. I looked up if Wiring can read bits, but that seems not to be the case either. I looked in some C- and  
Java-references and read about bitsets. These are arrays with bits.  

Does anyone know how to implement them in Processing/Wiring? Of even know if this is possible at all? Help is really really appreciated!

Thank you in advance.
Re: Array of bits, not bytes + serial with Process
Reply #1 - Mar 19th, 2008, 12:14am
 
You could load the binary into ints using bitwise operations

Code:

String binary_in = "11110000111100001111000011110000";
int binary_holder=0;
void setup(){
// you can do this with the unbinary() function
// - I'm just demonstrating the bitwise operation
for(int i = 0; i < binary_in.length(); i++){
if(binary_in.charAt(i) == '1') binary_holder |= 1<<i;
}
// Now to get the information out of the int - note you can only
// do the binary in chunks of 32
for(int i = 0; i < 32; i++){
if((binary_holder & (1<<i)) > 0) print("1"); else print("0");
}
}


All I'm doing in the above code is turning the binary on and off in the int datatype and reading it again.

You can find more examples which explain this much more clearly on the internet - just look up "bitwise" and "C++" or "Java" (and the word "tutorial" if you need it to be really basic).

Not only is bitwise stuff very fast, it also uses less memory. And instead of an array of 32 bytes, you're sending 1 int.
Re: Array of bits, not bytes + serial with Process
Reply #2 - Mar 19th, 2008, 12:26am
 
hi, i don't understand why you want to send it bit by bit. i mean, wouldn't it be much more efficient to pack your 32bits into 4 bytes and send them using the write-/readByte methods?

as far as i know there's no datatype Bite in Java. so, your "byte"-array is kinda strange here. I guess you have your "bits" arround as ints? or how do you get your 32bits? i assume you have something like int[0,1,1,...].

i needed something like this recently for image-conversion... so here's some code:

Quote:
// bits to byte
// extrapixel 2008

int[] bits;

void setup()
{
 size(200, 200);
 bits = new int[] {
   1,0,0,1,0,1,0,1    };

}

void draw()
{
 println(binary(bits2byte(bits)));

}

// turns an int-array into a byte
byte bits2byte(int[] bits) {
 byte returnByte = 0x00; // = 00000000
 byte bitMask = 0x01; // = 00000001

   if (bits.length==8) {
   // loop through the 8 bits
   for (int bitNr=7; bitNr>=0; bitNr--) {

     if (bits[bitNr]==1) {
       // search for "java bitwise or" in google ;)
       // basically, if there's a 1 in a bit-slot in the mask, it
       // will be copied into the destination byte.
       // so, if you have byte1: 00000001 and byte2: 10000000
       // and apply a bitwise or
       // byte3 = byte1 | byte2
       // byte3 will be 10000001
       returnByte = (byte) (returnByte | bitMask);
     }

     //println(binary(bitMask));
     // now we shift our mask one bit to the left
     // this results in the following bit-masks
     // 00000001
     // 00000010
     // 00000100
     // 00001000
     // 00010000
     // 00100000
     // 01000000
     // 10000000

     bitMask = (byte) (bitMask << 1) ;

   }
 }

 return returnByte;
}
Re: Array of bits, not bytes + serial with Process
Reply #3 - Apr 17th, 2008, 11:53pm
 
Thank you for this eplaination. I haven't been working with bitshift yet. It looks a little scary at first, but I understand it better and better now. Also I found out that the reason Processing is communicating slow because I was calling serial.wite() for each seperate value in a loop. More of this 'problem' in this topic
Page Index Toggle Pages: 1