-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial.ino
41 lines (31 loc) · 961 Bytes
/
Serial.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void serialInit() {
pinPeripheral(5, PIO_SERCOM_ALT);
pinPeripheral(6, PIO_SERCOM_ALT);
#if DEBUG
Serial.begin(9600);
#endif
Serial1.begin(9600);
/**
Serial 3 is used for boardcasting CMD to modules.
It is not allowed to receive CMD.
*/
Serial3.begin(9600); //RX: 5, TX: 6
}
void sendCmd(Stream &_serial, char cmd, char* payload, int length) {
Stream* serial = &_serial;
char buf[6 + length]; //start, data_length, data, checksum, stop
buf[0] = CMD_START; //start_byte
buf[1] = cmd; //cmd_byte
buf[2] = length & 0xff; //data_length - low byte
buf[3] = (length >> 8) & 0xff; //data_length - high byte
buf[4 + length] = 0x00; //checksum
buf[5 + length] = CMD_EOF; //stop_byte
for (int i = 0; i < length; i++) //load buf
buf[4 + i] = payload[i];
for (int i = 0; i < sizeof(buf); i++)
serial->print(buf[i]);
}
void sendCmd(Stream &_serial, char cmd) {
char *p = 0x00;
sendCmd(_serial, cmd, p, 0);
}