最近测试了一下我们公司apus板子的串口,看多个串口同时使用会不会有问题。所以就在网上找了一个例子,自己再改了一下。现在代码如下:
实现同时对串口uart1,uart2,uart3同时进行写操作。
#include /*标准输入输出定义*/
#include /*标准函数库定义*/
#include /*Unix标准函数定义*/
#include
#include
#include /*文件控制定义*/
#include /*PPSIX终端控制定义*/
#include /*错误号定义*/
#include
#include
#define COM1 "/dev/ttyS1"
#define COM2 "/dev/ttyS2"
#define COM3 "/dev/ttyS3"
#define FAILURE -1
#define SUCCESS 0
#define BUFF_SIZE 512
int rate_arr[] = {B57600, B38400, B19200, B9600, B2400};
int name_arr[] = { 57600, 38400, 19200, 9600, 2400 };
int serial_open (int& fd, char *pSerial)
{
fd = open (pSerial, O_RDWR);
if (FAILURE == fd)
{
perror ("Cannot open Serial!\n");
return FAILURE;
}
else
printf("Open %s success!\n",pSerial);
return SUCCESS;
}
int serial_speed_set (int& fd, int nBaud)
{
int i, status;
struct termios opt;
if ((i=tcgetattr (fd, &opt)) != SUCCESS)
{
perror("Cannot get speed status!\n");
return FAILURE;
}
for (i=0; i< (sizeof(rate_arr)/sizeof(rate_arr[0])); i++)
{
if (nBaud == name_arr[i])
{
tcflush (fd, TCIOFLUSH);
cfsetispeed (&opt, rate_arr[i]);
cfsetospeed (&opt, rate_arr[i]);
status = tcsetattr (fd, TCSANOW, &opt);
if (status != SUCCESS)
{
perror ("Baud setting error!\n");
return FAILURE;
}
tcflush (fd, TCIOFLUSH);
}
}
return SUCCESS;
}
int serial_parity_set (int& fd, int databits, int stopbits, int parity)
{
struct termios options;
if (tcgetattr(fd, &options) != SUCCESS)
{
perror ("tcgetattr error!\n");
return FAILURE;
}
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 FAILURE;
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARENB|PARODD);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 's':
case 'S':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf (stderr, "Unsupported parity\n");
return FAILURE;
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf (stderr, "Unsupported stop bits\n");
return FAILURE;
}
/*set input parity option*/
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush (fd, TCIFLUSH);
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0;
options.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG);
options.c_oflag&=~OPOST;
if (tcsetattr(fd, TCSANOW, &options) != SUCCESS)
{
perror ("set parity error!\n");
return FAILURE;
}
return SUCCESS;
}
int serial_write (int& fd,char *pBuf,int nLength)
{
//int nLength;
int nByte;
nByte = write (fd, pBuf, nLength);
if (nByte>0)
return nByte;
return FAILURE;
}
int serial_read (int& fd, char *pBuf, int nLength)
{
int nByte =0;
nByte = read (fd, pBuf, nLength);
return nByte;
}
int serial_close (int& fd)
{
if (!close (fd))
{
perror ("Cannot close serial!\n");
return FAILURE;
}
return SUCCESS;
}
int main (void)
{
int fd_com1,fd_com2,fd_com3;
int nRead = 0;
char buff[BUFF_SIZE] = {0};
int nTotal= 0;
//FILE *fp = NULL;
//COM1
if (serial_open (fd_com1, COM1) == FAILURE)
return 0;
if (serial_speed_set (fd_com1, 57600) == FAILURE)
return 0;
if (serial_parity_set (fd_com1, 8, 1, 'n') == FAILURE)
return 0;
//COM2
if (serial_open (fd_com2, COM2) == FAILURE)
return 0;
if (serial_speed_set (fd_com2, 57600) == FAILURE)
return 0;
if (serial_parity_set (fd_com2, 8, 1, 'n') == FAILURE)
return 0;
//COM3
if (serial_open (fd_com3, COM3) == FAILURE)
return 0;
if (serial_speed_set (fd_com3, 57600) == FAILURE)
return 0;
if (serial_parity_set (fd_com3, 8, 1, 'n') == FAILURE)
return 0;
char pszCon[20] = "***ok****!";
char pszBuf[20] = {0};
int nCmdLen = 20;
int nLen = 0;
printf("Start write content");
while (true)
{
//memset(pszCon, 0, 20);
//memset(pszBuf, 0, 20);
nCmdLen = strlen(pszCon);
serial_write(fd_com1, pszCon, nCmdLen);
serial_write(fd_com2, pszCon, nCmdLen);
serial_write(fd_com3, pszCon, nCmdLen);
sleep(1);
}
//fclose (fp);
serial_close (fd_com1);
serial_close (fd_com2);
serial_close (fd_com3);
return 0;
}
阅读(457) | 评论(0) | 转发(1) |