Hi everyone,
I’m stuck on this issue on trying to obtain 3 bytes of data from the UART.
Setup goes like this: Arduino Mega Software Serial -> Electric Imp (imp001)
I’m sending one byte whose bits will represent indicators and the other two will be the upper/lower byte of a 10-bit ADC value as I want the full 10-bit resolution.
UART on the imp and software serial on the Mega both run at 1200 baud (I’ve also tried 9600).
The Mega will send the 3 bytes, here is the snippet:
byte payload[3] = {indicatorByte, LowerByte, UpperByte}; impSerial.write(payload, sizeof(payload)); // in form (array, length of buffer)
Now on the Imp device side I want to read each byte and place them in a blob, which I send the whole blob to a function in the agent side that will parse the bytes and concatenate LowerByte and UpperByte.
Here is the snippet that reads the data coming in (DEVICE):
//Counter to obtain the 3 bytes of payload payload_counter <- 0; //Blob of binary payload data 3 bytes large payloadBlob <- blob(3);
.
.
.
`
function serialRead() {
local data = arduino.read();
//server.log(data);
if(payload_counter < 3)
{
payloadBlob.writen(data, ‘b’);
payload_counter++;
}
else
{
payload_counter = 0;
if(payloadBlob.eos() != null) // is pointer at the end of the blob
payloadBlob.seek(0, ‘b’); // reset pointer to beginning of blob
agent.send("parseBlob", payloadBlob);
}
}
. . .
arduino.configure(1200, 8, PARITY_NONE, 1, NO_CTSRTS, serialRead);
`
Here is the code snippet that will parse the results (AGENT):
`
function parsePayload(payload)
{
local status = payload[0];
local LowerByte = payload[1];
local UpperByte = payload[2];
statusFunction(status);
local value10bit = ((UpperByte << 8) | LowerByte);
status10bit(value10bit);
}
`
This was one method I have tried and I’ve tried a few other ways, but the data keeps on getting out of sync.
I tried having flag variables before each set of data, but that still didn’t work.
If anyone has any tips and solutions, it would be appreciated!
Thanks