////combuad_recv.cpp

#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 <string.h> #include <sys/ioctl.h>
#include <linux/serial.h> #define FALSE -1
#define TRUE 0 /*
设置串口
最基本的设置串口包括波特率设置,效验位和停止位设置。
串口的设置主要是设置 struct termios 结构体的各成员值。 struct termio
{
unsigned short c_iflag; // 输入模式标志
unsigned short c_oflag; // 输出模式标志
unsigned short c_cflag; // 控制模式标志
unsigned short c_lflag; // local mode flags
unsigned char c_line; // line discipline
unsigned char c_cc[NCC]; // control characters
};
*/ /**
*@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void
*/
static int speed_arr[] = { B200, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200, /*B200000,*/ B230400 };
static int name_arr[] = { , , , , , , , , , , , , /*200000,*/ };
static int set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
int baud_rate; tcgetattr(fd, &Opt); for (i = ; i < sizeof(speed_arr)/sizeof(int); i++)
{
if(speed == name_arr[i])
{
baud_rate = speed_arr[i]; if(baud_rate <= B38400)
{
cfsetispeed(&Opt, baud_rate);
cfsetospeed(&Opt, baud_rate);
}
else
{
Opt.c_cflag |= CBAUDEX;
baud_rate -= ;/*baud_rate取值为1 ~ 5,分别对应B57600/B115200/B3000000/B6000000/B12000000*/
cfsetispeed(&Opt, baud_rate);
cfsetospeed(&Opt, baud_rate);
} tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != )
{
perror("tcsetattr fd");
return FALSE;
}
tcflush(fd,TCIOFLUSH); return TRUE;
}
} fprintf(stderr, "Unsupported speed\n"); return FALSE;
} // uart_set_info
// 设置为特诉波特率,比如200000
int set_speci_baud(int fd, int baud)
{
struct serial_struct ss, ss_set;
struct termios Opt;
tcgetattr(fd, &Opt); cfsetispeed(&Opt, B38400);
cfsetospeed(&Opt, B38400); tcflush(fd, TCIFLUSH);/*handle unrecevie char*/
tcsetattr(fd, TCSANOW, &Opt); if((ioctl(fd, TIOCGSERIAL, &ss)) < )
{
printf("BAUD: error to get the serial_struct info:%s\n", strerror(errno));
return -;
} ss.flags = ASYNC_SPD_CUST;
ss.custom_divisor = ss.baud_base / baud;
if((ioctl(fd, TIOCSSERIAL, &ss)) < )
{
printf("BAUD: error to set serial_struct:%s\n", strerror(errno));
return -;
} ioctl(fd, TIOCGSERIAL, &ss_set);
printf("BAUD: success set baud to %d,custom_divisor=%d,baud_base=%d\n",
baud, ss_set.custom_divisor, ss_set.baud_base); return ;
} /**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param databits 类型 int 数据位 取值 为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
static 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 */
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);
} // important
options.c_cflag |= CLOCAL | CREAD;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); /* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd, TCIFLUSH);
options.c_cc[VTIME] = ; /* 设置超时*/
options.c_cc[VMIN] = ; /* Update the options and do it NOW */ if (tcsetattr(fd, TCSANOW, &options) != )
{
perror("SetupSerial 3");
return (FALSE);
} return (TRUE);
} static int open_dev(const char *dev)
{
int fd = open(dev, O_RDWR); //| O_NOCTTY | O_NDELAY
if (- == fd)
{
perror("can't open serial port");
return -;
}
else
{
return fd;
}
} ssize_t Read1(int fd, char *ptr)
{
static int read_cnt = ;
static char *read_ptr = NULL;
static char read_buf[]; if (read_cnt <= )
{
again:
if (- == (read_cnt = read(fd, read_buf,
sizeof(read_buf))))
{
if (EINTR == errno)
{
fprintf(stdout, "[lgw] we received a int signal. \n");
goto again;
} return -;
}
else if ( == read_cnt)
{
return ;
} read_ptr = read_buf;
} --read_cnt;
*ptr = *read_ptr++; return ;
} int main(int argc, char *argv[])
{
int fd;
int band_rate;
int uart_init_interval_second;
char *dev; int cnt = -, i = ;
unsigned char recv_buff[] = { };
char c; if( != argc)
{
printf("Usage: %s <rs232_dev> <band_rate> <uart_init_interval_second>\n", argv[]);
return -;
} dev = (char *)argv[];
band_rate = atoi((char *)argv[]);
uart_init_interval_second = atoi((char *)argv[]);
/*
if((fd = open_dev(dev)) < 0)
{
printf("can't open port %s \n", dev);
return -1;
} */
if((fd = open_dev(dev)) < )
{
printf("can't open port %s \n", dev);
return -;
}
else
{
if(set_speci_baud(fd, band_rate) < )
//if(set_speci_baud(fd, 500000) < 0)
{
printf("Set Speed Error set_speci_baud: %d\n",band_rate);
close(fd);
return -;
} sleep(uart_init_interval_second);
// if(set_speed(fd, 115200) < 0)
// {
// printf("Set Speed Error set_speed\n");
// return -1;
// } if(set_parity(fd, , , 'N') < )
{
printf("Set Parity Error\n");
close(fd);
return -;
}
sleep(uart_init_interval_second);
} while(){
if( == Read1(fd, &c))
{
fprintf(stderr, "c[%c] = 0x%02x \n", c, (unsigned char)c);
}
} close(fd); return ;
}

