Hi,
I would like to use my Arduino Mega2560 to communicate with an inertial measurement unit CHR-6d. The CHR-6d communicates over a TTL UART at 115200 baud. Therefore its connected to TX1, RX1, 5V and GND. TX0 and RX0 are used for serial communication with the PC.
CHR-6d RX packet structure:
- first three bytes contain 's' 'n' 'p' – this is the start sequence
- byte four specifies the packet type (PT)
- byte five specifies the number of data bytes to expect (N)
- byte N+6 and N+7 contains two-byte checksum
To set the sensor into silent mode I have to send: 73:6E:70:83:00:01:D4. This works while using a software terminal like Hterm. But I can't figure out how to send this sequence using the Mega.
Serial1.write(73:6E:70:83:00:01:D4)
... that's not working. Is it necessary to send bytes one by one?
Serial1.write(0x73);
Serial1.write(0x6E);
Serial1.write(0x70);
Serial1.write(0x83);
Serial1.write((byte)0x00);
Serial1.write(0x01);
Serial1.write(0xD4);
... but this don't work as well.
I would appreciate any help! Alex
programm code follows:
int pos;
char buffer[128];
int inByte = 0;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
Serial1.write(0x73);
Serial1.write(0x6E);
Serial1.write(0x70);
Serial1.write(0x83);
Serial1.write((byte)0x00);
Serial1.write(0x01);
Serial1.write(0xD4);
pos = 0;
for (int i = 0; i < sizeof(buffer); i++)
buffer[i] = 0;
}
void loop()
{
if (Serial.available() > 0)
{
inByte = Serial.read();
Serial1.write(inByte);
}
char c;
if (Serial1.available() > 0) {
c = Serial1.read();
if (c == 's') {
buffer[pos] = 0;
Serial.println(buffer);
pos = 0;
}
else {
buffer[pos] = c;
pos++;
if (pos >= sizeof(buffer))
pos = 0;
}
}
}