有精通嵌入式linux和c语言的大神吗?求帮助
arm吧
全部回复
仅看楼主
level 1
题目:基于ARM 和 Linux 的 RS232 通讯系统设计与实现
要求:在嵌入式 ARM 平台及 Linux 环境下编程 RS232 通信程序,实现两个
ARM 平台或 ARM 与 PC 机之间 RS232 通信。
2013年12月07日 12点12分 1
level 1
我有接收程序如下:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h> /* 文件操作 */
#include <sys/ioctl.h> /* ioctl()函数 */
#include <fcntl.h> /* file controle */
#include <termios.h> /*PPSIX terminal controle */
#include <errno.h> /* error */
#include <stdlib.h>
#include <unistd.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/************ set baud ********************/
void set_speed(int fd, int speed)
{
int status;
struct termios Opt;
tcgetattr(fd, &Opt); // get attribute of serial port
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed);
cfsetospeed(&Opt, speed);
status = tcsetattr(fd, TCSANOW, &Opt); // set attribute
if (status != 0)
{
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
/*********** set data format: databits,stopbits and parity **********/
int set_data_format(int fd,int databits,int stopbits,int parity)
{
struct termios opt;
if( tcgetattr(fd, &opt) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
opt.c_cflag &= ~CSIZE;
switch (databits)
{
case 5:
opt.c_cflag |= CS5;
break;
case 6:
opt.c_cflag |= CS6;
break;
case 7:
opt.c_cflag |= CS7;
break;
case 8:
opt.c_cflag |= CS8;
break;
default:
printf("Unsupported data size\n");
return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
opt.c_cflag &= ~PARENB; /* Clear parity enable */
opt.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
opt.c_cflag |= (PARODD | PARENB); /* parity checking */
opt.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
opt.c_cflag |= PARENB; /* Enable parity */
opt.c_cflag &= ~PARODD; /* */
opt.c_iflag |= INPCK; /* Disnable parity checking */
break;
default:
printf("Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case 1:
opt.c_cflag &= ~CSTOPB;
break;
case 2:
opt.c_cflag |= CSTOPB;
break;
default:
printf("Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
opt.c_iflag |= INPCK;
opt.c_cc[VTIME] = 100; // 10 seconds
opt.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &opt) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int main()
{
int fd;
int n=-1;
char recbuff[100]={};
fd = open("/dev/ttyS1", O_RDWR); /* 打开设备 */
if(fd == -1)
{
printf("\nCan't open ttyS1 device!\n");
exit(-1);
}
set_speed(fd,B115200); // set baud 115200
if (set_data_format(fd,8,1,'N')== FALSE)
{
printf("Data format Error!\n");
exit(1);
}
printf("Please Enter 10 Bytes\n");
while(1)
{
n=read(fd,recbuff, 10);
if(n>0)
write(fd,recbuff,10);
}
close(fd);
return 0;
}
/**************************************************************************
** End Of File
**************************************************************************/
2013年12月07日 13点12分 2
level 1
发送程序如下:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h> /* 文件操作 */
#include <sys/ioctl.h> /* ioctl()函数 */
#include <fcntl.h> /* file controle */
#include <termios.h> /*PPSIX terminal controle */
#include <errno.h> /* error */
#include <stdlib.h>
#include <unistd.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/************ set baud ********************/
void set_speed(int fd, int speed)
{
int status;
struct termios Opt;
tcgetattr(fd, &Opt); // get attribute of serial port
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed);
cfsetospeed(&Opt, speed);
status = tcsetattr(fd, TCSANOW, &Opt); // set attribute
if (status != 0)
{
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
/*********** set data format: databits,stopbits and parity **********/
int set_data_format(int fd,int databits,int stopbits,int parity)
{
struct termios opt;
if( tcgetattr(fd, &opt) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
opt.c_cflag &= ~CSIZE;
switch (databits)
{
case 5:
opt.c_cflag |= CS5;
break;
case 6:
opt.c_cflag |= CS6;
break;
case 7:
opt.c_cflag |= CS7;
break;
case 8:
opt.c_cflag |= CS8;
break;
default:
printf("Unsupported data size\n");
return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
opt.c_cflag &= ~PARENB; /* Clear parity enable */
opt.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
opt.c_cflag |= (PARODD | PARENB); /* parity checking */
opt.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
opt.c_cflag |= PARENB; /* Enable parity */
opt.c_cflag &= ~PARODD; /* */
opt.c_iflag |= INPCK; /* Disnable parity checking */
break;
default:
printf("Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case 1:
opt.c_cflag &= ~CSTOPB;
break;
case 2:
opt.c_cflag |= CSTOPB;
break;
default:
printf("Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
opt.c_iflag |= INPCK;
opt.c_cc[VTIME] = 100; // 10 seconds
opt.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &opt) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int main()
{
int fd;
int n=-1;
char recbuff[100]={};
fd = open("/dev/ttyS1", O_RDWR); /* 打开设备 */
if(fd == -1)
{
printf("\nCan't open ttyS1 device!\n");
exit(-1);
}
set_speed(fd,B115200); // set baud 115200
if (set_data_format(fd,8,1,'N')== FALSE)
{
printf("Data format Error!\n");
exit(1);
}
printf("Please Enter 10 Bytes\n");
memset(recbuff,0,sizeof(recbuff));
while(1)
{
if(fgets(recbuff,sizeof(recbuff),stdin)==NULL)
{
printf("error");
exit(1);
}
write(fd,recbuff,10);
}
close(fd);
return 0;
}
/**************************************************************************
** End Of File
**************************************************************************/
2013年12月07日 13点12分 3
level 1
但是这两个程序好像都有点问题,谁能帮我修改一下,跪求。。。。。。。。。。。。。
貌似两段程序都是要修改printf("Please Enter 10 Bytes\n")以下的内容
2013年12月07日 13点12分 4
1