Hello everyone. I have a problem, with which I am afraid I won't deal on my own. will appreciate any suggestions or help.
I am using Arduino Micro with HC-05 module. It's used as remote driver for four DC motors. To handle that I am generating on source device (Android smartphone) 4-byte message, which should transfer with first two bytes a channel number, and in latter two bytes a power, that should be delivered to the selected channel.
I've started the project with default hc-05 baud rate - 9600, where everything was working well, using following code to get messages decoded:
//Serial1 is hardware port to which I have HC-05 connected
if(Serial1.available()){
byte msg[4];
int bytesRead = Serial1.readBytes(msg,4); //sender is emitting only 4-byte messages so I guess I should always get valid set of bytes
int channel = word(msg[0],msg[1]); //read PWM channel
int power = word(msg[2],msg[3]); // read power: <0,4095>
//output for debugging
Serial.print(channel);
Serial.print(":");
Serial.print(power);
Serial.print("\n");
}
In general it should return numbers like:
0:0
4:0
8:0
12:0
0:345
4:1034
8:800
12:2500
etc.
Unfortunately it turned out that desired frequency of channel updates exceeds 9600 baud rate, so I modified HC-05 to use 115200, and updated my code accordingly.
Then I got into troubles, because data I started to get from HC-05 module is really messed up. Generally it looks like I am not getting bytes sent from source device in order in which they were sent. Few first messages are decoded correctly, then it starts to look like total mess with unexpected results.
I saw few topics discussing similar behavior, but none of them delivered answer to questions what causes such issues, as well as any fix more sophisticated than lowering baud rate to default. I think I might be able to implement software signal buffering and retrieve right order of bytes on my own, but I believe it would be silly to do something like that manually once I have hardware UART interface, right?