该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
发送程序如下:
#include
#include
#include /* 文件操作 */
#include /* ioctl()函数 */
#include /* file controle */
#include /*PPSIX terminal controle */
#include /* error */
#include
#include
#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
**************************************************************************/