社区
Linux/Unix社区
帖子详情
linux下c语言实现串口读写的问题
lw_haifeng
2005-08-07 04:06:25
各位大侠,我是个菜鸟,不知道linux下c语言编程实现串口读写的程序该怎么处理,请执教。
有源代码最好,分不够另开帖。
...全文
499
3
打赏
收藏
linux下c语言实现串口读写的问题
各位大侠,我是个菜鸟,不知道linux下c语言编程实现串口读写的程序该怎么处理,请执教。 有源代码最好,分不够另开帖。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
yjf7888
2005-08-07
打赏
举报
回复
网上教程比较多
http://www.mcuchina.com/Article/Interface/COM/COMPRG/200411/97.html
loveleijuforever
2005-08-07
打赏
举报
回复
原理参照
Serial Programming Guide for POSIX Operating Systems
POSIX操作系统串行编程指南
5th Edition, 3rd Revision
Copyright 1994-2003 by Michael R. Sweet
Translated by Cedar Peng ( 翻译 彭雪松 )
你到网上找一下应该能找到。其实串口操作很简单,无非就是:
打开串口,设置串口参数(速率,奇偶),写串口或者读串口
loveleijuforever
2005-08-07
打赏
举报
回复
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
//fprintf 写文件
//printf 输出到屏幕上
#define _DEBUG_
#ifdef _DEBUG_
FILE* file1;
#endif
#define FALSE -1
#define TRUE 0
int speed_arr[] = { B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = { 115200, 38400, 19200, 9600, 4800, 2400, 1200, 300};
void set_speed(int fd, int speed);
int set_Parity(int fd, int databits, int stopbits, int parity);
int OpenDev(char *Dev);
int Opencommsg();
int Closecommsg();
int readcommsg( char* s );
int writecommsg( char* s );
int fd;
void set_speed(int fd, int speed)//设置串口参数 (速度)
{
int i;
int status;
struct termios opt;
tcgetattr(fd, &opt);
for( i=0; i<sizeof(speed_arr)/sizeof(int); i++)
{
if(speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&opt, speed_arr[i]);
(&opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &opt);
if(status != 0)
{
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd, int databits, int stopbits, int parity)//设置串口参数 (奇偶)
{
struct termios options;
if( tcgetattr(fd, &options) != 0)
{
perror("tcgetattr function");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag |= INPCK;
break;
case 'S':
case 's':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return(FALSE);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return(FALSE);
}
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 1;
options.c_cc[VMIN] = 0;
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("tcsetattr function");
return(FALSE);
}
return (TRUE);
}
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR|O_NOCTTY);
if( fd < 0 )
{
printf("Can't Open Serial Port");
return -1;
}
else
{
return fd;
}
}
int Opencommsg()
{
char *dev = "/dev/ttys1";
fd = OpenDev( dev );
if( fd < 0 )
{
printf( "error open the serialport\n" );
return -1;
}
set_speed( fd, 38400 );
if( set_Parity( fd, 8, 1, 'n') == FALSE )
{
return -1;
}
#ifdef _DEBUG_
char filename[50];
time_t NowTime;
NowTime = time( NULL );
sprintf( filename, "pform_data/RETURN_%.2d_%.2d.dat",
(localtime(&NowTime)->tm_mon+1),localtime(&NowTime)->tm_mday );
file1 = fopen( filename, "a" );
if( file1 == NULL )
{
printf( "Error: can not Open the Set return code data!\n" );
}
fprintf( file1, "A New Process***:\n" );
fprintf( file1, "current time is: %.2d:%.2d:%.2d\n", localtime(&NowTime)->tm_hour,
localtime(&NowTime)->tm_min, localtime(&NowTime)->tm_sec );
#endif
return 1;
}
int Closecommsg()
{
#ifdef _DEBUG_
fclose( file1 );
#endif
if( close( fd ) )
{
return 1;
}
else
{
return -1;
}
}
int writecommsg( char* s )
{
int count = 0;
char que_code[31];
if( s[0] == 'Q' )
{
#ifdef _DEBUG_
printf( " write query code! \n" );
#endif
count = write( fd, s, 1 );
}
else if( s[0] == 'S' )
{
#ifdef _DEBUG_
printf( " write the set code !\n " );
#endif
count = write( fd, s, 31 );
int ret = readcommsg( que_code );
#ifdef _DEBUG_
fprintf( file1, "Set code is: " );
for( int a = 0; a < 31; a++ )
{
fprintf( file1, "%c", s[a] );
}
fprintf( file1, "\n" );
#endif
if( ret == -1 )
{
printf( "error read the set code !\n" );
return -1;
}
else if( ret == 1 )
{
#ifdef _DEBUG_
fprintf( file1, "the return set code is: " );
#endif
for( int i =0; i < 31; i++ )
{
printf( "%c", que_code[i] );
#ifdef _DEBUG_
fprintf( file1, "%c", que_code[i] );
#endif
/* if( que_code[i] != s[i] )
{
printf( "error return set code\n" );
return -1;
}
*/ }
printf( "\n" );
#ifdef _DEBUG_
fprintf( file1, "\n" );
fprintf( file1, "******\n" );
fflush( file1 );
#endif
}
}
if( count > 0 )
{
printf( " write count = %d\n", count );
return 1;
}
else
{
printf( "error write the serial port!\n" );
return -1;
}
}
int readcommsg( char* s )
{
int count = 0;
int remaind ;
int head = 0;
int read_count = 0;
while(1)
{
count = read( fd, s, 1 );
read_count++;
if(s[0]=='Q' || s[0]=='P' || s[0] == 'S')
{
printf( "read first byte is: %c\n", s[0] );
break;
}
else if( read_count > 100 )
{
printf( "Read 10 seconds, Maybe the platform's power is not on!\n" );
return -1;
}
}
if( s[0]=='Q' )
remaind = 24;
else if( s[0]=='P' )
remaind = 13;
else if( s[0] == 'S' )
remaind = 30;
head = 1;
while( remaind > 0 )
{
count = read(fd , s + head , remaind);
remaind -= count;
head += count;
}
#ifdef _DEBUG_
printf( "read count = %d \n", head );
#endif
return 1;
}
经典
linux
c语言
串口
读写
以及UDP
读写
例程
经典
linux
c语言
串口
读写
以及UDP
读写
例程
用
C语言
写的
linux
串口
读写
源代码com serial,适用于arm和各种
linux
系统
用
C语言
写的
linux
串口
读写
源代码com serial,适用于arm和各种
linux
系统
SMS 消息编码与解码---
C语言
实现
(bit7 bit8 ucs2
串口
等)
1,消息编码与解码 1),用C
实现
7-bit编码和解码的算法 2),用C
实现
UCS2编码和解码的算法 2,消息的发送(PDU编解码) 1),发送短消息 2),读取短消息 3),删除短消息 3,
串口
读写
短信
linux
下基于Bluez
实现
蓝牙SPP服务端demo
基于Bluez
实现
了蓝牙
串口
通信,用来与手机端,pc端传输数据。蓝牙模块服务多种多样,这个小demo
实现
了
linux
下建立蓝牙服务端的demo,并带了一个客户端测试。 使用时,可通过两台设备,分别作为服务端可客户端,连接时填入对方地址即可开启测试。服务端建立后,客户端可通过android或者Qt客户端建立SPP服务访问服务端,并互相传递数据。 客户端并不一定需要用C来
实现
,只需要根据相关api建立于服务端的连接即可
嵌入式
Linux
应用程序开发标准教程(第2版全)
《嵌入式
Linux
应用程序开发标准教程(第2版)》主要分为3个部分,包括
Linux
基础、搭建嵌入式
Linux
环境和嵌入式
Linux
的应用开发。
Linux
基础部分从
Linux
基础、基本操作命令讲起,为
Linux
初学者能快速入门提供了保证。接着系统地讲解了嵌入式
Linux
的环境搭建,以及嵌入式
Linux
的I/O与文件系统的开发、进程控制开发、进程间通信开发、网络应用开发、基于中断的开发、设备驱动程序的开发以及嵌入式图形界面的开发等,并且还安排了丰富的实验内容与课后实践,使读者能够边学边用,更快更好地掌握所学知识。 《嵌入式
Linux
应用程序开发标准教程(第2版)》可作为高等院校电子类、电气类、控制类等专业高年级本科生、研究生学习嵌入式
Linux
的教材,也可供希望转入嵌入式领域的科研和工程技术人员参考使用,还可作为嵌入式培训班的教材和参考书。 第1章
Linux
快速入门 1.1 嵌入式
Linux
基础 1.1.1
Linux
发展概述 1.1.2
Linux
作为嵌入式操作系统的优势 1.1.3
Linux
发行版本 1.1.4 如何学习
Linux
1.2
Linux
安装 1.2.1 基础概念 1.2.2 硬件需求 1.2.3 安装准备 1.3
Linux
文件及文件系统 1.3.1 文件类型及文件属性 1.3.2 文件系统类型介绍 1.3.3
Linux
目录结构 1.4 实验内容——安装
Linux
操作系统 1.5 本章小结 1.6 思考与练习 第2章
Linux
基础命令 2.1
Linux
常用命令 2.1.1 用户系统相关命令 2.1.2 文件相关命令 2.1.3 压缩打包相关命令 2.1.4 文件比较合并相关命令 2.1.5 网络相关命令 2.2
Linux
启动过程详解 2.2.1 概述 2.2.2 内核引导阶段 2.2.3 init阶段 2.3
Linux
系统服务 2.3.1 独立运行的服务 2.3.2 xinetd设定的服务 2.3.3 系统服务的其他相关命令 2.4 实验内容 2.4.1 在
Linux
下解压常见软件 2.4.2 定制
Linux
系统服务 2.5 本章小结 2.6 思考与练习 第3章
Linux
下C编程基础 3.1
Linux
下
C语言
编程概述 3.1.1
C语言
简单回顾 3.1.2
Linux
下
C语言
编程环境概述 3.2 常用编辑器 3.2.1 进入vi 3.2.2 初探emacs 3.3 gcc编译器 3.3.1 gcc编译流程解析 3.3.2 gcc编译选项分析 3.4 gdb调试器 3.4.1 gdb使用流程 3.4.2 gdb基本命令 3.5 make工程管理器 3.5.1 makefile基本结构 3.5.2 makefile变量 3.5.3 makefile规则 3.5.4 make管理器的使用 3.6 使用autotools 3.6.1 autotools使用流程 3.6.2 使用autotools所生成的makefile 3.7 实验内容 3.7.1 vi使用练习 3.7.2 用gdb调试程序的bug 3.7.3 编写包含多文件的makefile 3.7.4 使用autotools生成包含多文件的makefile 3.8 本章小结 3.9 思考与练习 第4章 嵌入式系统基础 4.1 嵌入式系统概述 4.1.1 嵌入式系统简介 4.1.2 嵌入式系统发展历史 4.1.3 嵌入式系统的特点 4.1.4 嵌入式系统的体系结构 4.1.5 几种主流嵌入式操作系统分析 4.2 ARM处理器硬件开发平台 4.2.1 ARM处理器简介 4.2.2 ARM体系结构简介 4.2.3 ARM9体系结构 4.2.4 S3C2410处理器详解 4.3 嵌入式软件开发流程 4.3.1 嵌入式系统开发概述 4.3.2 嵌入式软件开发概述 4.4 实验内容——使用JTAG烧写NandFlash 4.5 本章小结 4.6 思考与练习 第5章 嵌入式
Linux
开发环境的搭建 5.1 嵌入式开发环境的搭建 5.1.1 嵌入式交叉编译环境的搭建 5.1.2 超级终端和minicom配置及使用 5.1.3 下载映像到开发板 5.1.4 编译嵌入式
Linux
内核 5.1.5
Linux
内核源码目录结构 5.1.6 制作文件系统 5.2 U-Boot移植 5.2.1 Bootloader介绍 5.2.2 U-Boot概述 5.2.3 U-Boot源码导读 5.2.4 U-Boot移植主要步骤 5.3 实验内容——创建
Linux
内核和文件系统 5.4 本章小结 5.5 思考与练习 第6章 文件I/O编程 6.1
Linux
系统调用及用户编程接口(API) 6.1.1 系统调用 6.1.2 用户编程接口(API) 6.1.3 系统命令 6.2
Linux
中文件及文件描述符概述 6.3 底层文件I/O操作 6.3.1 基本文件操作 6.3.2 文件锁 6.3.3 多路复用 6.4 嵌入式
Linux
串口
应用编程 6.4.1
串口
概述 6.4.2
串口
设置详解 6.4.3
串口
使用详解 6.5 标准I/O编程 6.5.1 基本操作 6.5.2 其他操作 6.6 实验内容 6.6.1 文件
读写
及上锁 6.6.2 多路复用式
串口
操作 6.7 本章小结 6.8 思考与练习 第7章 进程控制开发 7.1
Linux
进程概述 7.1.1 进程的基本概念 7.1.2
Linux
下的进程结构 7.1.3
Linux
下进程的模式和类型 7.1.4
Linux
下的进程管理 7.2
Linux
进程控制编程 7.3
Linux
守护进程 7.3.1 守护进程概述 7.3.2 编写守护进程 7.3.3 守护进程的出错处理 7.4 实验内容 7.4.1 编写多进程程序 7.4.2 编写守护进程 7.5 本章小结 7.6 思考与练习 第8章 进程间通信 8.1
Linux
下进程间通信概述 8.2 管道 8.2.1 管道概述 8.2.2 管道系统调用 8.2.3 标准流管道 8.2.4 FIFO 8.3 信号 8.3.1 信号概述 8.3.2 信号发送与捕捉 8.3.3 信号的处理 8.4 信号量 8.4.1 信号量概述 8.4.2 信号量的应用 8.5 共享内存 8.5.1 共享内存概述 8.5.2 共享内存的应用 8.6 消息队列 8.6.1 消息队列概述 8.6.2 消息队列的应用 8.7 实验内容 8.7.1 管道通信实验 8.7.2 共享内存实验 8.8 本章小结 8.9 思考与练习 第9章 多线程编程 9.1
Linux
线程概述 9.1.1 线程概述 9.1.2 线程机制的分类和特性 9.2
Linux
线程编程 9.2.1 线程基本编程 9.2.2 线程之间的同步与互斥 9.2.3 线程属性 9.3 实验内容——“生产者消费者”实验 9.4 本章小结 9.5 思考与练习 第10章 嵌入式
Linux
网络编程 10.1 TCP/IP概述 10.1.1 OSI参考模型及TCP/IP参考模型 10.1.2 TCP/IP协议族 10.1.3 TCP和UDP 10.2 网络基础编程 10.2.1 socket概述 10.2.2 地址及顺序处理 10.2.3 socket基础编程 10.3 网络高级编程 10.4 实验内容——NTP协议
实现
10.5 本章小结 10.6 思考与练习 第11章 嵌入式
Linux
设备驱动开发 11.1 设备驱动概述 11.1.1 设备驱动简介及驱动模块 11.1.2 设备分类 11.1.3 设备号 11.1.4 驱动层次结构 11.1.5 设备驱动程序与外界的接口 11.1.6 设备驱动程序的特点 11.2 字符设备驱动编程 11.3 GPIO驱动程序实例 11.3.1 GPIO工作原理 11.3.2 GPIO驱动程序 11.4 块设备驱动编程 11.5 中断编程 11.6 按键驱动程序实例 11.6.1 按键工作原理 11.6.2 按键驱动程序 11.6.3 按键驱动的测试程序 11.7 实验内容——test驱动 11.8 本章小结 11.9 思考与练习 第12章 Qt图形编程基础 12.1 嵌入式GUI简介 12.1.1 Qt/Embedded 12.1.2 MiniGUI 12.1.3 Microwindows、TinyX等 12.2 Qt/Embedded开发入门 12.2.1 Qt/Embedded介绍 12.2.2 Qt/Embedded信号和插槽机制 12.2.3 搭建Qt/Embedded开发环境 12.2.4 Qt/Embedded窗口部件 12.2.5 Qt/Embedded图形界面编程 12.2.6 Qt/Embedded对话框设计 12.3 实验内容——使用Qt编写“Hello,World”程序 12.4 本章小结
Linux/Unix社区
23,120
社区成员
74,507
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章