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

📄 serialport.cpp

📁 使用C#开发的串口读写例程
💻 CPP
字号:
#include "SerialPort.h"#include "assert.h"#include "fcntl.h"#include <sys/select.h>csSerialPort::csSerialPort(){		 fd = 8888;		// a invalid value}csSerialPort::~csSerialPort(){	}void csSerialPort::Open(char * device, int nBaudRate, int Parity, int DataBits, int stopbits){	/* open with read and write */	fd = open(device, O_RDWR);		// open	if (-1 == fd)	{ 		perror("Com open error");	}		// set nBaudRate	set_Speed(fd, nBaudRate);	// setupserial	set_Parity(fd, DataBits, stopbits, Parity);}void csSerialPort::set_Speed(int fd, int speed){	speed_t BaudRates[8] = {B2400, B4800, B9600, B9600, B9600, B9600, B9600, B19200};	int status; 	struct termios Opt;	tcgetattr(fd, &Opt); 		tcflush(fd, TCIOFLUSH);	 	assert( (2400 <= speed) && (speed <= 19200));	//assert(1);	cfsetispeed(&Opt, BaudRates[(speed/2400) - 1]); 	cfsetospeed(&Opt, BaudRates[(speed/2400) - 1]); 	status = tcsetattr(fd, TCSANOW, &Opt); 			if (status != 0)	{ 		perror("tcsetattr"); 		return; 	} 	tcflush(fd,TCIOFLUSH); }int  csSerialPort::set_Parity(int fd, int databits, int stopbits, int parity){//	printf("databits = %d, stopbits = %d, parity = %d\n", databits, stopbits, parity);	struct termios options; 	if (tcgetattr(fd, &options) != 0)	{ 		//perror("SetupSerial"); 		return(false); 	}	options.c_cflag &= ~CSIZE; 		/* set databits */	switch (databits)	{ 		case 7: 			options.c_cflag |= CS7; break;				case 8: 			options.c_cflag |= CS8; break; 				default: 			//fprintf(stderr, "Unsupported data sizen");            return (false); 	}	/* set parity */	switch (parity) 	{ 		/////////////////		/*		enum Parity		{    		0 EvenParity,		1 MarkParity,		2 NoParity,		3 OddParity,		4 SpaceParity		};		*/		//////////////////		case 'n':	//	NoParity?//		case 'N': 		case 0 : 			options.c_cflag &= ~PARENB; /* Clear parity enable */			options.c_iflag &= ~INPCK;  /* Enable parity checking */			break; //		case 'o':	// OddParity ?//		case 'O': 		case 3 :			options.c_cflag |= (PARODD | PARENB);	/* 设置为奇效验*/ 			options.c_iflag |= INPCK;				/* Disnable parity checking */ 			break; //		case 'e': //		case 'E': 		case 2 :			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*/ 		case 4 :			options.c_cflag &= ~PARENB;			options.c_cflag &= ~CSTOPB;			break; 				default: 			//fprintf(stderr,"Unsupported parityn"); 			return (false); 	} 			/* Set stopbits*/ 	switch (stopbits)	{ 		///////////		/*		enum StopBits		{		OneStopBit,		OnePointFiveStopBits,		TwoStopBits		};		*/		////////////		case 0:											options.c_cflag &= ~CSTOPB; break;		// 不指明表示一位停止位 		case 2: 			options.c_cflag |= CSTOPB;  break;		// 指明CSTOPB表示两位,只有两种可能		default: 			//fprintf(stderr,"Unsupported stop bitsn"); 			return (false); 	} 	/* Set input parity option */ 	if (parity != 2) 	{			options.c_iflag |= INPCK; 	}		tcflush(fd, TCIFLUSH);	options.c_cc[VTIME] = 150;	/* 设置超时15 seconds*/ 	options.c_cc[VMIN] = 0;		/* Update the options and do it NOW */		if (tcsetattr(fd, TCSANOW, &options) != 0) 	{ 		perror("SetupSerial 2"); 		return (false); 	} 	return (true);	}int csSerialPort::Read (BYTE *buffer, int Length){	int result =  read(fd, buffer, Length);	return result;}int csSerialPort::Select(long sec, long usec){	//printf("the fd is %d\n", fd);	fd_set rset;	FD_ZERO(&rset);	FD_SET(fd, &rset);	timeval tv;	tv.tv_sec = sec;	tv.tv_usec = usec;	int nfds = fd + 1;			int result = select(nfds, &rset, NULL, NULL, &tv);		int res = FD_ISSET(fd, &rset);	//printf("read?? %d\n select ok?%d\n", res, result);		return result;}int csSerialPort::Write(BYTE *buffer, int Length){	int result = write(fd, buffer, Length);	return result;}int csSerialPort::Close(){	return close(fd);	 }

⌨️ 快捷键说明

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