Serial.print()

설명

사람이 읽을 수있는 ASCII 텍스트로 직렬 포트에 데이터를 프린트합니다. 이 명령은 다양한 형태를 취할 수 있습니다. 숫자는 각 자리에 ASCII 문자를 사용하여 프린트됩니다. 부동 소수점은 ASCII 숫자로 유사하게 프린트되며 기본값은 소수점 이하 두 자리입니다. 바이트는 단일 문자로 전송됩니다. 문자와 문자열은있는 그대로 전송됩니다. 예를 들면:

  • Serial.print(78) gives "78"
  • Serial.print(1.23456) gives "1.23"
  • Serial.print('N') gives "N"
  • Serial.print("Hello world.") gives "Hello world."

두 번째 옵션 매개 변수는 사용할 기본 (형식)을 지정합니다: BIN(binary, or base 2), OCT(octal, or base 8), DEC(decimal, or base 10), HEX(hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. 예를 들면:

  • Serial.print(78, BIN) gives "1001110"
  • Serial.print(78, OCT) gives "116"
  • Serial.print(78, DEC) gives "78"
  • Serial.print(78, HEX) gives "4E"
  • Serial.print(1.23456, 0) gives "1"
  • Serial.print(1.23456, 2) gives "1.23"
  • Serial.print(1.23456, 4) gives "1.2346"

플래시 메모리 기반 문자열을 F()로 래핑하여 Serial.print()에 전달할 수 있습니다. 예를 들면 :

Serial.print(F(“Hello World”))

문자로 변환하지 않고 데이터를 보내려면 Serial.write()를 사용하세요.

문법

Serial.print(val)

Serial.print(val, format)

매개변수

  • val: 프린트 할 값. 허용되는 데이터 유형: 모든 데이터 유형.

반환값

  • size_t: 프린트 한 바이트 수를 반환합니다.

예제 코드

예제 코드 1

void setup() { Serial.begin(9600); // open the serial port at 9600 bps: // prints character Serial.print('A'); Serial.print('R'); Serial.print('D'); Serial.print('U'); Serial.print('I'); Serial.print('N'); Serial.print('O'); Serial.print('\n'); // prints a new line character Serial.print('\n'); // prints a new line character // prints a string Serial.print("ArduinoGetStarted.com"); Serial.print("\n\n"); // prints a string with two new line characters // prints a floating point number float a = 1.23456; Serial.print(a); // prints 2 decimal places by default Serial.print('\n'); // prints a new line character Serial.print(a, 4); // prints 4 decimal places Serial.print('\n'); // prints a new line character Serial.print(a, 5); // prints 5 decimal places Serial.print('\n'); // prints a new line character Serial.print(a, 6); // prints 6 decimal places Serial.print("\n\n"); // prints a string with two new line characters int x = 77; // prints an integral number in diffrent format in a table Serial.print("DEFAULT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x); // prints as an ASCII-encoded decimal by default - same as "DEC" Serial.print('\n'); // prints a new line character Serial.print("DEC"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x, DEC); // prints as an ASCII-encoded decimal Serial.print('\n'); // prints a new line character Serial.print("HEX"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x, HEX); // prints as an ASCII-encoded hexadecimal Serial.print('\n'); // prints a new line character Serial.print("OCT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x, OCT); // prints as an ASCII-encoded octal Serial.print('\n'); // prints a new line character Serial.print("BIN"); // prints a string Serial.print('\t'); // prints a tab character Serial.print(x, BIN); // prints as an ASCII-encoded binary } void loop() { }

시리얼 모니터에 결과:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
ARDUINO ArduinoGetStarted.com 1.23 1.2346 1.23456 1.234560 DEFAULT 77 DEC 77 HEX 4D OCT 115 BIN 1001101
Ln 11, Col 1
Arduino Uno on COM15
2

예제 코드 2

void setup() { Serial.begin(9600); // open the serial port at 9600 bps: Serial.print("DEFAULT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print("DEC"); // prints a string Serial.print('\t'); // prints a tab character Serial.print("HEX"); // prints a string Serial.print('\t'); Serial.print("OCT"); // prints a string Serial.print('\t'); // prints a tab character Serial.print("BIN"); // prints a string Serial.print('\n'); // prints a new line character // only part of the ASCII table, change as desired for (int x = 0; x <= 16; x++) { // print it out in many formats: Serial.print(x); // prints as an ASCII-encoded decimal by default - same as "DEC" Serial.print('\t'); // prints a tab character Serial.print(x, DEC); // prints as an ASCII-encoded decimal Serial.print('\t'); // prints a tab character Serial.print(x, HEX); // prints as an ASCII-encoded hexadecimal Serial.print('\t'); // prints a tab character Serial.print(x, OCT); // prints as an ASCII-encoded octal Serial.print('\t'); // prints a tab character Serial.print(x, BIN); // prints as an ASCII-encoded binary Serial.print('\n'); // prints a new line character delay(50); // delay 50 milliseconds } } void loop() { }

시리얼 모니터에 결과:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
DEFAULT DEC HEX OCT BIN 0 0 0 0 0 1 1 1 1 1 2 2 2 2 10 3 3 3 3 11 4 4 4 4 100 5 5 5 5 101 6 6 6 6 110 7 7 7 7 111 8 8 8 10 1000 9 9 9 11 1001 10 10 A 12 1010 11 11 B 13 1011 12 12 C 14 1100 13 13 D 15 1101 14 14 E 16 1110 15 15 F 17 1111 16 16 10 20 10000
Ln 11, Col 1
Arduino Uno on COM15
2

※ 주의 및 경고:

  • Serial.print()의 비동기성에 대한 자세한 내용 은 Serial.write() 참조 페이지 의 참고 및 경고 섹션을 참고하세요.
  • Serial functions are not only used for the communication between an Arduino board and Serial Monitor of Arduino IDE but also used for the communication between:
  • An Arduino board and other Arduino board
  • An Arduino board and other sensors/devices
  • An Arduino board and computer (any Serial software on computer)

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES