hi everyone, i have a java program writing to the serial monitor through the jssc lib a byte value, after reading the serial input basics by robin, and basically copying his example of reading binary data, i have an output format that looks like this <00100> 8 times, now the problem is parsing this data to a byte so i can output it using a shift register, but i have some difficulties with it, basically, i'm saving the recieved bits in a char array, but i can't convert it to a byte value, basically if the each index of the char array read something like 00100, i want to put this value into a byte, i'm using the atoi function but the outputByte outputs the binary value of the integer insted of just storing the value as is, i feel like i'm missing something obvious, i would really aprreciate if someone can guide to the right direction because i'm just newbie.
so you are sending, in ASCII, with '<' as a start marker and '>' as an end marker and your byte is only 5 bits long and you do so 8 times in a row
correct?
what value then do you need to reconstruct? the 8 bytes (with only 5 bits each) or something else? (sounds like a character definition for 5x8 char matrix)
FYI: this version supports any number of bits up to 8 assuming less than 8 bits go in the least significant bits of the output byte. Also, you should probably check to make sure the input characters are only '0' and '1'.
the char buffer is not necessary, you could build the end result byte (or larger) as you go (shift left, add the corresponding bit to the char received)
totally untested, typed here... so mind the bugs and typos but you could try something like this
byte received[8]; // we wait for a sequence of 8 "<binary_byte>" coming through serial.
void printReceived() {
Serial.print(F("Received: "));
for (byte b = 0; b < sizeof received; b++) {
Serial.print(F("0b")); Serial.print(received[b], BIN); Serial.write(' ');
}
Serial.println();
}
bool getData() {
static bool incomingByte = false;
static byte currentByte = 0;
int r = Serial.read(); // -1 if there is nothing to read
switch (r) {
case '<': incomingByte = true; received[currentByte] = 0; break;
case '>': incomingByte = false; currentByte++; break;
case '0':
if (incomingByte) received[currentByte] <<= 1;
break;
case '1':
if (incomingByte) {
received[currentByte] <<= 1;
received[currentByte] |= 1;
}
break;
default: break; // ignore everything else
}
if (currentByte >= sizeof received) {
currentByte = 0;
return true;
}
return false;
}
void setup() {
Serial.begin(9600);
}
void loop() {
if (getData()) { // we received the 8 elements
printReceived();
}
}
There is no input error checking, once you have received the '<' whatever comes next if it's 0 or 1 will be added in a byte after shifting the content left but that won't crash (no overflow, the byte just keep accumulating and extra bits get dropped). Once you get the '>' then you are done for that byte and ready for the next one.
Once you got 8 sequences , getData() returns true and the 8 sequences are printed and you are ready for the next 8 ones
yep it worked as expected! thank you so much for your time, i learned a few new things along the way, it didn't came to mind to just simply shift and add the byte, thanks again!