ARM9里的LINUX系统的串口设备节点ttys#用fd open打不开是怎么回事,有没有知道的错误提示
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<...
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#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);/*INPUT*/
options.c_oflag &=~OPOST;/*OUTPUT*/
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
有哪位大大帮忙解决下,急!!!!!! 展开
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#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);/*INPUT*/
options.c_oflag &=~OPOST;/*OUTPUT*/
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
有哪位大大帮忙解决下,急!!!!!! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |