#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <math.h>
#include <time.h>
#define max_buffer_size 100
const char *const days[] = {"SUN", "MON", "TUES", "WED", "THUR", "FIR", "SAT"};
int fd;
int s;
time_t sec;
struct tm *n;
int open_serial(int k)
{
if (k=0)
{
fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY);
perror("open /dev/ttyUSB0");
}
else
{
fd=open("/dev/ttyUSB1",O_RDWR|O_NOCTTY);
perror("open /dev/ttyUSB1");
}
if(fd==-1)
return -1;
else
return 0;
}
int main()
{
char hd[max_buffer_size],*rbuf,*rbuftemp;
int flag_close,retv,i,ncount=0;
struct termios opt;
int realdata=0;
open_serial(0);
tcgetattr(fd,&opt);
cfmakeraw(&opt);
cfsetispeed(&opt,B115200);
cfsetospeed(&opt,B115200);
tcsetattr(fd,TCSANOW,&opt);
rbuf=hd;
printf("ready for receiving data...\n");
while(1)
{
if((retv = read(fd,rbuf,512))>0)
{
sec = time(NULL);
n = localtime(&sec);
printf("in %d.%d.%d.%s.%d:%d:%d the receiving data is :",
n->tm_year + 1900,
n->tm_mon + 1,
n->tm_mday,
days[n->tm_wday],
n->tm_hour,
n->tm_min,
n->tm_sec);
for(i=0; i<retv; i++)
{
printf("%c",hd[i]);
}
}
if(retv==-1)
{
perror("read");
}
}
// retv=read(fd,rbuf,1);
// if(retv==-1)
// {
// perror("read");
// }
// while(*rbuf!='\n')
// {
//
// ncount+=1;
// rbuf++;
// if(read(fd,rbuf,1)== -1) perror("read");
//
// }
//
// printf("The data received is:");
//
// for(i=0;i<ncount;i++)
// {
// printf("%c",hd[i]);
// }
// printf("\n");
// flag_close=close(fd);
// if(flag_close == -1) printf("Close the Device failur��\n");
// return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
ttys.rar_ttys linux
共1个文件
c:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 68 浏览量
2022-09-24
20:24:48
上传
评论
收藏 947B RAR 举报
温馨提示
Linux下串口编程,打印接收到的数据和本地时间
资源推荐
资源详情
资源评论
收起资源包目录
ttys.rar (1个子文件)
ttys.c 2KB
共 1 条
- 1
资源评论
局外狗
- 粉丝: 66
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功