香橙派zero2w——串口的配置和使用

目录

一、串口的配置

1、查看可用串口

2、 图形化界面打开串口

a.首先运行sudo orangepi-config命令,进入图形化界面,并选择System;

b.然后选择Hardware

c.打开uart5串口

d.保存重启开发板

3.检测串口是否打开

 二、串口测试代码

三、编译运行代码

 


一、串口的配置

1、查看可用串口

输入gpio readall命令查看串口,发现可用的 uart 为 uart2、uart3、uart4和uart5。请注意 uart0 默认设置 为调试串口,请不要把 uart0 当成普通串口使用。

2、 图形化界面打开串口

a.首先运行sudo orangepi-config命令,进入图形化界面,并选择System;

b.然后选择Hardware

c.打开uart5串口

 

d.保存重启开发板

3.检测串口是否打开

将usb转ttl模块插入电脑使用串口调试助手,检测串口是否正常

输入命令gpio serial /dev/ttyS5查看串口信息

 二、串口测试代码

#include <stdio.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
 
int fd;
 
/* 发送数据线程 */
void *sendDatas(){
    char *sendBuffer = (char *)malloc(128);
    while(1){
        memset(sendBuffer,'\0',sizeof(sendBuffer));
        printf("Send -> ");
        scanf("%s",sendBuffer);
        serialPuts(fd,sendBuffer);
    }
}
 
int main ()
{
    fd = serialOpen("/dev/ttyAS5",115200);          //打开串口函数,注意波特率
    if (fd < 0){
        printf("serial open error\n");
        return -1;
    }
 
    if (wiringPiSetup () == -1){
        perror("wiringPi setup");
        return -1;
    }
 
    pthread_t sendThread;                           
    char recvBuffer[128];
    int cnt;
 
    pthread_create(&sendThread,NULL,sendDatas,NULL);    //新建串口发送线程
 
    /* 主线程,接收数据 */
    while(1){
        cnt = serialDataAvail(fd);
        if(cnt > 0){
            memset(recvBuffer,'\0',128);
            read(fd,recvBuffer,cnt);
            printf("receive -> %s\n",recvBuffer);
        }
    }
}

三、编译运行代码

 

gcc serial.c -lwiringPi -lpthread    #编译代码
sudo ./a.out                         #运行代码

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琪琪猫不会嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值