Serial Communication between Mega2560 & CHR-6d

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;
    }
  }
}

Is it possible that the packet has to be finished with a line feed/carriage return or both?

The sequence of Serial1.write()s is doing what you describe as the packet structure. How do you send it in Hterm? Do you send the hex code string as written in the post? Do you send single bytes using a hex code entry method?

If the former is true, your code might be like this:

Serial1.println(F("73:6E:70:83:00:01:D4"));

Hey pylon,

thanks for your reply.

Is it possible that the packet has to be finished with a line feed/carriage return or both?

I don't think so. The Sensor (CHR-6d) will recognize the packet length from byte five. When using HTerm I'm not sending line feed or carriage return as well. I'm just sending let's say 73 6E 70 83 00 01 D4 in hex code entry method for setting the silent mode. And it works just perfect.

Serial1.println(F("73:6E:70:83:00:01:D4"));

I've tried this and it seems to work, but now I'm receiving a packet from sensor which contains "buffer_overflow". Meaning the internal receive buffer of the CHR-6d overflows before receiving a full packet. I'm not sure, but should I synchronize Sensor's reading and Mega's writing somehow?

Alex

whole programm code follows:

int inByte = 0;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  delay(5000);
  //Serial1.println(F("73 6E 70 01 00 01 52"));
  //Serial1.println(F("0x730x6E0x700x830x000x010xD4"));
  Serial1.println(F("73 6E 70 83 00 01 D4"));
 }

void loop()
{
  if (Serial.available() > 0)
  {
    inByte = Serial.read();
    Serial1.write(inByte);
  }
 
  if (Serial1.available() > 0)
  {
    inByte = Serial1.read();
    Serial.write(inByte);
  }
   
  
}

I've tried this and it seems to work, but now I'm receiving a packet from sensor which contains "buffer_overflow". Meaning the internal receive buffer of the CHR-6d overflows before receiving a full packet. I'm not sure, but should I synchronize Sensor's reading and Mega's writing somehow?

If the packet size is limited to 5 bytes (+2 checksum) I understand that the buffer is overflowing with 20 characters sent in the string.

I would try it this way:

uint8_t sleep_code[] = {0x73, 0x6E, 0x70, 0x83, 0x00, 0x01, 0xD4};
for (uint8_t n = 0; n < 7; n++) {
  Serial1.write(sleep_code[n]);
}

This is very similar to your first version but it's always a uin8_t type, so you know what exactly is transferred.

Does the datasheet specify any timing information? Do you have a link to the datasheet?