|
发表于 2005-5-19 11:40:57
|
显示全部楼层
拿这个去试试,不行就慢慢调吧,二进制问题其实就是串口的raw模式问题,设为raw模式就行了。
[code:1]
#include <termios.h>
#include <termio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int SerialInit(char *dev)
{
int fd;
struct termios Opt;
fd=open(dev,O_RDWR);// | O_NOCTTY);
if(fd<=0)
return -1;
if ( tcgetattr( fd,&Opt) != 0)
return -1;
cfsetispeed(&Opt, B9600);
cfsetospeed(&Opt, B9600);
Opt.c_cflag |= (CLOCAL | CREAD);
Opt.c_lflag &=~(ICANON | ECHO | ECHOE | ISIG); //ÉèÖÃΪRAWģʽ
Opt.c_oflag &=~OPOST; //ÉèÖÃΪRAWģʽ
Opt.c_oflag &=~VTDLY;
Opt.c_iflag &=~(IXON | ICRNL); //ͨѶÖв»´¦Àí¿ØÖÆ×Ö·û
Opt.c_cc[VTIME] = 1;
Opt.c_cc[VMIN] = 0;
Opt.c_cflag &= ~CSIZE;
Opt.c_cflag |= CS8;
Opt.c_cflag &= ~PARENB; /* Clear parity enable */
Opt.c_iflag &= ~INPCK; /* Enable parity checking */
Opt.c_cflag &= ~CSTOPB;
tcflush(fd,TCIOFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&Opt) != 0)
{ perror("SetupSerial 3");
return (0);
}
tcflush(fd,TCIOFLUSH); /* Update the options and do it NOW */
return fd;
}
[/code:1] |
|