资源简介
此例子经过验证 不是转载 Linux 串口 数据接收 发送

代码片段和文件信息
/*receive.c 没有线程*/
#include /*标准输入输出定义*/
#include /*标准函数库定义*/
#include /*Unix标准函数定义*/
#include /**/
#include /**/
#include /*文件控制定义*/
#include /*PPSIX终端控制定义*/
#include /*错误号定义*/
#define TRUE 0
#define FALSE -1
/***@brief 设置串口通信速率
*@param fd 类型int 打开串口的文件句柄
*@param speed 类型int 串口速度
*@return void*/
int speed_arr[] = {B115200 B38400 B19200 B9600 B4800 B2400 B1200 B300
B38400 B19200 B9600 B4800 B2400 B1200 B300 };
int name_arr[] = {11520038400 19200 9600 4800 2400 1200 300
38400 19200 9600 4800 2400 1200 300 };
//设置波特率
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]);
cfsetospeed(&Opt speed_arr[i]);
status = tcsetattr(fd TCSANOW &Opt);
if (status != 0)
perror(“tcsetattr fd1“);
return;
}
tcflush(fdTCIOFLUSH);//清空串口的缓冲区
}
}
/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄*
*@param databits 类型 int 数据位 取值为7 或者*
*@param stopbits 类型 int 停止位 取值为1 或者*
*@param parity 类型 int 效验类型取值为NEOS
*/
//此类是封好的比较完整的类 可以直接使用
static int set_Parity(int fd int databits int stopbits int parity)
{
struct termios options;
if(tcgetattr(fd&options)!=0)
{
perror(“SetupSerial 1“);
return(FALSE);
}
options.c_cflag &= ~CSIZE;
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; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case ‘o‘:
case ‘O‘:
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case ‘e‘:
case ‘E‘:
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case ‘S‘:
case ‘s‘: /*as no parity*/
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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5907 2010-02-25 00:05 uart.c
----------- --------- ---------- ----- ----
5907 1
相关资源
- Modbus协议官方文档中、英文全
- 易语言OTG串口通信
- delphi 串口通讯
- STM32蓝牙和串口程序
- uboot到linux logo显示不间断 补丁
- LCD显示温度+串口接收温度.rar
- WPF USB 网络 串口 通信软件
- UNIX/LINUX编程实践教程的源码
- DELPHI与西门子200PLC的串口通信实例
- Linux任务管理器
- linux应用层的华容道游戏源代码
- Verilog FPGA UART串口控制器
- USB转串口驱动,FT232R驱动程序,最新
- PC -- 单片机的串口数据传输系统设计
- STM32F103 串口程序(完整版)
- stm32 ds18b20 温度传感器 测试通过
- ubuntu9.10 可加载内核模块和字符设备驱
- PC 串口调试软件
- 51单片机读取温度数据存储到SD卡中并
- MP3文件ID3v2ID3v2APEv2标签读取
- 单片机与PC机串口通讯仿真
- 串口调试助手V5.0
- 操作系统实验——虚存管理实验
- 双串口调试助手 V4.0 _ 可同时调试两个
- 读取串口数据并画实时曲线的VC 程序
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
评论
共有 条评论