很多设备是用AT指令的,这个库可以更方便的使用这些设备
https://github.com/coddingtonbear/arduino-managed-serial-device
给设备生成一个独特的ID
https://github.com/ricaun/ArduinoUniqueID
修剪serial.println等带着的\r\n这些特殊符号
https://github.com/bakercp/PacketSerial/blob/master/docs/BACKGROUND.md
和processing通讯用的库
https://github.com/SMFSW/SerialTerminal
可以设定arduino串口指令,比如发送ON打开LED什么的
https://github.com/ppedro74/Arduino-SerialCommands
然后看到一个arduino之间通过串口传输数据并且带校验部分的样例代码
https://henryforceblog.wordpress.com/2015/03/12/designing-a-communication-protocol-using-arduinos-serial-library/
我把代码粘过来吧怕他有天删了
主机
const uint8_t bufferSize = 5;
uint8_t buffer[bufferSize];
void setup(){
while(!Serial);
Serial.begin(115200);
//we define our header byte only once, we're not going to change it
buffer[0] = 0x7E;
}
void loop(){
//Read potentiometer values
int r = analogRead(0);
int g = analogRead(1);
int b = analogRead(2);
//analogRead() returns a 10 bit value, we need to scale it to a 8 bit value.
//10 bit max value is 1023, 8 bit max value is 255.
//We have two options:
//1 - Divide our 10 bit value by 4, to obtain and effective 8 bit value.
//2 - Bitshift Right by 2, to obtain and effective 8 bit value.
//Dividing is easier to understand, but computes slowly
buffer[1] = r / 4;
buffer[2] = g / 4;
buffer[3] = b / 4;
/*
//Bitshifting is faster, but a little harder to understand.
//After the shifting we AND the result because the value is a 16 bit value
//int is a signed 16 bit value in Arduino 8 bit boards.
buffer[1] = (r >> 2) & 0xFF;
buffer[2] = (g >> 2) & 0xFF;
buffer[3] = (b >> 2) & 0xFF;
*/
buffer[4] = checksum();
//We send all bytes stored in the buffer
Serial.write(buffer, bufferSize);
delay(100);
}
//We perform a sum of all bytes, except the one that corresponds to the original
//checksum value. After summing we need to AND the result to a byte value.
uint8_t checksum(){
uint8_t result = 0;
uint16_t sum = 0;
for(uint8_t i = 0; i < (bufferSize - 1); i++){
sum += buffer[i];
}
result = sum & 0xFF;
return result;
}
从机
const uint8_t header = 0x7E;
const uint8_t bufferSize = 5;
uint8_t buffer[bufferSize];
uint8_t readCounter;
uint8_t isHeader;
//Flag that helps us restart counter when we first find header byte
uint8_t firstTimeHeader;
void setup(){
while(!Serial);
Serial.begin(115200);
readCounter = 0;
isHeader = 0;
firstTimeHeader = 0;
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
}
void loop(){
//Check if there is any data available to read
if(Serial.available() > 0){
//read only one byte at a time
uint8_t c = Serial.read();
//Check if header is found
if(c == header){
//We must consider that we may sometimes receive unformatted data, and
//given the case we must ignore it and restart our reading code.
//If it's the first time we find the header, we restart readCounter
//indicating that data is coming.
//It's possible the header appears again as a data byte. That's why
//this conditional is implemented, so that we don't restart readCounter
//and corrupt the data.
if(!firstTimeHeader){
isHeader = 1;
readCounter = 0;
firstTimeHeader = 1;
}
}
//store received byte, increase readCounter
buffer[readCounter] = c;
readCounter++;
//prior overflow, we have to restart readCounter
if(readCounter >= bufferSize){
readCounter = 0;
//if header was found
if(isHeader){
//get checksum value from buffer's last value, according to defined protocol
uint8_t checksumValue = buffer[4];
//perform checksum validation, it's optional but really suggested
if(verifyChecksum(checksumValue)){
//We'll employ PWM to control each RGB Component in the Led
analogWrite(11, buffer[1]);
analogWrite(10, buffer[2]);
analogWrite(9, buffer[3]);
}
//restart header flag
isHeader = 0;
firstTimeHeader = 0;
}
}
}
}
//This a common checksum validation method
//We perform a sum of all bytes, except the one that corresponds to the original
//checksum value. After summing we need to AND the result to a byte value.
uint8_t verifyChecksum(uint8_t originalResult){
uint8_t result = 0;
uint16_t sum = 0;
for(uint8_t i = 0; i < (bufferSize - 1); i++){
sum += buffer[i];
}
result = sum & 0xFF;
if(originalResult == result){
return 1;
}else{
return 0;
}
}
网友评论