发表于:2003-09-24 15:01:00
楼主
十分感谢回复!!
还有一个问题,那源程序是否需要改动。以下为源代码:
# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include <fcntl.h>
# include <errno.h>
# include <termios.h>
# include <sys/types.h >
# include <sys/stat.h>
# include <stdlib.h>
// # define BAUDRATE b300
// # define BAUDRATE b9600
// # define BAUDRATE b57600
# define MODEMDEVICE "/dev/ttyS0"
# define _POSIX_SOURCE 1
# define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
int cnvbaudrate(char* pszbaudrate) {
switch (atoi(pszbaudrate))
{
case 50:
return B50;
case 75:
return B75;
case 110:
return B110;
case 134:
return B134;
case 150:
return B150;
case 200:
return B200;
case 300:
return B300;
case 600:
return B600;
case 1200:
return B1200;
case 1800:
return B1800;
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
default:
return B57600;
}
}
int main (int argc,char** argv)
{
int fd,c,res,nsize=1024,allsize=0,BAUDRATE;
struct termios oldtio,newtio;
char szsendbuf [1024];
int filelength;
FILE * pfile;
struct stat st;
fd = open (MODEMDEVICE,O_RDWR|O_NOCTTY);
if (fd<0)
{
perror (MODEMDEVICE);
printf ("Error in open COM1/n");
exit (-1);
}
tcgetattr(fd,&oldtio);
bzero(&newtio,sizeof(newtio));
/*串口新属性设置*/
//newtio.c_cflag = BAUDRATE |/* CRTSCTS | */ CS8;// | CLOCAL | CREAD;
BAUDRATE=B57600;
//printf("argc= %d,argv[1]=%s\n",argc,grgv[1]);
if(argc >=2)
BAUDRATE = cnvbaudrate(argv[1]);
cfsetispeed(&newtio,BAUDRATE);
cfsetospeed(&newtio,BAUDRATE);
newtio.c_cflag|=CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
//newtio.c_lflag = ICANON;//|ECHO;
newtio.c_cc[VINTR] = 0; /*Ctrl-c*/
newtio.c_cc[VQUIT] = 0; /*Ctrl-\*/
newtio.c_cc[VERASE] = 0; /*del*/
newtio.c_cc[VKILL] = 0; /*@*/
newtio.c_cc[VEOF] = 4; /*Ctrl-d*/
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VSWTC] = 0; /*`\0`*/
newtio.c_cc[VSTART] = 0; /*Ctrl-q*/
newtio.c_cc[VSTOP] = 0; /*Ctrl-s*/
newtio.c_cc[VSUSP] = 0; /*ctrl-z*/
newtio.c_cc[VEOL] = 0; /*`\0*/
newtio.c_cc[VREPRINT] = 0; /*ctrl-r*/
newtio.c_cc[VDISCARD] = 0; /*ctrl-u*/
newtio.c_cc[VWERASE] = 0; /*ctrl-w*/
newtio.c_cc[VLNEXT] = 0; /*ctrl-v*/
newtio.c_cc[VEOL2] = 0; /*`\0`*/
tcflush (fd,TCIFLUSH);
tcsetattr (fd,TCSANOW,&newtio);
stat ("test.bmp",&st);
if ((pfile = fopen ("test.bmp","r"))==NULL)
{
printf ("Can`t find the file test.bmp!");
return 0;
}
filelength=st.st_size;
printf("file size :%d\n",filelength);
//write (fd,(char *)&filelength,sizeof(int));
while (nsize==1024)
{
bzero(szsendbuf,1024);
nsize=fread(szsendbuf,1,1024,pfile);
nsize=write(fd,szsendbuf,nsize);
allsize+=nsize;
if(! (allsize&0x3fff))
printf("all_size:%dthis_time:%dtimes:%d\n",allsize,nsize,allsize/1024);
}
fclose (pfile);
tcsetattr (fd,TCSANOW,&oldtio);
close(fd);
}