Arduino串口函数

如何从一个单片机的串口发送数据到Arduino上,然后用Arduino显示呢?

先用单片机的print函数把数据打印到串口,然后用Serial1.read把数据从串口读取回来(以ASCII码的形式),然后把ASCII码转换成字符,然后发送.

unsigned int send_data =1234;
char rx_data[4];
void setup(){
Serial.begin(9600);
Serial3.begin(9600);
Serial1.begin(9600);
}

void loop()
{
Serial3.print( send_data);
if(Serial1.available()>0)
{
Serial1.readBytes(rx_data,4);
for(char i=0;i<4;i++)
{
Serial.print(char(rx_data));
}
Serial.print("\n");
}
delay(500);
}

1,接收函数Serial.read()

int Serial.read(void)
如果串行数据缓冲区有数据,这个函数会读取串行数据缓冲区的第一个字节,数据读取位置移动到下一个数据缓冲区,也就是说如果继续读取的话会读取下一个数据缓冲区的第一个字节.
如果数据缓冲区没有数据,将返回-1.

2,发送函数Serial.print()

Serial.print(val)
print是一个打印函数,主要用于在上位机上显示,让数据可视化,它会将数据用字符的形式(ASCII码)逐个发送到串口. 一般不用这个函数来进行数据传输.
比如下面这段代码不会在串口助手上显示3个256,而会分别显示’2’,‘5’,'6’的ASCII码值,50,53,54(分三次发送,有三个地址不同的数据缓冲区)

void setup() {
// initialize serial ports:
Serial.begin(9600);
Serial1.begin(9600);
Serial3.begin(9600);

Serial3.print(256);
if (Serial1.available()>0) {
Serial.println(Serial1.read());
Serial.println(Serial1.read());
Serial.println(Serial1.read());
}
}
void loop() {
}
3,发送函数Serial.write()

写入二进制数据到串行端口。这个数据是作为一个字节或字节序列发送的。如果发送字符串则需要用到print方法。

Serial.write(str)
str为字符串的首地址,buf为一个用来存放数据的数组的首地址. 这个函数会先发送字符串,然后返回字符串的长度

Serial.write(val)

val的数据类型为unsigned char,这个函数每次只发送一个字节的数据,所以val的值在0-255之间.

Serial.write(buf, len)
buf为要发送的数组的首地址(注意数组的类型为unsigned char),len为数组的长度.

  1. Serial.peek():功能类似于read(),但是我们知道当调用一次read()后,缓冲区的数据流会被读取并删除read过的数据,也就是available值会减少,但peek()不会出现类似情况,其功能类似于检测缓冲区的头部数据,反复调用peek()返回的值是一样的。

Serial.end(); //关闭通信串口
6.
Serial.available();//判断串口缓冲器是否有数据装入
7.
Serial.flush(); //清空串口缓存
8.
Serial.println(); //写入字符串数据+换行到串口
9.
Serial.SerialEvent();//read时触发的事件函数
10.
Serial.readBytes(buffer,length);//读取固定长度的二进制流
11.
Serial.println(incomingByte, DEC);//打印接到数据十进制表示的ascii码。 HEX 十六进制表示

转录于https://blog.csdn.net/nulidehahafr/article/details/80530067,用于个人学习

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Serial is a modern terminal emulator designed to make working with servers, network equipment, and embedded hardware easier for serialengineers and system administrators. For system administrators, Serial supports the all-important break sequence required when working with routers and switches. Serial also includes a full-featured terminal emulator, allowing you to work with the screen-based menus found in network equipment and Unix programs including the text editors vi and emacs. Makers and engineers will appreciate Serial’s built-in support for embedded devices including the Arduino, Raspberry Pi, BeagleBone, and thousands of others. Serial includes several useful features for working with these devices including the ability to reset an Arduino, and the ability to connect to a BeagleBone over USB directly or through the on-board header. Finally, Serial supports several of the most common USB to serial adapters WITHOUT requiring drivers, often saving you the hassle of finding and installing additional software. Features: Full-featured xterm, linux, and ANSI/VT100 terminal emulation Works with USB to serial adapters and devices that use USB to serial chips internally UTF-8 character set support No kernel drivers required for many of the most common devices Bluetooth serial support USB Communication Device Class (CDC) support Break support required for routers, switches and other network equipment Line-buffered send mode Text pacing options XMODEM and YMODEM file transfer support WHAT’S NEW Version 1.3.9: Added support for macOS 10.14 (Mojave) Added the ability to customize the session log save path Added CP437 and CP850 character set support Added support for additional devices Other bug fixes and improvements REQUIREMENTS OS X 10.7 or later, 64-bit processor
Arduino 的 Serial 库提供了许多常用的串口相关函数,下面是一些常见的函数及其用法: 1. Serial.begin(baudrate) 用于初始化串口,设置波特率。baudrate 参数为波特率,单位为 bps。常见的波特率有 9600、115200 等。例如: ``` Serial.begin(9600); // 初始化串口,波特率为9600 ``` 2. Serial.print() 用于向串口发送数据,可以发送数字、字符串、变量等。例如: ``` Serial.print("Hello, world!"); // 发送字符串 int value = 123; Serial.print(value); // 发送数字 ``` 3. Serial.write() 用于向串口发送字节数据。例如: ``` Serial.write(0x55); // 发送字节数据 ``` 4. Serial.available() 用于检查串口接收缓冲区中是否有数据可读。返回值为可读取的字节数。例如: ``` if (Serial.available() > 0) { // 有数据可读 } ``` 5. Serial.read() 用于从串口接收缓冲区中读取一个字节。返回值为读取到的字节数据,如果没有可读取的数据则返回 -1。例如: ``` int data = Serial.read(); // 读取一个字节 if (data != -1) { // 读取到数据 } ``` 6. Serial.flush() 用于清空串口接收缓冲区。例如: ``` Serial.flush(); // 清空接收缓冲区 ``` 7. Serial.setTimeout() 用于设置串口读取数据时的超时时间,单位为毫秒。如果在超时时间内没有读取到数据,则返回 0。例如: ``` Serial.setTimeout(1000); // 设置超时时间为1秒 ``` 以上是一些常见的 Arduino 串口相关函数,还有一些其他的函数和属性可以在 Arduino 的官方文档中查看。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值