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

📄 serial.cpp~

📁 linux下用QT 做的串口编程工具,在linux下做串口调试有一定的参考价值
💻 CPP~
字号:
#include "serial.h"#include <string.h>Serial::Serial(){    ptty = &pttyInfo;    memset(ptty,0,sizeof(TTY_INFO));     pthread_mutex_init(&ptty->mt, NULL);    }Serial::~Serial(){    }bool Serial::readyTTY(int fd){    sprintf(ptty->name,"/dev/ttyS%d",fd);    ptty->fd = open(ptty->name,O_RDWR | O_NOCTTY | O_NDELAY);    if (ptty->fd < 0)      return false;    tcgetattr(ptty->fd,&ptty->otm);      return true;}int Serial::cleanTTY(){    if (ptty->fd > 0)    {	tcsetattr(ptty->fd,TCSANOW,&ptty->otm);	close(ptty->fd);	ptty->fd = -1;	ptty = NULL;    }    return 0;}int Serial::setTTYSpeed(int speed){    bzero(&ptty->ntm, sizeof(ptty->ntm));    tcgetattr(ptty->fd,&ptty->ntm);    ptty->ntm.c_cflag = /*CS8 |*/ CLOCAL | CREAD;        switch(speed)    {    case 300:	ptty->ntm.c_cflag |= B300;	break;    case 1200:	ptty->ntm.c_cflag |= B1200;	break;    case 2400:	ptty->ntm.c_cflag |= B2400;	break;    case 4800:	ptty->ntm.c_cflag |= B4800;	break;    case 9600:	ptty->ntm.c_cflag |= B9600;	break;    case 19200:	ptty->ntm.c_cflag |= B19200;	break;    case 38400:	ptty->ntm.c_cflag |= B38400;	break;    case 115200:	ptty->ntm.c_cflag |= B115200;	break;    }    ptty->ntm.c_iflag = IGNPAR;    ptty->ntm.c_oflag = 0;        tcflush(ptty->fd,TCIFLUSH);    tcsetattr(ptty->fd,TCSANOW,&ptty->ntm);        return 0;}int Serial::setTTYParity(int databits,int parity,int stopbits){    //    if( tcgetattr(ptty->fd,&ptty->ntm) != 0)    {	return 1;    }        bzero(&ptty->ntm, sizeof(ptty->ntm));    ptty->ntm.c_cflag = CS8 | CLOCAL | CREAD;    ptty->ntm.c_iflag = IGNPAR;    ptty->ntm.c_oflag = 0;    //    switch (databits)    {      case 7:	ptty->ntm.c_cflag |= CS7;	break;    case 8:	ptty->ntm.c_cflag |= CS8;	break;    default:	printf("Unsupported data size\n");	return 5;    }    //    //    switch (parity)    {     case 'n':    case 'N':	ptty->ntm.c_cflag &= ~PARENB; /* Clear parity enable */	ptty->ntm.c_iflag &= ~INPCK; /* Enable parity checking */	break;    case 'o':    case 'O':	ptty->ntm.c_cflag |= (PARODD|PARENB); 	ptty->ntm.c_iflag |= INPCK; 	break;    case 'e':    case 'E':	ptty->ntm.c_cflag |= PARENB; /* Enable parity */	ptty->ntm.c_cflag &= ~PARODD;	ptty->ntm.c_iflag |= INPCK; /* Disnable parity checking */	break;    case 'S':	case 's': /*as no parity*/	ptty->ntm.c_cflag &= ~PARENB;	ptty->ntm.c_cflag &= ~CSTOPB;	break;    default:	printf("Unsupported parity\n");	return 2;    }    //     switch (stopbits)    {    case 1:	ptty->ntm.c_cflag &= ~CSTOPB;	break;    case 2:	ptty->ntm.c_cflag |= CSTOPB;	break;    default:	printf("Unsupported stop bits\n");	return 3;    }    //    //    ptty->ntm.c_lflag = 0;    ptty->ntm.c_cc[VTIME] = 0;  // inter-character timer unused    ptty->ntm.c_cc[VMIN] = 1;  // blocking read until 1 chars received    tcflush(ptty->fd, TCIFLUSH);    if (tcsetattr(ptty->fd,TCSANOW,&ptty->ntm) != 0)    {	printf("SetupSerial \n");	return 4;    }        return 0;}int Serial::recvnTTY(char *pbuf,int size){    int ret,bytes;        ret = 0;	bytes = 0;	pthread_mutex_lock(&ptty->mt);	ioctl(ptty->fd, FIONREAD, &bytes);	if(bytes>0)	{	    ret = read(ptty->fd,pbuf,size);	}	pthread_mutex_unlock(&ptty->mt);    return ret;}int Serial::sendnTTY(char *pbuf,int size){    int ret,nleft;    char *ptmp;        ret = 0;    nleft = size;    ptmp = pbuf;    	pthread_mutex_lock(&ptty->mt);	ret = write(ptty->fd,ptmp,nleft);	pthread_mutex_unlock(&ptty->mt);        tcflush(ptty->fd,TCIFLUSH);    return ret;}int Serial::lockTTY(){    if(ptty->fd < 0)    {	return 1;    }        return flock(ptty->fd,LOCK_EX);}int Serial::unlockTTY(TTY_INFO *ptty){    if(ptty->fd < 0)    {	return 1;    }        return flock(ptty->fd,LOCK_UN);}#ifdef LEAF_TTY_TEST///////////////////////////////////////////////////////////////////////////////// 接口测试int main(int argc,char **argv){    TTY_INFO *ptty;    int nbyte,idx;    unsigned char cc[16];        ptty = readyTTY(0);    if(ptty == NULL)    {	//  printf("readyTTY(0) error\n");	return 1;    }    //    //    lockTTY(ptty);    if(setTTYSpeed(ptty,9600)>0)    {	//  printf("setTTYSpeed() error\n");	return -1;    }    if(setTTYParity(ptty,8,'N',1)>0)    {	//   printf("setTTYParity() error\n");	return -1;    }    //    idx = 0;    while(1)    {	cc[0] = 0xFA;	sendnTTY(ptty,&cc[0],1);	nbyte = recvnTTY(ptty,cc,1);	//  printf("%d:%02X\n",idx++,cc[0]);    }        cleanTTY(ptty);    }#endif

⌨️ 快捷键说明

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