首页 > 系统相关 >Linux 串口编程

Linux 串口编程

时间:2022-10-01 11:55:05浏览次数:70  
标签:buf 串口 res 编程 int fd Linux include config

1 代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>

int Uart_Init(char *Device, unsigned int Baudrate)
{
	int fd;
	int res;
	struct termios config;
	
	fd = open(Device, O_RDWR|O_NOCTTY|O_NDELAY);
	
	//打开失败
	if(fd == -1){
		return fd;
	}

	//配置串口
	bzero(&config, sizeof(config));		
	config.c_cflag |= (CLOCAL|CREAD);		// 使能读
	config.c_cflag &= ~CSIZE;			
	config.c_cflag |= CS8;					// 8位数据
	config.c_cflag &= ~PARENB;				// 无校验
	config.c_cflag &= ~CSTOPB;				// 停止位为1
	config.c_cc[VTIME] = 0;					
	config.c_cc[VMIN]  = 0;
	cfsetispeed (&config, Baudrate);			//输入波特率
	cfsetospeed (&config, Baudrate);			//输出波特率


	res = tcsetattr(fd, TCSANOW, &config);
	if(res != 0){
		close (fd);
		return -2;
	}
	
	return fd;
}

void main()
{
	int fd;
	int i,j;
	int res;
	
	char buf[1024]="Hello Serial";
	int len;

	fd = Uart_Init("/dev/ttyS3", 115200);

	len = strlen(buf);
	
	res = write (fd, buf, len);
	printf("write size=%d\n", res);
	
	
	sleep(1);
	buf[0] = '\0';
	res = read(fd, buf, len);
	printf("read  size=%d %s\n", res, buf);

	close(fd);
}

2 参考链接

一、串口编程流程

https://blog.csdn.net/baweiyaoji/article/details/72885633

https://blog.csdn.net/u011192270/article/details/48174353

二、termios介绍

https://www.cnblogs.com/einstein-2014731/p/6922977.html

标签:buf,串口,res,编程,int,fd,Linux,include,config
From: https://www.cnblogs.com/santion/p/11107810.html

相关文章

  • linux usb端点
    前言端点在实际的USB通讯过程种的具体作用与含义十分重要,但是有些难以理解,自己整理一些知识作为备忘。(注:1、文中的EP表示Endpoint;一、端点概念。端点(Endpoint),是主机与......
  • Linux时间日期类
    Linux时间日期类date指令:显示当前日期基本语法date功能描述:显示当前时间date+%Y功能描述:显示当前年份date+%m功能描述:显示当前月份date+%d功能描述:......
  • 函数式编程(纯函数、避免改变参数值和全局变量)、使用.map()、.filter()使代码更简洁
    函数式编程是:独立函数——不依赖于程序(里面含有可能更改的全局变量)的状态。纯函数——相同的输入总是产生相同的输出,不产生副作用(不更改参数值和全局变量值)。副作用尽量小......
  • Linux实用指令1
    Linux实用指令指定运行级别基本介绍0关机1单用户找回丢失密码2多用户状态没有网络服务3多用户状态有网络服务4系统未使用保留给用户5图形界面6系统重启......
  • Windows下编译linux程序. 1. outguess
    outguess编译使用cygwin。运行终端后。进入outguess目录。./configuremake直接报个错checkingwhethermakesets${MAKE}..../configure:eval:line868:unexp......
  • linux grep、sed、awk 实操备忘
    前提#声明x,y,z,默认字符串连接[cmd]x=1[cmd]declare-pxdeclare--x="1"[cmd]y=1[cmd]z=$x+$y&&echo$z1+1[cmd]declare-iz[cmd]dec......
  • linux学前知识
    linux体系liunx特点自由开放,安全性高可移植,灵活性高多任务,多用户,多线程,网络功能丰富liunx学习阶段    linux学习内容    linux就业方......
  • 最小 Linux 发行版 Tiny Core Linux 7.1 发布
    TinyCore Linux 是一个极度简约但是也高度可扩展的GNU/Linux发行版,其之精简甚至可以小到只有10MB大小。昨天5月23日刚刚发布的TinyCoreLinux7.1也仅仅只有......
  • 信迈AM5728 CAN总线测试及SDK升级,linux can 调试命令
    一、AM5728CAN总线测试开发板文件系统下执行如下指令,配置CAN0总线(软件上的CAN0对应为硬件上的CAN1)比特率为1MHz,并启动CAN总线:Target#canconfigcan0bitrate10000......
  • TI AM5728 SDK升级之 linux设备树解析,以网口cpsw为例
    如果一个结点描述的设备有地址,则应该给出@unit-address。多个相同类型设备结点的name可以一样,只要unit-address不同即可,如本例中含有cpu@0、cpu@1以及serial@101f0000与seri......