Working on binary data

edited November 2016 in Arduino

I am facing some problems sending and using a byte from arduino to processing. My goal would be to make an array of boolean from this byte. If you can, tell me if i am doing something wrong:

In arduino i send a single byte using Serial.write(); In processing I receive using Serial.read();

Problems:

1) looks like that i have a problem of comunication because i reveice much more bytes than i wait. Baudrate is the same and the port is correct.

2) There must be something that i miss because i am forced to use an int to store the value of Serial.read(), why can't i use a byte?

3) i have also problems in working on this byte using the normal | & and ^ operations, same problem as point 2, why am i fprced to use an int to use them? Them are bitwise operations and i would like to use bytes not int!

4) how can i use directly binary data? For example in arduino i use 'B101010' but here seems that i can use only hex

Tagged:

Answers

  • 1) Not much to go on about here. Are you sending the values continuously? What do those other bytes look like? Can you maybe retrieve the bytes you want and ignore the extra?

    2) Serial.read() returns an int. If you need bytes instead you can either convert the int to a byte by casting it:

    byte b = (byte) Serial.read();

    or use Serial.readBytes() or Serial.readBytesUntil(), see: https://www.processing.org/reference/libraries/serial/

    3) Bitwise operations should work on ints

    4) That's similar as in Java. Use the prefix "0b":

    int b = 0b10101010;
    println(b, binary(b));
    

    or if you want to use bytes:

    byte b = (byte) 0b10101010;
    println(b, binary(b));
    
  • edited November 2016

    yes continuously

    thanks for point 4, i was used to arduino's 'B' and i didn't know this formatter about point 3, yes they work good with int but not with byte (i would like to understand why but it is not important for my project)

    arduino code:

    void setup() {
      Serial.begin(115200);
      DDRB |= 0x00;     // pin 8-13 input
      PORTB |= 0x3F;    // pull-up
    }
    
    void loop() {
      byte state = PINB | B1000000;
      //Serial.println(state,BIN);
      Serial.write(state);
      Serial.print(":");
    }
    

    tipical byte i send from it: 1011101 1010101

    processing code that doesn't work at all:

    import processing.serial.*;
    Serial myPort;
    
    
    void setup () {
    
      myPort = new Serial (this, "COM3", 115200);
      //myPort.bufferUntil(':');
    
    }
    
    void serialEvent (Serial myPort) {
      int inSerial = myPort.read();
      println(inSerial);
    }
    
  • I misunderstood your point 3. Hmmm, you're right, that never bothered me before. You can do int c = a|b where a and b are bytes, but c has to be int. If it's really bothering you, you can cast them back to byte.

    In your Arduino code you are using both write() and print(). Write sends a byte but print sends an ASCII string. After each state byte you send it the ":" symbol in bytes (0x3a or 00111010), possibly with some more bytes. Are you just trying to get the state byte or do you want more? If it's just the state byte you're after you could get rid of the Serial.print(":"); statement all together and just read bytes. Otherwise you could do a Serial.print() statement instead of write() and do some string parsing in Arduino and Processing to get the bytes you want.

  • just the state byte nothing more, i thought that i needed the ':' to separate the bytes. as you suggested i removed the Serial.print(":"); but nothing happens :(

    i had already tried to send the byte with the Seria.print(); and it works, but i would like to use a byte

  • edited November 2016

    yeppppp it worked! i was using wrong the redBytes. thanks to both of you! (gotoloop if i m not wrong it is the second time you help me here, thank you!)

    a

    is here a way to fill an array of values? i tried this without good results:
    isLow.fill(isLow[], boolean.false);

  • edited November 2016

    i was trying to use it before asking here but I don't know why it doesn't work

    fill(boolean[]isLow, boolean false);

    isLow is my array

  • edited November 2016 Answer ✓
    import java.util.Arrays;
    
    final boolean[] isLow = new boolean[3];
    println(isLow);
    println();
    
    Arrays.fill(isLow, true);
    println(isLow);
    
    exit();
    
Sign In or Register to comment.