|

楼主 |
发表于 2004-3-8 18:28:26
|
显示全部楼层
不好意思,由于当时匆忙,没有好好校验一下代码。现在看来发现了很多语法错误。:-(
现在将校正过的发表如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
///#include <sys/signal.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define MODEDEVICE "/dev/ttyS0"
volatile int STOP=FALSE;
int open_Com(char devicename[])
{ int fd;
char *deV;
strcpy(deV,devicename);
fd=open(deV , O_RDWR | O_NOCTTY | O_NDELAY);
if(-1==fd)
{printf("Can't open s%\n",deV);
return(-1);
}
return 0;
}
int set_ioSpeed(int fd ,int baud_speed)
{ int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300, };
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (baud_speed == name_arr)
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr);
cfsetospeed(&Opt, speed_arr);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
{
perror("tcsetattr fd\n");
return(FALSE);
}
return(TRUE);
}
tcflush(fd,TCIOFLUSH);
}
}
int set_Com(int fd , int databits , int stopbits , int paritybits)
{int status;
struct termios oldtios , newtios;
//fd=open_Com(MODEDEVICE);
if(fd==-1)
{perror("Can't open Com2");
return(FALSE);
}
tcgetattr(fd,&oldtios);
bzero(&newtios,sizeof(newtios));
newtios.c_cflag &=~CSIZE;
newtios.c_cflag =(CLOCAL | CREAD);
switch(databits)
{case 7:
newtios.c_cflag |=CS7;
break;
case 8:
newtios.c_cflag |=CS8;
break;
default:
printf("Unrecognizable databits!\n");
return(FALSE);
}
switch(stopbits)
{case 1:
newtios.c_cflag &=~CSTOPB;
break;
case 2:
newtios.c_cflag |=CSTOPB;
break;
default:
printf("unsupportable stopbits\n ");
return(FALSE);
}
switch(paritybits)
{case 'n':
case 'N':
newtios.c_cflag &=~PARENB;
newtios.c_iflag |=IGNPAR;
break;
case 'o':
case 'O':
newtios.c_cflag |=PARENB;
newtios.c_cflag |=PARODD;
newtios.c_iflag |=INPCK;
break;
case 'e':
case 'E':
newtios.c_cflag |=PARENB;
newtios.c_cflag &=~PARODD;
newtios.c_iflag |=INPCK;
break;
case 's':
case 'S':
newtios.c_cflag &=~PARENB;
newtios.c_cflag &=~CSTOPB;
break;
default:
printf("Unsupportable paritybits!\n");
return(FALSE);
}
newtios.c_lflag =0; ////set non-echo
newtios.c_oflag=0;
newtios.c_cc[VMIN]=1;
newtios.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtios);
tcsetattr(1,TCSANOW,&newtios);
return;
}
int read_Com(char *devicename)
{ char buf[512];
int com_read;
int fd;
fd=open_Com(MODEDEVICE);
if(fd==0) ////changed "1"->"0"
{
com_read=read(fd , buf , 512);
if(-1==com_read)
{
printf("Can't read!\n");
return(FALSE);
}
/// fcntl(0,F_SETFL,0);///ÎÊÌ⣺ÏÖÔÚ¿ÉÒÔÖ±½Ó¶Ô´®¿Ú½øÐжÁ²Ù×÷ÁËô£¿
return(TRUE);
}
printf("Can't open com!\n");
return(FALSE);
}
int write_Com(char *devicename)
{ char buf[512];
int com_write;
int fd;
fd=open_Com(MODEDEVICE);
if(fd==0) /////changed "1"->"0"
{
com_write=write(fd , buf ,512);
if(-1==com_write)
{printf("Can't write!\n");
return(FALSE);
}
return(TRUE);
}
printf("Can't write com!\n");
return(FALSE);
}
int main(int argc , char *argv)
{int fd;
int res0, res1,res2;
////char buffer[512];
fd=open_Com(MODEDEVICE);
if(0==fd)
{
res0=set_ioSpeed(fd , 19200);
if(TRUE==res0)
{
res1==set_Com(fd , 8 , 1 , 'n');
if(TRUE==res1)
{
res2==read_Com(MODEDEVICE);
if(res2==TRUE)
{
////close(fd);
return(TRUE);
}
}
}
}
return(FALSE);
} |
|