树莓派c语言串口通讯程序

本文介绍如何在树莓派上使用C语言进行串口通讯,通过对比Python实现,强调C语言在系统集成中的重要性。作者分享了之前在天嵌2440开发板上的串口程序经验,认为串口程序在不同系统间具有通用性,并提供了相应的C语言代码片段。
摘要由CSDN通过智能技术生成

在网上搜了很多关于树莓派串口通讯的例子,但是都是用python写的,虽然python很有名,而且最近也在学习这门语言,

但是还是想用c语言实现一下,因为需要用到整套系统里,其他内容都是用c语言写的。但是网上没有找打现成的。

还好,之前做过天嵌2440开发板的串口程序(参照天嵌工程师提供的代码),发现串口程序其实是通用的,

驱动层屏蔽了硬件细节,个人猜测,几乎所有移植好的系统的串口,都可以用一样的代码来操作,至少2440和树莓派是通用的。

分享代码如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include"pthread.h"
#include "serial.h"


struct serial_config serialread;

static int serial_fd;

int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,
		   B38400, B19200, B9600, B4800, B2400, B1200, B300};

int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,
		  38400, 19200, 9600, 4800, 2400, 1200, 300};

//-----------------------------------------------
//打印配置文件serial.cfg的内容
//-----------------------------------------------
void print_serialread()
{
	printf("serialread.dev is %s\n",serialread.serial_dev);
	printf("serialread.speed is %d\n",serialread.serial_speed);
	printf("serialread.databits is %d\n",serialread.databits);
	printf("serialread.stopbits is %d\n",serialread.stopbits);
	printf("serialread.parity is %c\n",serialread.parity);
}

//-----------------------------------------------
//读取serial.cfg文件并进行配置
//----------------------------------------------
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值