怎么也读不出电表数据,源代码如下:请大大们指点啊,感激不尽,小弟是新人,也不知道在哪看自己的积分,问题解决了有分送分,没分送q币
#include
#include
#include
#include
#include
#include
#include
#include
#define FALSE -1
#define TRUE 0
/*********************************************************************/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR );
if (-1 == fd)
{
perror("Can''t Open Serial Port");
return -1;
}
else
{
printf("%s\n", Dev);
return fd;
}
}
struct termio
{
unsigned short c_iflag;
unsigned short c_oflag;
unsigned short c_cflag;
unsigned short c_lflag;
unsigned char c_line;
//unsigned char c_cc[];
};
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,};
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for(i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
{
if(speed == name_arr)
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr);
cfsetospeed(&Opt, speed_arr);
status = tcsetattr(fd, TCSANOW, &Opt);
if(status != 0)
{
perror("tcsetattr fd1");
return;
}
tcflush(fd, TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /**/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data sizen"); return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;break;
default:
fprintf(stderr,"Unsupported parityn");
return (FALSE);
}
/* */
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bitsn");
return (FALSE);
}
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0;
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int main(int argc, char **argv){
int i;
int fd;
int nread;
int nwrite;
char buff_write[14];
char buff_read[14];
char buff_wake[2];
char sendbuff[512];
char recvbuff[512];
char *dev = "/dev/ttyS0";
fd = OpenDev(dev);
set_speed(fd,1200);
if (set_Parity(fd,8,1,'E') == FALSE)
{
printf("Set Parity Errorn");
exit (0);
}
buff_wake[0] = 0xFE;
buff_wake[1] = 0xFE;
buff_write[0] = 0x68;
buff_write[1] = 0x08;
buff_write[2] = 0x1E;
buff_write[3] = 0x02;
buff_write[4] = 0x1E;
buff_write[5] = 0x00;
buff_write[6] = 0x00;
buff_write[7] = 0x68;
buff_write[8] = 0x01;
buff_write[9] = 0x02;
buff_write[10] = 0x43;
buff_write[11] = 0xC3;
buff_write[12] = 0x1F;
buff_write[13] = 0x16;
// int nwrite1 = write(fd,buff_wake,2);
nwrite = write(fd,buff_write,14);
// while(1)
// {
nread = read(fd,recvbuff,512);
recvbuff[nread+1] = '\0';
printf("%s",recvbuff);
// }
for(i = 0; i < 512; i++)
{
printf("\n%c",recvbuff);
}
close(fd);
exit (0);
}