通信接口

Arduino软串口的使用

字号+ 作者:duino123.com 来源:未知 2016-05-04 15:27 我要评论( )

SoftwareSerial Library The Arduino hardware has built-in support for serial communication on pins 0 and 1 (which also goes to the computer via the USB connection). The native serial support happens via a piece of hardware (built into the c

SoftwareSerial Library

The Arduino hardware has built-in support for serial communication on pins 0 and 1 (which also goes to the computer via the USB connection). The native serial support happens via a piece of hardware (built into the chip) called a UART. This hardware allows the Atmega chip to receive serial communication even while working on other tasks, as long as there room in the 64 byte serial buffer.

The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol.

The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library by Mikal Hart.

Limitations

The library has the following known limitations:

  • If using multiple software serial ports, only one can receive data at a time.
  • Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
  • Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  • On Arduino or Genuino 101 the current maximum RX speed is 57600bps
  • On Arduino or Genuino 101 RX doesn't work on Pin 13

If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial libraryAltSoftSerial overcomes a number of other issues with the core SoftwareSerial, but has it's own limitations. Refer to the AltSoftSerial site for more information.
 


SoftwareSerial(rxPin, txPin, inverse_logic)

Description

SoftwareSerial is used to create an instance of a SoftwareSerial object, whose name you need to provide as in the example below. The inverse_logic argument is optional and defaults to false. See below for more details about what it does. Multiple SoftwareSerial objects may be created, however only one can be active at a given moment.

You need to call SoftwareSerial.begin() to enable communication.

Parameters

rxPin: the pin on which to receive serial data

txPin: the pin on which to transmit serial data

inverse_logic: is used to invert the sense of incoming bits (the default is normal logic). If set, SoftwareSerial treats a LOW (0 volts on the pin, normally) on the Rx pin as a 1-bit (the idle state) and a HIGH (5 volts on the pin, normally) as a 0-bit. It also affects the way that it writes to the Tx pin. Default value is false.

Warning: You should not connect devices which output serial data outside the range that the Arduino can handle, normally 0V to 5V, for a board running at 5V, and 0V to 3.3V for a board running at 3.3V.

Example

#include <SoftwareSerial.h>

const byte rxPin = 2;
const byte txPin = 3;

// set up a new serial object
SoftwareSerial mySerial (rxPin, txPin);

 

SoftwareSerial: available()

Description

Get the number of bytes (characters) available for reading from a software serial port. This is data that's already arrived and stored in the serial receive buffer.

Syntax

mySerial.available()

Parameters

none

Returns

the number of bytes available to read

Example

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available()>0){
  mySerial.read();
 }
}

SoftwareSerial: begin(speed)

Description

Sets the speed (baud rate) for the serial communication. Supported baud rates are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200.

Parameters

speed: the baud rate (long)

Returns

none

Example

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() {
  // ...
}


SoftwareSerial: isListening()

Description

Tests to see if requested software serial port is actively listening.

Syntax

mySerial.isListening()

Parameters

none

Returns

boolean

Example

#include <SoftwareSerial.h>

// software serial : TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10,11);

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);

  // Start software serial port
  portOne.begin(9600);
}

void loop()
{
  if (portOne.isListening()) {
   Serial.println("Port One is listening!"); 
}

SoftwareSerial: overflow()

Description

Tests to see if a software serial buffer overflow has occurred. Calling this function clears the overflow flag, meaning that subsequent calls will return false unless another byte of data has been received and discarded in the meantime.

The software serial buffer can hold 64 bytes.

Syntax

mySerial.overflow()

Parameters

none

Returns

boolean

Example

#include <SoftwareSerial.h>

// software serial : TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10,11);

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);

  // Start software serial port
  portOne.begin(9600);
}

