|
|
我在同一PC下,同一下串口下,发送和接收数据!
源代码:
#include <assert.h>
#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h> /*错误号定义*/
#define RCV_LEN 32
#define SND_LEN 32
#define TIMES 3
void receive(int fd)
{int errorcode;
char rec_buf[32];
errorcode=read(fd,rec_buf,RCV_LEN);
if(errorcode) printf("The received string is %s\n",rec_buf);
}
void send (int fd)
{int nwrite;
char snd_buf[32];
printf(" lease enter a string to send(<=32 characters):\n");
scanf ("%s",snd_buf);
nwrite=write(fd,snd_buf,SND_LEN);
if(nwrite==-1) printf("error");
if(nwrite) printf("You have already send %s ",snd_buf);
}
void set_speed(int fd, int speed)
{
int i;
int status;
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, };
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr)
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr);
cfsetospeed(&Opt, speed_arr);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(0);
}
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 (0);
}
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 (0);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (0);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (0);
}
return (1);
}
int main()
{
int fd,i;
char pathname[11]="/dev/ttyS0";
printf("now start。。。。。。\n:");
fd=open(pathname,O_RDWR,0666);
printf("fd=%d\n",fd);
if(fd) set_speed(fd,19200);
else {printf("Can't Open Serial Port!\n"); exit(0); }
if (set_Parity(fd,8,1,'N')== 0)
{printf("Set Parity Error\n"); exit(1); }
for (i=0;i<TIMES;i++) { send(fd); receive(fd); }
return 0;
}
运行结果:
now start。。。。。。
fd=3
Please enter a string to send(<=32 characters):
aadd //我的输入,然后回车
aadd? ? //回显的内容,然后就没有动静了!!
这个到底是什么原因阿??? |
|