简单的串口通信实例

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //MOXA的API头文件(本函数用到的是多串口传输方式的选择)
#defineBLEN 1024
#define MAX_COUNT 9999
/*波特率表*/
unsigned short btable[]={B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400};
int main(int argc, char *argv[])
{
24
pid_t pid; //进程号的类型定义
int iFlag1,iFlag2; //数据传输标志
struct termios term1;//串口设置参数结构体
struct termios term2;//
int i,j,k,l;
int iPortFD1;//串口1文件描述符
int iPortFD2; //串口2文件描述符
char cBuf[20];//串口设备名称
int iLen1;
int iLen2;
char txbuffer1[200];//串口1发送缓冲区
char rxbuffer1[200]; //串口1接收缓冲区
char txbuffer2[200]; //串口2发送缓冲区
char rxbuffer2[200]; //串口2接收缓冲区
printf("This is two serial ports test program\n");
/*open serial 1*/
i=0;
sprintf(cBuf, "/dev/ttyM%d", i); //?
iPortFD1 = open(cBuf, O_RDWR | O_NDELAY); //cBuf 要打开文件的文件名称,例如/dev/ttyS0 open成功返回文件描述符,如果失败返回-1
/*************************************************************
O_RDWR:以读写方式;
O_NDELAY:忽略DCD(数据载波检测)
**************************************************************/
if ( iPortFD1 < 0 )
{
printf("Open serial port 1 fail [%d] !\n", errno);
return -1;
}
else
{
printf("Open serial port 1 success!!\n");
}
iLen1 = RS232_MODE; //???????
ioctl(iPortFD1, MOXA_SET_OP_MODE, &iLen1);//以RS232方式打开
term1.c_cflag = B9600|CS8|CREAD|CLOCAL;
/*************************************************************
B9600:波特率为9600;
25
CS8:8数据位
CREAD:Enable receiver
CLOCAL:Local line - do not change "owner" of port
**************************************************************/
term1.c_iflag = 0;
term1.c_oflag = 0;
term1.c_lflag = 0;
term1.c_cc[VMIN] = 0;
term1.c_cc[VTIME] = 0;
tcflush(iPortFD1, 2);// Flushes the input and/or output queue.
tcsetattr(iPortFD1, TCSANOW, &term1); //设置终端的相关参数
/*open serial 2*/
i=1;
sprintf(cBuf, "/dev/ttyM%d", i);
iPortFD2 = open(cBuf, O_RDWR | O_NDELAY);
if ( iPortFD2 < 0 )
{
printf("Open serial port 2 fail [%d] !\n", errno);
return -1;
}
else
{
printf("Open serial port 2 success!!\n");
}
iLen2 = RS232_MODE;
ioctl(iPortFD2, MOXA_SET_OP_MODE, &iLen2);
term2.c_cflag = B9600|CS8|CREAD|CLOCAL;
term2.c_iflag = 0;
term2.c_oflag = 0;
term2.c_lflag = 0;
term2.c_cc[VMIN] = 0;
term2.c_cc[VTIME] = 0;
tcflush(iPortFD2, 2);
tcsetattr(iPortFD2, TCSANOW, &term2);
iFlag1 = 1;
iFlag2 = 1;
26
if ((pid=fork())>0)
{
while(iFlag1)
{
iLen1 = read(iPortFD

1, rxbuffer1, 10);//从串口1读取10个字符的数据送到rxbuffer1
if(iLen1>0)
{
printf("\n Port1 receive data:");
for(i=0;i{
printf("%c",rxbuffer1[i]);
if(rxbuffer1[i]>='a' && rxbuffer1[i]<='z' )
txbuffer1[i]=rxbuffer1[i]-32;
else if(rxbuffer1[i] >= 'A' && rxbuffer1[i] <= 'Z')
txbuffer1[i]=rxbuffer1[i]+32;
else
{
txbuffer1[i]='0';
iFlag1 = 0;
break;
}
}
write(iPortFD1,txbuffer1,iLen1);//将数据从txbuffer1发送到串口1
}
}
close(iPortFD1);
printf("\n Port1 closed!!\n");
}
else if (pid==0)
{
while(iFlag2)
{
//printf("this is child process\n");
iLen2 = read(iPortFD2, rxbuffer2, 10);
if(iLen2>0)
{
printf("\n Port2 receive data:");
27
for(j=0;j{
printf("%c",rxbuffer2[j]);
if(rxbuffer2[j]>='a' && rxbuffer2[j]<='z' )
txbuffer2[j]=rxbuffer2[j]-32;
else if(rxbuffer2[j] >= 'A' && rxbuffer2[j] <= 'Z')
txbuffer2[j]=rxbuffer2[j]+32;
else
{
txbuffer2[j]='0';
iFlag2 = 0;
break;
}
}
write(iPortFD2,txbuffer2,iLen2);
}
}
close(iPortFD2);
printf("\n Port2 closed!!\n");
exit(0); /*注意子进程必须用exit()退出运行*/
}
else
{
close(iPortFD1);
close(iPortFD2);
printf("fork error\n");
printf("Test over\n");
exit(0);
}
//printf("Test over\n");
return 0;
}

相关文档
最新文档