零知开源快速入门33-SoftSerialSTM32 软串口的使用

标准板上提供了3组硬件串口与外部设备进行通信,同时也提供了SoftSerialSTM32类库,让我们可以用普通数字引脚同过软件模拟成串口进行通信,我们称之为软件串口。

软串口是由程序模拟实现的,使用方法类似硬件串口,但也无法完全替代硬件串口。

SoftSerialSTM32.h不是零知的核心类库,所以在使用前,我们需要声明包含SoftSerialSTM32.h

然后通过SoftSerialSTM3(rxPin,txPin)构造函数指定软串口的RX、TX引脚。

SoftSerialSTM2中定义的成员函数和HardwareSerial类似,包括available()、begin()、read()、write()、print()、println()、peek() 等,用法也都一样。

使用 CP2102模块 USB TO TTL USB转串口模块UART STC下载器可以查看软串口 http://www.lingzhilab.com/home/introduction.html?gid=736

接线

CP2102        标准板

RXD             10

TXD               9

VCC             5v

GND            GND

实物接线

具体使用方法请看以下代码:

代码


							
	/**********************************************************
	*    文件: miniboard.ino      by 零知实验室([url=http://www.lingzhilab.com]www.lingzhilab.com[/url])
	*    -^^- 零知开源,让电子制作变得更简单! -^^-
	*    时间: 2019/04/29 12:14
	*    说明: 软件方式模拟的串口示例
	************************************************************/
	 
	#include "SoftSerialSTM32.h"
	 
	SoftSerialSTM32 mySerial(9, 10);
	 
	void setup()
	{
	  // Open serial communications and wait for port to open:
	  Serial.begin(57600);
	 
	  Serial.println("Goodnight moon!");
	 
	  // set the data rate for the SoftwareSerial port
	  mySerial.begin(9600);
	  mySerial.println("Hello, world?");
	}
	 
	void loop() // run over and over
	{
	}
						
							
						

将以上代码验证上传至零知标准板,然后用USB转串口工具连接9,,10号脚和电脑,打开串口调试界面,选择好串口,查看到的效果如下