#include
#include
#include
#include
#include
#include
#include
#define TRUE 1
#define FALSE 0
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[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
{
perror("tcsetattr fd1");
exit( 1 ) ;
}
}
tcflush(fd,TCIOFLUSH);
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if( tcgetattr( fd,&options) != 0)
{
perror("Setup Serial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
options.c_lflag &=~(ICANON |ECHO|ECHOE|ISIG);
options.c_oflag &=~OPOST;
return (TRUE);
}
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR|O_NOCTTY);
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
int main( int argc, char *argv[] )
{
int nLength;
char cmd[50]="abc123\0";
char buff[512] ;
char *dev="/dev/ttyS0";
int fd=OpenDev(dev);
set_speed(fd,115200);
if (set_Parity(fd,8,1,'N')== FALSE) {
printf("set Parity Error\n");
exit(0);
}
while(1)
{
printf("send:%s\n",cmd);
write(fd,cmd,sizeof(cmd));
sleep(1);
}
close(fd);
return TRUE;
}
这个是我写的程序,我在PC机上的linux系统上往ARM板子上发送数据,可以发送数据,但是我把程序烧到ARM板子上,重ARM板子上往PC机上发数据却提示我打不开端口,错误提示是
can't open serial port:input/output error
setup serial:bad file descriptor
set parity Error
有哪位大大帮忙解决下,急!!!!!!