资源简介
linux串口编程 非阻塞read 打包write 设备打开 串口配置

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include “sq110_uart.h“
#include
#define TTY_TEST “/dev/ttyO2“
struct termios tio;
int init_tty(struct termios *oldtio){
int fd;
struct termios newtio;
if((fd = open(TTY_TEST O_RDWR | O_NOCTTY)) < 0)
{
printf(“tty_test open fail\n“);
return -1;
}
fcntl(fd F_SETFL 0);
tcgetattr(fd oldtio);
bzero(&newtio sizeof(newtio));
cfsetispeed(&newtio B115200);
cfsetospeed(&newtio B115200);
newtio.c_cflag |= CLOCAL|CREAD;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_cflag &= ~(PARENB|PARODD);
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CRTSCTS;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_oflag &= ~OPOST;
tcflush(fd TCIFLUSH);
newtio.c_cc[VTIME] = 1;
newtio.c_cc[VMIN] = 0;
tcsetattr(fd TCSANOW &newtio);
return fd;
}
int send(int ftty unsigned char str[] int len){
// printf(“send -----> len = %d\n“ len);
return (!write(ftty str len)) ? -1 : 1;
}
int recieve(int ftty int streamfd){
int n max_fd len count=0 i = 0;
char buffer[2048] *ptr = buffer;
fd_set input;
struct timeval timeout;
FD_ZERO(&input);
FD_SET(ftty &input);
max_fd = ftty+1;
memset(buffer 0 2048);
count = 0;
while(1){
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if((n = select(max_fd &input NULL NULL &timeout)) <= 0)
break;
len = read(ftty ptr 0x05);
ptr += len;
count += len;
if(count>=5)
break;
}
buffer[count] = 0;
printf(“reading %d data:\n“ count);
for (i = 0; i < count; i++)
printf(“ msg[%d] = 0x%x\n“ i buffer[i]);
}
void hand_shake_init(fd)
{
if(send(fd ribao_init_msg 5) < 1)
{
printf(“tty write fail...\n“);
return ;
}
recieve(fd 0);
send(fd ribao_init_msg 5);
}
unsigned int char_to_int(unsigned char * strunsigned int len){
unsigned int tmpijtmp2;
for(i=0;i {
tmp2=1;
for(j=i+1;j {
tmp2*=10;
}
tmp+=((*str)*(tmp2));
str++;
}
return tmp;
}
unsigned char phone[18]={
12345678912345678
};
int main(int argc char *argv[]){
int fd = -1 count = 0;
struct termios oldtio;
int cnt = 0 i = 0;
printf(“start\n“);
if((fd = init_tty(&oldtio)) < 0) {
printf(“init tty fail so return....\n“);
return -1;
}
unsigned int phone_num;
unsigned int *phonep;
int count_time=115700;
while(1) {
send(fd ribao_init_msg 5);
phone_num=char_to_int(&phone[count%9]8);
// printf(“tty_send:%d\n“phone_num);
phonep=&phone_num;
char *char_phonep=(char *)phonep;
for(i=9;i>5;i--)
{
ack_test_msg[i]=*char_phonep;
char_phonep++;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
I.A.... 585 2018-08-14 17:01 uart_test\Makefile
I.A.... 473002 2018-08-14 17:01 uart_test\sq110_uart
I.A.... 3607 2018-08-14 17:01 uart_test\sq110_uart.c
I.A.... 479 2018-08-14 17:01 uart_test\sq110_uart.h
I..D... 0 2018-08-14 17:01 uart_test
----------- --------- ---------- ----- ----
477673 5
- 上一篇:safety mechanisms
- 下一篇:使用Qt实现网页自动刷新工具 demo
相关资源
- Scratch源码
- E4A无障碍跨程序操作类库(带源码、
- 设备管理系统源码
- 安卓wifi直连app源码
- 我的世界源码(易语言版)
- labview编程软件滤波器以及编写程序设
- 我的界面(visual foxpro)源码
- 易语言:一键cf基址源码
- The Secret Path 3D 3D魔方迷宫[源码][scra
- scratch垃圾分类源码(最终版本).sb
- 安卓QQ6.71协议源码易语言,qq协议源码
- 编译原理实验工具及参考源码(lex&
- E盾偷后台工具源码
- UNIX/LINUX编程实践教程的源码
- 十以内加减法练习 powerbuilder源码
- 农场开发项目
- OCR源码
- PLC上位机编程软件
- 用foobar2000听google音乐[更新一下]
- 学生信息管理系统源码
- 用VC 编写的仿QQ聊天室程序源代码
- 毕业论文之温度传感器DS18B20(源码
- 可自定义导航网站源码
- 栅栏填充算法源码(VC)
- msp430F149操作红外接收模块源码
- [免费]图像识别c 源码
- 周易排盘源码
- RSA算法源码
- 一个人脸识别程序源码
- 编译原理课程设计:词法语法编译器
评论
共有 条评论