幾個有意思串口相關的arduino庫

很多設備是用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;
}
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • # Python 資源大全中文版 我想很多程序員應該記得 GitHub 上有一個 Awesome - XXX 系列...
    小邁克閱讀 3,034評論 1 3
  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007閱讀 2,222評論 0 3
  • Arduino自2005年推出以來,廣受好評,如今已成為最熱門的開源硬件之一。在全球最大的開源社區Github上,...
    奈何col閱讀 6,327評論 3 53
  • 伸個懶腰,眨巴一下嘴,泛起肉骨頭粥的味道。應該是羊肉的
    阿吉二閱讀 195評論 0 0
  • 愛所呈現的方式 今天忙忙碌碌的一天,生命的流動是能量的交換,人生的狀態是內在沉靜而外在活動...
    岑語涵元寶閱讀 532評論 0 0