Arduino参考

Arduino参考 > Libraries > SoftwareSerial > print()

SoftwareSerial.print()

Prints data to the transmit pin of the SoftwareSerial object. Works the same as the Serial.print() function.

Syntax

mySerial.print(val)

Parameters

  • val: the value to print.

Returns

The number of bytes written (reading this number is optional).

Example

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial mySerial(10, 11);

int analogValue;

void setup() {
    // Set the baud rate for the SerialSoftware object
    mySerial.begin(9600);
}

void loop() {
    // Read the analog value on pin A0
    analogValue = analogRead(A0);

    // Print analogValue in the Serial Monitor in many formats:
    mySerial.print(analogValue);         // Print as an ASCII-encoded decimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, DEC);    // Print as an ASCII-encoded decimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, HEX);    // Print as an ASCII-encoded hexadecimal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, OCT);    // Print as an ASCII-encoded octal
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue, BIN);    // Print as an ASCII-encoded binary
    mySerial.print("\t");                // Print a tab character
    mySerial.print(analogValue/4, BYTE); // Print as a raw byte value (divide the
                                         // value in 4 because analogRead() function returns numbers
                                         // from 0 to 1023, but a byte can only hold values up to 255)

    mySerial.print("\t");                // Print a tab character    
    mySerial.println();                  // Print a line feed character

    // Pause for 10 milliseconds before the next reading
    delay(10);
}