|
刚刚接触linux和串口编程的菜鸟,看到一个源程序实例,却不知道如何执行,请教各位大侠。
以下是在网上找到的例子。
请问一下,那4个文件中的 SEND和 RECV是干什么用的?用gcc编译了recv.c和 send.c,之后就不知道该如何进行下去了。只知道linux下要用minicom去进行串口程序的信息交换,可是实在不知道实际怎么操作,查了很久也没查到,也许我的问题实在太白痴,大家不要笑我阿。
我的任务,是要做一个串口程序,在linux系统下的2个串口之间进行信息传递,星期一就要交作业了,大侠们帮帮忙啊。。。
共四个文件:RECV recv.c SEND send.c
用串口线连接两台PC,第二根与第三根交叉
该程序我一直用于两台计算机交换文件,从未出错
----------------------SEND-------------------------
cat $1 |send /dev/tty2a 9600
----------------------send.c-------------------------
/* this program read from stdin and write to serial(specify in command parg) */
/* usage: send device baud, for example cat <file>|send /dev/ttyi01 9600 */
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<termio.h>
#include<time.h>
#include<signal.h>
#define C_SND 1
#define C_RCV 2
#define C_ACK 7
#define C_END 9
#define C_BEG 11
#define C_ERR 63
struct PACKHEAD
{
short cmd;
int len;
}pkhead;
main(int argc, char *argv[])
{
int fd;
int baud;
int len, cou;
char buf[2050];
if(argc != 3)
{
fprintf(stderr,"usage: send device baud\n");
return(1);
}
baud=atoi(argv[2]);
if(baud<0||baud>38400)
{
fprintf(stderr,"baud error\n");
return(1);
}
fd=open(argv[1],O_RDWR,0);
if(fd<0)
{
fprintf(stderr,"open <%s> error %s\n",argv[1],strerror(errno));
return(1);
}
if(setTerm(fd))
{
fprintf(stderr,"setterm error %s\n",strerror(errno));
return(1);
}
cou=0;
/* i am begin to send */
pkhead.cmd=C_BEG;
pkhead.len=0;
write_n(fd,&pkhead,sizeof(pkhead));
/* is server ready */
read_n(fd,&pkhead,sizeof(pkhead));
if(pkhead.cmd!=C_RCV)
{
fprintf(stderr,"recvive not ready");
return(1);
}
fprintf(stderr,"begin to send\n");
while( (len=read(0,buf,204) >0 )
{
/* send a pack */
pkhead.cmd=C_SND;
pkhead.len=len;
write_n(fd,&pkhead,sizeof(pkhead));
write_n(fd,buf,len);
/* has peer receive the pack */
read_n(fd,&pkhead,sizeof(pkhead));
if(pkhead.cmd!=C_ACK)
{
fprintf(stderr,"recvive not recv this pack");
return(1);
}
cou+=len;
fprintf(stderr,"send %d byte\n",len);
}
pkhead.cmd=C_END;
pkhead.len=0;
write_n(fd,&pkhead,sizeof(pkhead));
fprintf(stderr,"all send %d byte\n",cou);
}
/* 时间中断处理函数 */
void sig_alarm(int sig)
{
}
/* 设超时时间 */
static void (*old_sig)();
static int sig_flag=0;
int set_time_out(int sec)
{
if(sec<1)
return(1);
sig_flag=1;
old_sig=signal(SIGALRM,sig_alarm);
alarm(sec);
return(0);
}
/* 设超时时间 */
int reset_time()
{
if(sig_flag==1)
{
signal(SIGALRM,old_sig);
alarm(0);
}
return(0);
}
static struct termio oterm_attr;
int setTerm(int fd)
{
struct termio term_attr;
if(ioctl(fd,TCGETA,&oterm_attr)<0) return -1;
if(ioctl(fd,TCGETA,&term_attr)<0) return -1;
term_attr.c_iflag &=~(IXON|IXOFF|IXANY|INLCR|IGNCR|ICRNL|ISTRIP);
term_attr.c_lflag &=~(ISIG|ECHO|ICANON|NOFLSH|XCLUDE);
term_attr.c_cflag &=~CBAUD;
term_attr.c_cflag |=B19200;
term_attr.c_oflag &=~(OPOST|ONLCR|OCRNL);
term_attr.c_cc[VMIN]=1;
term_attr.c_cc[VTIME]=0;
if(ioctl(fd,TCSETAW,&term_attr)<0) return(-1);
if(ioctl(fd,TCFLSH,2)<0) return(-1);
return 0;
}
int resetTerm(int fd)
{
if(ioctl(fd,TCSETAW,&oterm_attr)<0) return -1;
return 0;
}
read_n(int fd, void *buf, int len)
{
int cou, ret;
ret=0;cou=0;
while(len>0)
{
ret=read(fd,(char*)buf+cou,len);
if(ret<1)
{
fprintf(stderr,"read device err %s\n",strerror(errno));
break;
}
cou+=ret;
len=len-ret;
}
}
write_n(int fd, void *buf, int len)
{
int cou, ret;
ret=0;cou=0;
while(len>0)
{
ret=write(fd,(char*)buf+cou,len);
if(ret<1)
{
fprintf(stderr,"write device err %s\n",strerror(errno));
break;
}
cou+=ret;
len=len-ret;
}
}
----------------------RECV-------------------------
recv /dev/tty2a 9600 >$1
----------------------recv.c-------------------------
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<time.h>
#include<signal.h>
#include<termios.h>
#define C_SND 1
#define C_RCV 2
#define C_ACK 7
#define C_END 9
#define C_BEG 11
#define C_ERR 63
struct PACKHEAD
{
short cmd;
int len;
}pkhead;
main(int argc, char *argv[])
{
int fd;
int baud;
int len, cou;
char buf[2050];
if(argc != 3)
{
fprintf(stderr,"usage: send device baud\n");
return(1);
}
baud=atoi(argv[2]);
if(baud<0||baud>38400)
{
fprintf(stderr,"baud error\n");
return(1);
}
fd=open(argv[1],O_RDWR,0);
if(fd<0)
{
fprintf(stderr,"open <%s> error %s\n",argv[1],strerror(errno));
return(1);
}
if(setTerm(fd))
{
fprintf(stderr,"setterm error %s\n",strerror(errno));
return(1);
}
cou=0;
/* are you ready */
read_n(fd,&pkhead,sizeof(pkhead));
if(pkhead.cmd!=C_BEG)
{
fprintf(stderr,"send not ready\n");
return(1);
}
/* i am ready to recv */
pkhead.cmd=C_RCV;
pkhead.len=0;
write_n(fd,&pkhead,sizeof(pkhead));
fprintf(stderr,"begin to receive\n");
while( 1 )
{
/* read a pack */
read_n(fd,&pkhead,sizeof(pkhead));
if(pkhead.cmd!=C_SND) break;
len=pkhead.len;
read_n(fd,buf,len);
write(1,buf,len);
/* i receive a pack */
pkhead.cmd=C_ACK;
pkhead.len=0;
write_n(fd,&pkhead,sizeof(pkhead));
cou+=len;
fprintf(stderr,"recv %d byte\n",len);
}
fprintf(stderr,"all recv %d byte\n",cou);
}
/* 时间中断处理函数 */
void sig_alarm(int sig)
{
}
/* 设超时时间 */
static void (*old_sig)();
static int sig_flag=0;
int set_time_out(int sec)
{
if(sec<1)
return(1);
sig_flag=1;
old_sig=signal(SIGALRM,sig_alarm);
alarm(sec);
return(0);
}
/* 设超时时间 */
int reset_time()
{
if(sig_flag==1)
{
signal(SIGALRM,old_sig);
alarm(0);
}
return(0);
}
static struct termio oterm_attr;
int setTerm(int fd)
{
struct termio term_attr;
if(ioctl(fd,TCGETA,&oterm_attr)<0) return -1;
if(ioctl(fd,TCGETA,&term_attr)<0) return -1;
term_attr.c_iflag &=~(IXON|IXOFF|IXANY|INLCR|IGNCR|ICRNL|ISTRIP);
term_attr.c_lflag &=~(ISIG|ECHO|ICANON|NOFLSH|XCLUDE);
term_attr.c_cflag &=~CBAUD;
term_attr.c_cflag |=B19200;
term_attr.c_oflag &=~(OPOST|ONLCR|OCRNL);
term_attr.c_cc[VMIN]=1;
term_attr.c_cc[VTIME]=0;
if(ioctl(fd,TCSETAW,&term_attr)<0) return(-1);
if(ioctl(fd,TCFLSH,2)<0) return(-1);
return 0;
}
int resetTerm(int fd)
{
if(ioctl(fd,TCSETAW,&oterm_attr)<0) return -1;
return 0;
}
read_n(int fd, void *buf, int len)
{
int cou, ret;
ret=0;cou=0;
while(len>0)
{
ret=read(fd,(char*)buf+cou,len);
if(ret<1)
{
fprintf(stderr,"read device err %s\n",strerror(errno));
break;
}
cou+=ret;
len=len-ret;
}
}
write_n(int fd, void *buf, int len)
{
int cou, ret;
ret=0;cou=0;
while(len>0)
{
ret=write(fd,(char*)buf+cou,len);
if(ret<1)
{
fprintf(stderr,"write device err %s\n",strerror(errno));
break;
}
cou+=ret;
len=len-ret;
}
} |
|