|
看到别人的帖子,附后。
但是我send.c能够正常编译,recv.c却不能,不知道是为什么?
编译时出现提示(我将recv.c更名为jieshou.c)
[root@localhost code]# gcc -c jieshou.c
jieshou.c: In function `setTerm':
jieshou.c:256: storage size of `term_attr' isn't known
jieshou.c:258: `TCGETA' undeclared (first use in this function)
jieshou.c:258: (Each undeclared identifier is reported only once
jieshou.c:258: for each function it appears in.)
jieshou.c:266: `XCLUDE' undeclared (first use in this function)
jieshou.c:282: `TCSETAW' undeclared (first use in this function)
jieshou.c:284: `TCFLSH' undeclared (first use in this function)
jieshou.c: In function `resetTerm':
jieshou.c:300: `TCSETAW' undeclared (first use in this function)
jieshou.c: At top level:
jieshou.c:250: storage size of `oterm_attr' isn't known
[root@localhost code]#
我进行如下步骤
1. 编译生成可执行文件:
gcc -o send send.c
2. 建立脚本文件SEND(可用vi), 内容为
#!/bin/sh
cat $1 | send /dev/tty2a 9600
3. 使脚本变为可执行
chmod +x SEND
4. 传文件a.c:
SEND a.c
但是却提示
bash: SEND: command not found
这是为啥呢?
共四个文件: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,2048)) >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;
}
}
谢谢! |
|