ArduCAM / RaspberryPi

This is Arducam camera demos used on Raspberry Pi paltform
BSD 3-Clause "New" or "Revised" License
162 stars 97 forks source link

Issues in Serial read, API bytes Frame data is receving invalid #103

Open Sarac7t opened 8 months ago

Sarac7t commented 8 months ago

In touch panel controller there is a API frame communication listed below... we cant able to receive the defined data, I getting error value while receiving as serial.

  1. UART configuration Baud Rate : 9600 Parity : None Data Bits : 8 Stop Bits : 1

  2. Switch Numbering In the products variants with fan controller, switch (node) number for fan is 1 and rest of non-dimmable node is starting from 2 in horizontal zigzag pattern.

  3. API frame format. In API frame structure following is the fixed definition of any command frame Every first byte of frame is fixed 0x7B (“{” in ASCII, 123 in decimal). Every second byte of frame is ‘command type’ byte, it informs what you need to do with rest of the data. This will act as a frame Identifier in the data received from touch panel (response frame and event frame) Third byte is length of frame. It is 1-byte value (L - 4) where L is total numbers of bytes of whole frame. Second Last byte is checksum. Checksum is a lower byte of addition of whole individual bytes of frame except First byte (Start byte), Second byte (Command type byte), Second Last byte (Checksum byte itself) and Last byte is 0x7D. Last byte is 0x7D, it is End code to indicate the end of frame. (“}” in ASCII, 125 in decimal). For Example, consider following frame.

Table 1.1 Frame example 1. 1st Byte 2nd Byte 3rd Byte 4th Byte 5th Byte 6th Byte 0x7B 0x03 0x02 0x05 0x07 0x7D Start Code Command Length Data Checksum End Code

So the checksum will be lower byte of addition of third byte and fourth byte. 0x02 + 0x05 = 0x07 so we will consider 0x07 as checksum here.

Example 2, consider following frame. Table 1.2 Frame example 2.

1st Byte 2nd Byte 3rd Byte 4th Byte 5th Byte 6th Byte 7th Byte 8th Byte 0x7B 0x52 0x04 0x02 0xFF 0x14 0x19 0x7D Start Code Identifier Length Data Data Data Checksum End Code

In this example 2 the checksum will be lower byte of addition of third to sixth byte. 0x04 + 0x02 + 0xFF + 0x14 = 0x0119 so we will consider 0x19 as checksum here.

  1. Command package to update individual single node.
  2. Command Following is the command to change the status on touch board for individual single node. Table 1.3 Command package for individual single nodes. 1st 2nd 3rd 4th 5th 6th 7th 8th
    0x7B 0x00 0x04 (0x01 to0xNN) (0x00 for ON and0xFF for OFF) (0x00 to 0x64) CHECKSUM 0x7D

*0xNN depends on number of total nodes In this frame 4th byte is the node number, 5th byte is On and OFF status and 6th byte is dimming value. For Fan as well as dimmer, dimming value is in step of 25s. The touch module should respond an acknowledgement package for this frame Table 1. as described in the next table. In case of curtain to open the curtain the value of node will be 0x01 to open the curtain and the value of node will be 0x02 to close the curtain. In both the case the value of status is 0x00. To stop the curtain rotating in between pass the value of status with 0x00.

  1. (Response) Acknowledgement package. This acknowledgement package is as response of command has been successfully executed or not. This acknowledgement will be transmitted when individual node has been updated. (as described in section 1.4). 4th Byte (Node No) is available in hardware version 0x41 onwards.

Status Success 1st 2nd 3rd 4th 5th 6th 7th
0x7B 0x50 0x03 0x01 0x00 CHECKSUM 0x7D

Status Error 1st 2nd 3rd 4th 5th 6th 7th
0x7B 0x50 0x03 (0x01 to0xNN) 0xFF CHECKSUM 0x7D

Can you help me out... I just used to read the data from the Controller , how to read the data properly...

Have used bytes read options but the value is not correct..

I will enclose the code I have used to get serial..

enter code here// const byte startMarker = 0x7B; const byte messageLength = 12 ; byte receivedBytes[messageLength + 1]; //include start marker boolean newData = false;

void setup() {

 Serial.begin(115200);
 Serial2.begin(9600);
// mySerial.setTimeout(200);
 Serial.println("<Arduino is ready>");
}

void loop() {
  recvBytesWithStartMarker();
  showNewData();
}

void recvBytesWithStartMarker() {
  static boolean recvInProgress = false;
  static int ndx = 0;
byte rc;

  while (Serial2.available() > 0 && newData == false) {
    rc =Serial2.read();
//    Serial.println(rc);
    if (recvInProgress == true) {
      receivedBytes[ndx] = rc;
      ndx++;
      if (ndx >= messageLength) {
        newData = true;
        recvInProgress = false;
        ndx = 0;
      }
    }
    else {//if (rc == startMarker) {
      recvInProgress = true;
      ndx = 0; // save start marker
      receivedBytes[ndx] = rc;
      ndx++;
    }
  }

}

void showNewData() {
  if (newData == true) {
    Serial.println("This just in ... ");
    for (byte i = 0; i <= messageLength; i++)
    {
      Serial.println(receivedBytes[i], HEX);
    }
    newData = false;
  }
}

OUTPUT: 11:14:22.237 -> 55 11:14:22.237 -> AA 11:14:22.237 -> 3 11:14:22.237 -> 7 11:14:22.237 -> 0 11:14:22.237 -> 5 11:14:22.237 -> 2 11:14:22.237 -> 1 11:14:22.237 -> 0 11:14:22.237 -> 1 11:14:22.237 -> 1 11:14:22.237 -> 13