Non-standard serial port baud rate setting的更多相关文章

  1. ROS 进阶学习笔记(12) - Communication with ROS through USART Serial Port

    Communication with ROS through USART Serial Port We always need to communicate with ROS through seri ...

  2. I.MX6 Linux Serial Baud Rate hacking

    /******************************************************************************** * I.MX6 Linux Seri ...

  3. Serial Port Programming using Win32 API(转载)

    In this tutorial we will learn How to communicate with an external device like a microcontroller boa ...

  4. Serial Port Programming on Linux(转载)

    This is a tutorial on how to program the Serial Ports on your Linux box.Serial Ports are nice little ...

  5. select/poll/epoll on serial port

    In this article, I will use three asynchronous conferencing--select, poll and epoll on serial port t ...

  6. CANBus Determining Network Baud Rate, Automatic bit-rate detection

    http://www.canbushack.com/blog/index.php?title=determining-network-baud-rate Determining Network Bau ...

  7. [转]How Can I Find Out What Is Using a Busy or Reserved Serial Port?

    转自:http://digital.ni.com/public.nsf/allkb/29B079481C5ECE76862578810082394E How Can I Find Out What I ...

  8. 串口总是报'Error opening serial port'

    Comm1.CommName := '//./' + Trim(combx_Port.Text); 目前串口大于20  用上面方法解决的 网上也有上面方法解决如下错误的. 若是您已会应用SPCOMM且 ...

  9. Unable to open serial port /dev/ttyUSB0

    ubuntu12.04使用USB转串口时出现权限不够问题,如下 Unable to open serial port /dev/ttyUSB0 权限不够 解决办法: 通过增加udev规则来实现.步骤如 ...

随机推荐

  1. lxml and 代理ip

    pip install lxml 导包From lxml import etree 1. 注意这个是本地html就直接使用etree.parse即可 2. html_etree=etree.parse ...

  2. hdu 2066 Dijstra 堆优化

    嗯 有广搜的意思 #include<cstdio> #include<iostream> #include<queue> #include<vector> ...

  3. Skywalking的增强与拦截机制

    整理自架构经理(汤哥)的分享 字节增强条件匹配 在 skywalking 中实现很多基于 byte-buddy 的关于链式匹配查询的实现, 代码如下所示: public abstract class ...

  4. C++性能榨汁机之伪共享

    C++性能榨汁机之伪共享 来源  http://irootlee.com/juicer_false_sharing/ 前言 在多核并发编程中,如果将互斥锁的争用比作“性能杀手”的话,那么伪共享则相当于 ...

  5. 【opencv 源码剖析】 三、 morphOp 数学形态学滤波函数, 腐蚀和膨胀就是通过这个函数得到的

    // //_kernel : 形态学滤波的核 //anchor: 锚点再滤波核的位置 //iterations: 迭代次数 static void morphOp( int op, InputArra ...

  6. JavaScript 的查询机制——LHS 与 RHS

    JavaScript 引擎在查找一个变量的时候,有两种查找机制:LHS 和 RHS. RHS 的查询是简单地查找到某个变量的值,而 LHS 则是试图找到变量的容器的本身. 一个简单的例子:当我们执行 ...

  7. navigateTo防止多次跳转

    “wx.navigateTo” 页面跳转.在有网络延迟时多次点击会产生 多次二级页面 再使用wx.navigateBack就会多次返回到之前那页面 解决办法: 点击之前标个状态true 点击之后跳转路 ...

  8. 编译luacheck Linux版

    最近在写Visual Studio Code的Lua插件,需要把luacheck集成进去.但是luacheck默认只提供了win32版本,见https://github.com/mpeterv/lua ...

  9. jqGrid TreeGrid 加载数据 排序 扩展

    发现 jqGrid TreeGrid 加载的数据必须要排序 给了两种平滑数据模式尽然不内部递归 所以改了下源码加了个数据二次过滤器扩展 数据本该是这样的 结果没排序成这样了 (而且还得是从根节点到子节 ...

  10. Scala语言面向对象

    apply1. 面向对象的基本概念: 把数据及对数据的操作方法放在一起,作为一个相互依存的整体-----对象,面向对象的三大特征:封装.多态.继承 2. scala类的定义 · class Emplo ...