#include     <stdio.h>      /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix标准函数定义*/
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX终端控制定义*/
#include <errno.h> /*错误号定义*/
#include <sys/time.h> /***@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void*/ #define TRUE 1
#define FALSE -1 int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, }; int name_arr[] = {, , , , , , , ,
, , , , , , , }; void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt); for ( i= ; 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 != )
perror("tcsetattr fd1");
return;
} tcflush(fd,TCIOFLUSH);
}
} /**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄*
*@param databits 类型 int 数据位 取值 为 7 或者8*
*@param stopbits 类型 int 停止位 取值为 1 或者2*
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{ struct termios options; if ( tcgetattr( fd,&options) != )
{
perror("SetupSerial 1");
return(FALSE);
} options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{ case :
options.c_cflag |= CS7;
break;
case :
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 */
options.c_iflag &= ~(ICRNL|IGNCR);
options.c_lflag &= ~(ICANON );
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 :
options.c_cflag &= ~CSTOPB;
break;
case :
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
} /* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK; options.c_cc[VTIME] = ; // 15 seconds
options.c_cc[VMIN] = ; tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != )
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
/**
*@breif 打开串口
*/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (- == fd)
{ /*设置数据位数*/
perror("Can't Open Serial Port");
return -;
}
else
return fd;
} /**
*@breif main()
*/
int main(int argc, char **argv)
{
int fd;
int nread;
char buff[];
//char *dev ="/dev/ttySAC1";
int i=;
const char* dev = NULL;
const char* string = NULL;
const char* type = NULL; dev = argv[];
string = argv[];
type = argv[]; if(dev==NULL)
{
printf("Please input seria device name ,for exmaple /dev/ttyO1.\nNote:This is loop test application. Make sure that your serial is loop\n");
return ;
} /* 打开串口 */ fd = OpenDev(dev);
if (fd>)
set_speed(fd,); //设置波特率
else
{
printf("Can't Open Serial Port!\n");
exit();
} if (set_Parity(fd,,,'N')== FALSE) //设置传递参数
{
printf("Set Parity Error\n");
exit();
} printf("\nWelcome to TTYtest type:%s\n",type); while()
{ if(strcmp(type,"") == ){
if ((nread = read(fd, buff, ))>)
{
printf("\nLen %d\n", nread);
buff[nread+] = '\0';
printf("read %s\n", buff); }
}
else{
write(fd, string, strlen(string));
printf("Send test data------%s\n",string); } sleep();
}
}

最新文章

  1. Coming
  2. Android 圆形图片加白边加阴影
  3. android 命令编译
  4. css全局样式
  5. Struct Member Default Value
  6. Lua和C之间的交互
  7. Software caused connection abort: socket write error
  8. centos下 Apache、php、mysql默认安装路径
  9. OC中类别、扩展、协议与托付
  10. Android-往来:添加到联系人
  11. python_计算1+……+100中偶数和
  12. Office组件无法正常使用的解决方法
  13. nginx php-fpm开启session
  14. RESTful摘要
  15. HTML5 source标签:媒介元素定义媒介资源
  16. [转]Python中的eval()、exec()及其相关函数
  17. Lottie 动画库
  18. js中的假值
  19. shell 中的流程控制关键字
  20. C# RSA数据加密

热门文章

  1. mysql explain22222 system &gt; const &gt; eq_ref &gt; ref &gt; fulltext &gt; ref_or_null &gt; index_merge &gt; unique_subquery &gt; index_subquery &gt; range &gt; index &gt; ALL
  2. 24模拟keepalved vrrp功能,监听主节点,如果主节点不可访问则备节点启动并配置LVS实现接管主节点的资源提供服务(提醒:注意ARP缓存)
  3. 微信小程序 一些要点
  4. 10道mysql查询语句面试题
  5. WINDOWS 7.1 SDK 安装失败
  6. redis字典的底层实现hashTable
  7. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.2——设置Flavors和Variants
  8. 玩点不同之CSS的BEM规范
  9. POJ3090
  10. 【BZOJ1977】[BeiJing2010组队]次小生成树 Tree 最小生成树+倍增