现在手里有一块s3c2440友善之臂的开发板,这个开发板有三个串口,其中串口0和串口1的硬件电路是一样的,由s3c2440arm芯片出来后接RS232电平转换芯片后输出。
不同点:串口0 作为默认的控制台终端,打印信息。
我在应用程序中,可以直接打开串口0 ,然后对其读写。但是直接打开串口1,进行读写失败。
fd=open("/dev/ttySAC0",O_RDWR);
if(fd<0)
{
printf("-----open serial failed \n");
return 0;
}
write(fd,name,strlen(name));
write(fd,number,strlen(number));
close(fd);
以上是在应用程序中用串口0读写,没问题。
fd=open("/dev/ttySAC1",O_RDWR);
if(fd<0)
{
printf("-----open serial failed \n");
return 0;
}
write(fd,name,strlen(name));
write(fd,number,strlen(number));
close(fd);
如果换成串口1,再读写就不行了,在终端看不见信息。
/****************************************通过这个样的方法也可以用**************/
#include /*鏍囧噯杈撳叆杈撳嚭瀹氫箟*/
//#include /*鏍囧噯鍑芥暟搴撳畾涔?/
#include /*Unix鏍囧噯鍑芥暟瀹氫箟*/
#include /**/
#include /**/
#include /*鏂囦欢鎺у埗瀹氫箟*/
#include /*PPSIX缁堢鎺у埗瀹氫箟*/
#include /*閿欒鍙峰畾涔?/
/***@brief 璁剧疆涓插彛閫氫俊閫熺巼
@param fd 绫诲瀷 int 鎵撳紑涓插彛鐨勬枃浠跺彞鏌?
*@param speed 绫诲瀷 int 涓插彛閫熷害
*@return void/
#define FALSE 1
#define TRUE 0
char *recchr="We received:\"";
int speed_arr[] = { B921600, B460800, B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {921600, 460800, 230400, 115200, 57600, 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");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
/**
@brief 璁剧疆涓插彛鏁版嵁浣嶏紝鍋滄浣嶅拰鏁堥獙浣?
*@param fd 绫诲瀷 int 鎵撳紑鐨勪覆鍙f枃浠跺彞鏌?
*@param databits 绫诲瀷 int 鏁版嵁浣? 鍙栧€?涓?7 鎴栬€?
@param stopbits 绫诲瀷 int 鍋滄浣? 鍙栧€间负 1 鎴栬€?
@param parity 绫诲瀷 int 鏁堥獙绫诲瀷 鍙栧€间负N,E,O,,S
*/
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 size\n");
return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable /
options.c_iflag &= ~INPCK; / Enable parity checking /
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); / 璁剧疆涓哄鏁堥獙*/
options.c_iflag |= INPCK; /* Disnable parity checking /
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; / Enable parity /
options.c_cflag &= ~PARODD; / 杞崲涓哄伓鏁堥獙*/
options.c_iflag |= INPCK; /* Disnable parity checking /
break;
case 'S':
case 's': /*as no parity/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
/* 璁剧疆鍋滄浣?/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 0; // 15 seconds
options.c_cc[VMIN] = 8;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int uartw(int fd, char *buf,int length)
{
int len=0;
set_speed(fd,115200);
if (set_Parity(fd,8,1,'N')== FALSE)
{
printf("Set Parity Error\n");
exit(1);
}
length = write(fd, buf, strlen(buf));
return len;
}
int uartr(int fd,void data, int datalength)
{
set_speed(fd,115200);
if (set_Parity(fd,8,1,'N')== FALSE)
{
printf("Set Parity Error\n");
exit(1);
}
return (read (fd, data, datalength));
}
/*
@breif 鎵撳紑涓插彛
*/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{ /璁剧疆鏁版嵁浣嶆暟*/
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
/**
*@breif main()
*/
int main(int argc, char *argv[])
{
int fd;
int nread;
char buff[512];
//char *dev ="/dev/ttyS0";
char *dev ="/dev/ttySAC1";
char recchr[512];
//int device, c;
//sleep(1);
fd = OpenDev(dev);
if (fd>0){
//set_speed(fd,115200);
}
else{
printf("Can't Open Serial Port!\n");
exit(0);
}
/*
while(1)
{
if((nread = uartr(fd,buff,512))>0)
{
printf("\n the length of reading if %d\n",nread);
buff[nread]='\0';
printf("\n %s",buff);
}
}
scanf("%s",recchr) ;
uartw(fd, recchr, strlen(recchr));
*/
if (0== atoi(argv[1]))
uartw(fd, argv[2], strlen(argv[2]));
else if (1 == atoi(argv[1]))
{
if((nread = uartr(fd,buff,512))>0)
{
printf("\n the length of reading if %d\n",nread);
buff[nread]='\0';
printf("\n %s",buff);
}
}
else
printf("the parameter of argv[1] is %s\n",argv[1]);
close(fd);
exit(0);
}
我想问的问题就是,怎么修改(u-boot 内核或者驱动或者别的地方),可以像程序1一样读写串口1(就是程序片段2能够正确执行)?