⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialtest.cpp

📁 串口测试程序
💻 CPP
字号:
#include  <stdio.h>#include  <stdlib.h>#include  <unistd.h>   /* UNIX stdandard lib define */#include  <sys/types.h>#include  <sys/stat.h>#include  <fcntl.h>      /* file control */#include  <termios.h>    /* PPSIX terminater control define */#include  <errno.h>      /* ERROR no define */#include  <string.h>#define   COM1  "/dev/ttyS0"#define   COM2  "/dev/ttyS1"#define   FAILURE   -1#define   SUCCESS    0#define   BUFF_SIZE  512int rate_arr[] = {B38400, B19200, B9600, B2400};int name_arr[] = { 38400,  19200, 9600, 2400 };int serial_open (int& fd, char *pSerial){	fd = open (COM1, O_RDWR);	if (FAILURE == fd)	{		perror ("Cannot open Serial!\n");		return  FAILURE;	}	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;	int nRead = 0;	char buff[BUFF_SIZE] = {0};	int   nTotal= 0;	FILE *fp = NULL;	char temp[30]={0};	char *pFile_name=NULL;	typedef struct 	{		int   file_len;		char  file_name[10];	}MSG_HEADER;        MSG_HEADER  *pMSG = (MSG_HEADER *)temp;			if (serial_open (fd, COM1) == FAILURE)		return 0;	if (serial_speed_set (fd, 9600) == FAILURE)		return 0;	if (serial_parity_set (fd, 8, 1, 'n') == FAILURE)		return 0;	        /*if ((fp = fopen ("timer", "w+")) == NULL)	{		printf ("Create file failure!\n");	  	return  -1;		}*/	unsigned int uiLength =0;	while ( serial_read(fd, temp, sizeof(MSG_HEADER))==0)		;	uiLength = pMSG->file_len;	pFile_name = pMSG->file_name;	        if ((fp = fopen (pFile_name, "w+")) == NULL)	{		printf ("Create file failure!\n");	  	return  -1;		}	printf ("file size: %u, file Name:%s\n", uiLength, pFile_name);	while (nTotal<uiLength) 	{		int nCount = 0;		while (nCount<BUFF_SIZE)		{			nRead = serial_read(fd, buff, BUFF_SIZE-nCount);			nCount += nRead;			fwrite (buff,1, nRead, fp);		}		nTotal += nCount;		printf ("Receive file length:%d \n", nTotal);	}        printf ("End  data receive!\n");		fclose (fp);	serial_close (fd);}			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -