| Arduino (電腦) 與 Serial monitor(人機介面---電腦與人的翻譯者) |
| ASII Code Table(PDF) (含extended) Converter ![]()
![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
| PROTOCOLS: UART - I2C - SPI - Serial communications #001(11:57)(YouTube) |
|
| Serial (Arduino.cc) |
Serial.write()(Arduino.cc)
|
| Serial.write() Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the Serial.print() function instead. Serial.write(數) : Serial.write(123)--->0111 1011 ![]() serial monitor 收到後印出 { ![]() Serial.print(字) : Serial.print(123)-->"1" ,"2" ,"3" 一共3個Byte 0011 0001 , 0011 0010 , 0011 0011 ![]() Serial monitor : 一律將收到的binary 轉為ASCII code顯示出來 一律將欲傳送的資料一個一個的轉為binary傳送 為"人"與"電腦"的翻譯者 |
| Serial.read() |
| Returns The first byte of incoming serial data available (or -1 if no data is available). Data type: int. |
#define led 12 void setup(){ Serial.begin(115200); pinMode(led,OUTPUT); digitalWrite(led,LOW); } void loop(){ if(Serial.available()){ if(Serial.read()==49) digitalWrite(led,HIGH); else digitalWrite(led,LOW); delay(2000); } } ![]() ![]() //試試 Serial.read()==1 //試試 Serial.read()=='1' |
|
或
![]() ![]() ![]() ![]() |
Serial.print() (Arduino.cc)
|
![]() The commands Serial.print(217) or Serial.print("217") will both print the same thing: 00110010 00110001 00110111. This is because the print command first converts any number to a string representation and then uses the command Serial.write() to send each character as individual ASCII bits. Serial.print("217",HEX)------WRONG! Serial.print(217,HEX) --------OK! Serial.print(65535,HEX) --------OK! |
| Serial.println() (Arduino.cc) |
| void setup(){ Serial.begin(115200); } void loop(){ if(Serial.available()){ Serial.println(Serial.read()); } } |
![]() ![]() ![]() 如果設定為: (1) NL & CR 就會在送出的字元代碼之後加上 13 與 10 (2) 沒有行結尾 ==> 只送出字元代碼 (3) NL ==> 在字元代碼之後加上 10 (4) CR ==> 在字元代碼之後加上13 |
![]()
|
void setup(){ Serial.begin(115200); } void loop(){ if(Serial.available()){ Serial.println(Serial.read(),HEX); } } ![]() |
void setup(){ Serial.begin(115200); } void loop(){ if(Serial.available()){ Serial.println(Serial.read(),BIN); } } ![]() |
| 補充說明 |
Returns The first byte of incoming serial data available (or -1 if no data is available). Data type: int.
Serial.readBytes() reads characters from the serial port into a buffer. The function terminates if the determined length has been read, or it times out (see Serial.setTimeout()). Serial.readBytes() returns the number of characters placed in the buffer. A 0 means no valid data was found. Syntax Serial.readBytes(buffer, length) Parameters buffer: the buffer to store the bytes in. Allowed data types: or array of char or byte. length: the number of bytes to read. Allowed data types: int. Returns The number of bytes placed in the buffer. Data type: size_t. Example: char readData[10];//The character array is used as buffer to read into. int x = Serial.readBytes(readData,10);//10 is the length of data to read. The function continuously read the received data from the buffer and if target number of character is received it will return with the number of character. And if the number of character it read is less than specified number and timeout time is completes than it will return the number of characters, which is less than the target length.
Syntax Serial.readBytesUntil(character, buffer, length) Parameters character: the character to search for. Allowed data types: char. buffer: the buffer to store the bytes in. Allowed data types: array of char or byte. length: the number of bytes to read. Allowed data types: int. Returns Data type: size_t. Example: char readData[10];//The character array is used as buffer to read into. int x = Serial.readBytesUntil(“\n”,readData,10);//10 is the length of data to read. The function continuously read the received data and store in the variable provided. The function can exit by three events- 1.The timeout time completes, by default the timeout time is one second. 2. Before the timeout time, the function received the specified number of characters ( 10 char ). 3.Before above two events, the function received terminating character ( “\n” newline character ). In all the three exit case the Serial.readBytesUntil() function return the number of character read in the buffer variable. The default wait time is one second, which can be modified with the help of function Serial.setTimeout().
|
| Serial.readString() Description Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()). Syntax Serial.readString() Parameters Serial: serial port object. See the list of available serial ports for each board on the Serial main page. Returns A String read from the serial buffer Example:
|
void setup() { Serial.begin(115200); } void loop() { String readData; readData= Serial.readStringUntil('3'); Serial.println(readData); delay(500); } //傳送 12345 ![]() |
| Serial.print() |
| 於Serial Monitor中顯示不換行 |
| void setup(){ Serial.begin(115200); } void loop(){ if(Serial.available()){ Serial.print(Serial.read(),HEX); } }
|
| Serial.write(0x45); // will write 0100 0101 to the cable Serial.print in the other hand is more versatile , it will do the conversion for you from ASCII to binary it also can convert to BIN/ HEX/OCT/DEC but you need to specify a second argument like so Serial.print(76, BIN) gives "0100 1100" Serial.print(76, OCT) gives "114" Serial.print("L", DEC) gives "76" Serial.print(76, HEX) gives "4C" |
Serial.write(0x48); // H Serial.write(0x45); // E Serial.write(0x4C); // L Serial.write(0x4C); // L Serial.write(0x4F); // O ![]() Serial.print("HELLO"); ![]() Serial.println("HELLO"); ![]() Serial.println() will add 2 bytes 0x0D and 0x0A |
| The commands Serial.print(217) or Serial.print("217") will both print the same thing: 00110010 00110001 00110111. This is because the print command first converts any number to a string representation and then uses the command Serial.write() to send each character as individual ASCII bits. |
| Serial.write()
和Serial.print()的主要差異 例如: Serial.print(65); // 輸出’6’和’5′ 0x36,0x35 Serial.write(65); // Serial monitor 解釋為 ’A’ , //若傳給另一Arduino 就是 0x41 0100 0001 Serial.print()與Serial.println() 有多種format 可用,適合顯示(與人溝通) Serial.wrte()則無,適於晶片與晶片間通訊
|
| 人<----->晶片 Serial Monitor 與 Arduino 通訊 |
| Serial monitor 以ASCII code傳送資料 使用Serial.read();讀取序列埠視窗傳來的數字時 1.會回傳ASC II碼 2.將一個數字一個數字讀取,必須使用字串的方式存取傳來的數字 3.翻譯ASC II碼,C語言內有幾個重要的函式可以負責。
|
| 第二種方法 |
| void setup(){ Serial.begin(115200); Serial.setTimeout(10); // 設定為每10毫秒結束一次讀取(數字愈小愈快) } void loop(){ if(Serial.available()){ int x = Serial.parseInt(); Serial.println(x); } }
![]()
int x,y; void setup(){ Serial.begin(9600); Serial.setTimeout(10); // 設定為每10毫秒結束一次讀取(數字愈小愈快) } void loop(){ while(Serial.available()){ x = Serial.parseInt(); y = Serial.parseInt(); Serial.print(x); // 這邊的x就是我們輸入的值 Serial.print(" , "); Serial.println(y); //這邊的y就是回傳的中止碼0 } }
float x; int y; void setup(){ Serial.begin(115200); Serial.setTimeout(10); // 設定為每10毫秒結束一次讀取(數字愈小愈快) } void loop(){ while(Serial.available()){ x = Serial.parseFloat(); y = Serial.parseInt(); Serial.println(x*0.6+32); // 這邊的x就是我們輸入的值 } }
Serial.begin(115200); Serial.setTimeout(10); // 設定為每10毫秒結束一次讀取(數字愈小愈快) servo.attach(9); } void loop(){ while(Serial.available()){ x = Serial.parseInt(); y = Serial.parseInt(); Serial.print(x); Serial.print(" , "); Serial.println(y); servo.write(x); delay(100); } } |
| Serial.setTimeout() (Arduino.ref) sets the maximum milliseconds to wait for serial data. It defaults to 1000 milliseconds. Serial functions that use the timeout value set via Serial.setTimeout(): Serial.find() Serial.findUntil() Serial.parseInt() Serial.parseFloat() Serial.readBytes() Serial.readBytesUntil() Serial.readString() Serial.readStringUntil() |
| byte
byte_2[]={0x31,0x41}; void setup() { Serial.begin(9600); Serial.write(byte_2,sizeof(byte_2)); } void loop() { } //Serial monitor 一律轉為ASCII Code 顯示 |
| Serial.read() return : the first byte of incoming serial data available (or -1 if no data is available) - int |
| 晶片<----->晶片 2 個 Arduino 使用Serial.write() 與Serial.read()傳送2 Byte data |
或
或
|