📄 communication.cpp
字号:
//communication.cpp#include "communication.h"/*==============================================================================* for error==============================================================================*/static char* error_msg;void g_errorlog(char *msg){ error_msg = msg;}char* g_errorget(){ return error_msg;}/*==============================================================================* desc: COM==============================================================================*/static const char *DEVCOMPATH = "/dev/znl";static const char *DEVCOMPATHS = "/dev/";void error_log(char* message){ printf("--@@Com103 the COM error ,message is: %s @@--\n", message);}CComdev::CComdev(){ this->com_fd = -1;}CComdev::~CComdev(){ this->CloseCom();}CComdev::CComdev(int comPort, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay){ bool result; result = this->Open(comPort, initSpeed, dataBit, stopBit, parityType, delay); if (!result) { error_log("error at CComdev::CComdev, this->Open error\n"); }}ssize_t CComdev::ReadData(int comfd, void *buffer, size_t num){ if (!isatty(comfd))//if (comfd < 0) { error_log("error at ReadData, comfd < 0\n"); return -1; } if (buffer == NULL) { error_log("error at ReadData, buffer is NULL\n"); return -1; } ssize_t result = 0; ssize_t iread = 0; int itimes = 0; while (result != (ssize_t)num) { iread = read(comfd, (char*)buffer+result, num-result); if (iread == 0) { // printf("--@@Com103 no data for read@@--\n"); if (itimes++ > 2) break; } else { result += iread; itimes = 0; } } return result;}ssize_t CComdev::ReadData(void *buffer, size_t num){ ssize_t result; result = CComdev::ReadData(this->com_fd, buffer, num); return result;}ssize_t CComdev::WriteData(int comfd, void *buffer, size_t num){ if (!isatty(comfd)) { error_log("error at WriteData, comfd < 0\n"); return -1; } if (buffer == NULL) { error_log("error at WriteData, buffer is NULL\n"); return -1; } ssize_t result; result = write(comfd, buffer, num); return result;}ssize_t CComdev::WriteData(void *buffer, size_t num){ ssize_t result; result = CComdev::WriteData(this->com_fd, buffer, num); return result;}int CComdev::OpenCom(int comPort, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay){ int tempComFd; if ( (comPort < 0) || (comPort > 99) ) { error_log("comPort < 0 or > 99\n"); comPort = 0; } if (dataBit < 5) { error_log("at CComdev::OpenCom, dataBit < 5\n"); dataBit = 5; } if (dataBit > 8) { error_log("at CComdev::OpenCom, dataBit > 8\n"); dataBit = 8; } if ( (stopBit != 1) && (stopBit != 2)) { error_log("at CComdev::OpenCom, stopBit is not 1 and is not 2\n"); stopBit = 1; } char *portstr = NULL; if ( (comPort >= 0) && (comPort <= 9) ) { portstr = (char *)malloc(sizeof (char) +1); if (portstr != NULL) portstr = gcvt(comPort, 1, portstr); } else { portstr = (char *)malloc(sizeof (char) * 2 +1); if (portstr != NULL) portstr = gcvt(comPort, 2, portstr); } char *comname = (char *)malloc(strlen(DEVCOMPATH) + strlen(portstr) +1); if (comname == NULL) { error_log("error at Open, malloc error\n"); if (portstr != NULL) { free(portstr); portstr = NULL; } tempComFd = -1; return tempComFd; } strcpy(comname, DEVCOMPATH); strcat(comname, portstr); tempComFd = open(comname, O_RDWR|O_NOCTTY); if (!isatty(tempComFd)) { tempComFd = -1; error_log("open com file failure\n"); if (portstr != NULL) { free(portstr); portstr = NULL; } if (comname != NULL) { free(comname); comname = NULL; } return tempComFd; } if (!CComdev::SetRaw(tempComFd, true)) { error_log("error at OpenCom, SetRaw\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; free(portstr); free(comname); return tempComFd; } if (!CComdev::SetTime(tempComFd, delay)) { error_log("error at OpenCom, SetTime\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; free(portstr); free(comname); return tempComFd; } if (!CComdev::SetSpeed(tempComFd, initSpeed)) { error_log("error at OpenCom, SetSpeed error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; free(portstr); free(comname); return tempComFd; } if (!CComdev::SetDatabit(tempComFd, dataBit)) { error_log("error at OpenCom, SetDatabit error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; free(portstr); free(comname); return tempComFd; } if (!CComdev::SetStopbit(tempComFd, stopBit)) { error_log("error at OpenCom, SetStopbit error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; free(portstr); free(comname); return tempComFd; } if (!CComdev::SetParity(tempComFd, parityType)) { error_log("error at OpenCom, SetParity error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; free(portstr); free(comname); return tempComFd; } free(portstr); portstr = NULL; free(comname); comname = NULL; return tempComFd;}int CComdev::OpenCom(char *com_filename, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay){ int tempComFd; if ( (com_filename == NULL) || (!isalpha((int)*com_filename)) ) { error_log("CComdev::OpenCom , com_filename is NULL or isalpha error"); tempComFd = -1; return tempComFd; } char *comname = (char *)malloc(strlen(DEVCOMPATHS) + strlen(com_filename) + 1); if (comname == NULL) { error_log("error at Open, malloc error\n"); tempComFd = -1; return tempComFd; } strcpy(comname, DEVCOMPATHS); strcat(comname, com_filename); tempComFd = open(comname, O_RDWR|O_NOCTTY); free(comname); comname = NULL; if (!isatty(tempComFd)) { tempComFd = -1; error_log("open com file failure\n"); return tempComFd; } if (!CComdev::SetRaw(tempComFd, true)) { error_log("error at OpenCom, SetRaw\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; return tempComFd; } if (!CComdev::SetTime(tempComFd, delay)) { error_log("error at OpenCom, SetTime\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; return tempComFd; } if (!CComdev::SetSpeed(tempComFd, initSpeed)) { error_log("error at OpenCom, SetSpeed error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; return tempComFd; } if (!CComdev::SetDatabit(tempComFd, dataBit)) { error_log("error at OpenCom, SetDatabit error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; return tempComFd; } if (!CComdev::SetStopbit(tempComFd, stopBit)) { error_log("error at OpenCom, SetStopbit error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; return tempComFd; } if (!CComdev::SetParity(tempComFd, parityType)) { error_log("error at OpenCom, SetParity error\n"); CComdev::CloseCom(tempComFd); tempComFd = -1; return tempComFd; } return tempComFd;}bool CComdev::Open(int comPort, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay){ int tempfd; if (isatty(this->com_fd)) { this->CloseCom(); } tempfd = CComdev::OpenCom(comPort, initSpeed, dataBit, stopBit, parityType, delay);//this->OpenCom(comPort, initSpeed, dataBit, stopBit, parityType); if (isatty(tempfd)) { this->com_fd = tempfd; return true; } else { this->com_fd = -1; error_log("error at Open, fd is not tty\n"); return false; }}bool CComdev::Open(char *com_filename, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay){ int tempfd; if (isatty(this->com_fd)) { this->CloseCom(); } tempfd = CComdev::OpenCom(com_filename, initSpeed, dataBit, stopBit, parityType, delay);//this->OpenCom(comPort, initSpeed, dataBit, stopBit, parityType); if (isatty(tempfd)) { this->com_fd = tempfd; return true; } else { this->com_fd = -1; error_log("error at Open, fd is not tty\n"); return false; }}bool CComdev::SetRaw(int comfd, bool isRaw){ if (!isatty(comfd)) { error_log("error at SetRaw, comfd is not atty\n"); return false; } struct termios option; if (tcgetattr(comfd, &option) != 0) { error_log("error at SetRaw, tcgetattr error\n"); return false; } if (isRaw) { option.c_iflag &= ~(ICRNL|IGNCR|INLCR|IGNBRK|BRKINT); option.c_iflag &= ~(IXON | IXOFF | IXANY); option.c_oflag &= ~(OCRNL|OLCUC|ONLCR|OPOST); option.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); } else { option.c_lflag |= (ICANON | ECHO| ECHOE | ISIG); option.c_oflag |= OPOST; } if (tcsetattr(comfd, TCSANOW, &option) != 0) { error_log("error at SetRaw, tcsetattr error\n"); return false; } else return true;}bool CComdev::SetRaw(bool isRaw){ bool result; result = CComdev::SetRaw(this->com_fd, isRaw); return result;}bool CComdev::SetMin(int comfd, unsigned int min){ if (!isatty(comfd)) { error_log("error at SetRaw, comfd is not atty\n"); return false; } struct termios option; if (tcgetattr(comfd, &option) != 0) { error_log("error at SetRaw, tcgetattr error\n"); return false; } option.c_cc[VMIN] = min; if (tcsetattr(comfd, TCSANOW, &option) != 0) { error_log("error at SetRaw, tcsetattr error\n"); return false; } else return true;}bool CComdev::SetMin(unsigned int min){ bool result; result = CComdev::SetMin(this->com_fd, min); return result;}bool CComdev::SetTime(int comfd, unsigned int delay){ if (!isatty(comfd)) { error_log("error at SetRaw, comfd is not atty\n"); return false; } struct termios option; if (tcgetattr(comfd, &option) != 0) { error_log("error at SetRaw, tcgetattr error\n"); return false; } option.c_cc[VTIME] = delay; if (tcsetattr(comfd, TCSANOW, &option) != 0) { error_log("error at SetRaw, tcsetattr error\n"); return false; } else return true;}bool CComdev::SetTime(unsigned int delay){ bool result; result = CComdev::SetTime(this->com_fd, delay); return result;}bool CComdev::SetSpeed(int comfd, speed_t speed){ if (!isatty(comfd)) { error_log("comfd is not COM\n"); return false; } struct termios option; if (tcgetattr(comfd, &option) != 0) { error_log("error at SetSpeed, tcgetattr error\n"); return false; } if ((cfsetispeed(&option, speed)) != 0) { error_log("error at SetSpeed, cfsetispeed error\n"); return false; } if ((cfsetospeed(&option, speed)) != 0) { error_log("error at SetSpeed, cfsetospeed error\n"); return false; } if (tcsetattr(comfd, TCSANOW, &option) != 0) { error_log("error SetParity, tcsetattr error\n"); return false; } else return true;}bool CComdev::SetSpeed(speed_t speed){ bool result; result = CComdev::SetSpeed(this->com_fd, speed); return result;}bool CComdev::SetDatabit(int comfd, int dataBit){ if (!isatty(comfd)) { error_log("error , comfd is not COM\n"); return false; } struct termios option; if (tcgetattr(comfd, &option) != 0) { error_log("error at SetDatabit, tcgetattr error\n"); return false; } option.c_cflag &= ~CSIZE; switch (dataBit) { case 5: option.c_cflag |= CS5; break; case 6: option.c_cflag |= CS6; break; case 7: option.c_cflag |= CS7; break; case 8: option.c_cflag |= CS8; break; default : error_log("error at SetDatabit, dataBit is error ,should in 5..8\n"); option.c_cflag |= CS8; return false; } if (tcsetattr(comfd, TCSANOW, &option) != 0) { error_log("error SetParity, tcsetattr error\n"); return false; } else return true;}bool CComdev::SetDatabit(int dataBit){ bool result; result = CComdev::SetDatabit(this->com_fd, dataBit); return result;}bool CComdev::SetStopbit(int comfd, int stopBit){ if (!isatty(comfd)) { error_log("error at SetStopbit, comfd is not COM\n"); return false; } struct termios option; if (tcgetattr(comfd, &option) != 0) { error_log("error at SetStopbit, tcgetattr error\n"); return false; } switch (stopBit) { case 1: option.c_cflag &= ~CSTOPB; break; case 2: option.c_cflag |= CSTOPB; break; default : error_log("error at SetStopbit, stopBit is not 1 or 2\n"); option.c_cflag &= ~CSTOPB; return false; } if (tcsetattr(comfd, TCSANOW, &option) != 0) { error_log("error SetParity, tcsetattr error\n"); return false; } else return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -