Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Integration >> Array of bits, not bytes + serial with Processing
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1205875892

Message started by hype1 on 03/18/08 at 21:31:32

Title: Array of bits, not bytes + serial with Processing
Post by hype1 on 03/18/08 at 21:31:32
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.

Title: Re: Array of bits, not bytes + serial with Process
Post by N201LJ on 06/02/08 at 22:39:34
I'm not too familiar with Processing, but I couldn't see a full range of bitwise operations in the reference.  Could you do something where you accumulate 32-bit integer sums by adding powers of 2?  bit_value * pow(2,n) where n=0-32.  Then send the entire chunk at once?

Title: Re: Array of bits, not bytes + serial with Process
Post by barragan on 06/02/08 at 23:59:52
hi, could you please post the minimum code for both Processing and Wiring to have an idea of what´s going on?

Title: Re: Array of bits, not bytes + serial with Process
Post by hype1 on 06/03/08 at 08:32:36
Hi,

I haven't got the code with me now. What I try to achieve is to send an array with the length of 32 numbers, where each number could be the value 1 or 0, so in essence they could be bits. Processing is only capable of sending integers or bytes. To send 32 bytes where only 32 bits are needed is offcourse not efficient. I am not really into bitshifting yet. I understand that will be the most logical thing to do, to pack those 1's and 0's in only 4 bytes.

But my device is working fast enough now, even if I send 32 bytes instead of 32 bits. The problem I encountered was that my communication from Processing was too slow. This was because I was sending those bytes seperately with for each byte a new serial.write call, for example:

serial = new Serial(this, Serial.list()[1], 19200);

byte[] myArray = {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 (i=0; i<myArray.length; i++){
    serial.write(myArray[i]);
}

I found out that the next code is a whole lot faster. Instead of sending those value one after the other, I send the full array. Like this:

serial = new Serial(this, Serial.list()[1], 19200);

byte[] myArray = {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};

serial.write(myArray);

Apparantly It takes Processing a while to initiate a serial.write call. This is being discussed on the Processing forums too (link1 link2).

Have you got any idea why this makes such a diffence?

Regards,

Jeroen

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.