1 Star 1 Fork 0

Schips/quick-cpp

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
uart.example.cpp 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
Schips 提交于 2024-06-28 17:33 +08:00 . example : uart : 优化例程为 echo 功能
#ifdef __cplusplus
extern "C"{
#endif
#include <stdio.h>
#include "uart.h"
#ifdef __cplusplus
}
#endif
int serial_init(char * uart_port, int speed)
{
int fd;
int SerialSpeed = speed;
printf("Open [%s] with [%d]\n", uart_port, speed);
fd = serial_open(uart_port);
if(fd == -1)
{
printf("open fail\n");
return -1; // 失败
}
if (serial_set(fd, SerialSpeed, 0, 8, 1, 'N') != 0)
{
return -1;
}
return fd;
}
int uart_demo(char * uart_dev_path)
{
// 打开串口
unsigned int i;
char uart_path [32] = "/dev/ttyS6";
int speed = 115200;
int uart_fd;
if(uart_dev_path != NULL)
{
uart_fd = serial_init(uart_dev_path, speed);
} else
{
uart_fd = serial_init(uart_path, speed);
}
if(uart_fd < 0)
{
return -1;
}
// 接收uart
unsigned char recv_buff[256];
int timeout_s = 0;
int timeout_us = 3000;
int len;
printf("Start Echo.\n");
while(1)
{
len = serial_recv(uart_fd, recv_buff, sizeof(recv_buff), timeout_s, timeout_us);
if (len <= 0)
{
continue;
}
// 将收到的数据发送出去
serial_send(uart_fd, recv_buff, len);
}
return 0;
}
/*
int main(int argc, char * argv[])
{
if(argc != 0)
{
return uart_demo(argv[1]);
}
return 0;
}
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/schips/quick-cpp.git
git@gitee.com:schips/quick-cpp.git
schips
quick-cpp
quick-cpp
master

搜索帮助