void loop()
{
  if (portOne.overflow()) {
   Serial.println("SoftwareSerial overflow!"); 
}

SoftwareSerial: peek

Description

Return a character that was received on the RX pin of the software serial port. Unlike read(), however, subsequent calls to this function will return the same character.

Note that only one SoftwareSerial instance can receive incoming data at a time (select which one with the listen()function).

Parameters

none

Returns

the character read, or -1 if none is available

Example

SoftwareSerial mySerial(10,11);

void setup()
{
  mySerial.begin(9600);
}

void loop()
{
  char c = mySerial.peek();
}


SoftwareSerial: read

Description

Return a character that was received on the RX pin of the software serial port. Note that only one SoftwareSerialinstance can receive incoming data at a time (select which one with the listen() function).

Parameters

none

Returns

the character read, or -1 if none is available

Example

SoftwareSerial mySerial(10,11);

void setup()
{
  mySerial.begin(9600);
}

void loop()
{
  char c = mySerial.read();
}

SoftwareSerial: write(data)

Description

Prints data to the transmit pin of the software serial port as raw bytes. Works the same as the Serial.write() function.

Parameters

Serial.write() for details

Returns

byte
write() will return the number of bytes written, though reading that number is optional

Example

SoftwareSerial mySerial(10, 11);

void setup()
{
  mySerial.begin(9600);
}

void loop()
{
  mySerial.write(45); // send a byte with the value 45

   int bytesSent = mySerial.write(“hello”); //send the string “hello” and return the length of the string.
}

SoftwareSerial: listen()

Description

Enables the selected software serial port to listen. Only one software serial port can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() (unless the given instance is already listening).

Syntax

mySerial.listen()

Parameters

mySerial:the name of the instance to listen

Returns

None

Example

#include <SoftwareSerial.h>

// software serial : TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10, 11);

// software serial : TX = digital pin 8, RX = digital pin 9
SoftwareSerial portTwo(8, 9);

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);

  // Start both software serial ports
  portOne.begin(9600);
  portTwo.begin(9600);

}

void loop()
{
  portOne.listen();

  if (portOne.isListening()) {
   Serial.println("Port One is listening!"); 
}else{
   Serial.println("Port One is not listening!"); 
}

  if (portTwo.isListening()) {
   Serial.println("Port Two is listening!"); 
}else{
   Serial.println("Port Two is not listening!"); 
}

}

SoftwareSerial: println(data)

Description

Prints data to the transmit pin of the software serial port, followed by a carriage return and line feed. Works the same as the Serial.println() function.

Parameters

vary, see Serial.println() for details

Returns

byte
println() will return the number of bytes written, though reading that number is optional

Example

SoftwareSerial serial(10,11);
int analogValue;

void setup()
{
  serial.begin(9600);
}

void loop()
{
  // read the analog input on pin 0:
  analogValue = analogRead(A0);

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

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

SoftwareSerial: print(data)

Description

Prints data to the transmit pin of the software serial port. Works the same as the Serial.print() function.

Parameters

vary, see Serial.print() for details

Returns

byte
print() will return the number of bytes written, though reading that number is optional

Example

SoftwareSerial serial(10,11);
int analogValue;

void setup()
{
  serial.begin(9600);
}

void loop()
{
  // read the analog input on pin 0:
  analogValue = analogRead(A0);

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

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

如需更详细资料,如有好资源分享,请加入QQ交流群:214461008

欢迎阅读,欢迎转载,不用注明出处!本站只提供入门和进阶资料,做您学习的小伙伴!

老司机也欢迎光临指导!有好内容请加群:214461008 不要看声明了,看内容吧!

相关文章
  • [2016.05.04]Arduino串口从基础到高级最全面教程

    [2016.05.04]Arduino串口从基础到高级最全面教程

    2016-05-05 14:14

  • 手机APP通过蓝牙控制Arduino上的通讯实验及完整例程

    手机APP通过蓝牙控制Arduino上的通讯实验及完整例程

    2016-05-02 22:13

  • Arduino串口高级用法

    Arduino串口高级用法

    2016-05-17 09:24

  • ESP8266使用指南(基于AT指令 NodeMCU 固件ESP-12E)

    ESP8266使用指南(基于AT指令 NodeMCU 固件ESP-12E)

    2016-04-28 17:50

网友点评
Arduino