有用的鏈接在這里:http://hi.baidu.com/mizuda/item/06b7fdc1d0e45a0ec710b2dd
更加詳細的內容請查閱Arduino官方:http://arduino.cc/en/Serial/Write
代碼如下:
/* *SendBinary sketch *Send a header followed by two random integer values as binary data. */ int intValue; // an short integer value (16 bits = 2bytes) intvalue must be less than 0xffff(65535 = 255*256+255) void setup(){ Serial.begin(9600); } void loop(){ Serial.print('H'); //send a header character intValue = random(591); //generate a random number between 0 and 590 Serial.write(lowByte(intValue)); //send the low byte Serial.write(highByte(intValue)); //send the high byte // send another random integer intValue = random(599); // generate a random number between 0 and 599 // send the two bytes that comprise an integer Serial.write(lowByte(intValue)); // send the low byte Serial.write(highByte(intValue)); // send the high byte //發送單字節很簡單,使用Serial.write(byteVal); //發送整數,需發低字節 和高字節來組成整數,value= high*256+low delay(100000); }
通過上面,我們可以看到
每次發送的都是頭為H的二進制內容。
正如上面注釋縮寫,發送低位字節,發送高位字節。
//發送單字節很簡單,使用Serial.write(byteVal); //發送整數,需發低字節 和高字節來組成整數,value= high*256+low
二進制是一些簡單的內容。這些二進制文件也許是遵循一定規律的文檔。在上位機上,我們可以編寫軟件來從串口獲得這些來自下位機的二進制文件。通過分析矯正位,頭文件等可以從中提取到單片機想傳來的有用的信息。這些信息是經過一定協議的。用起來會比較簡單。
我們可以看到上圖中有些內容可以顯示出來,而有些卻不能,這是因為不是任何二進制內容都可以翻譯成明文表的。ASCII表就那點,符號也不夠分配的。