📄 ttycontrl.c
字号:
/**************************************************************Description: 3G test program. Use AT-command: "AT+CGMI"$Log: **************************************************************///===================================================// head files//===================================================#include "3GDaemon.h"//===================================================// Global variables //=================================================== int gComFp; // pointer of device fileextern FILE *logfile;//===================================================// Functions//===================================================/*****************************************Function: OpenComDescription: Open a device fileParameter: deviceName[input]: Path of device fileReturn: SUCCEED: succeed to open files ERROR: failed to open filesRemark:******************************************/int OpenCom(const char *deviceName) { char msg[BUFFER_LEN]; struct termios newTio; // Attributions of terminal // Try to open a device file if ((gComFp = open(deviceName, O_RDWR|O_EXCL|O_NONBLOCK|O_NOCTTY)) == FAIL) { sprintf(msg, "Can't open 3G device %s.\n", deviceName); log_message(logfile, msg); return ERROR; } // Get the default attributions tcgetattr(gComFp, &newTio); bzero(&newTio, sizeof(newTio)); // Setting control mode flag newTio.c_cflag |= CREAD; newTio.c_cflag &= ~CLOCAL; newTio.c_cflag &= ~PARENB; newTio.c_cflag &= ~PARODD; newTio.c_cflag &= ~CSTOPB; newTio.c_cflag &= ~CSIZE; newTio.c_cflag |= CS8; newTio.c_oflag |= OPOST; // Setting input, output and local mode flag newTio.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR ); newTio.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET); newTio.c_lflag &= ~(ICANON | XCASE | ECHO | ECHOE | ECHONL); newTio.c_lflag &= ~(ECHO | ECHOE); // Set timeout, but it's said no use. newTio.c_cc[VMIN] = 1; // Do not update the options and do it newTio.c_cc[VTIME] = 0; // Timeout 0 seconds newTio.c_cc[VEOF] = 1; // Set baudrate cfsetispeed(&newTio, BAUDRATE); cfsetospeed(&newTio, BAUDRATE); // Enable the setting tcflush(gComFp, TCIFLUSH); // Set attributions if (tcsetattr(gComFp, TCSANOW, &newTio) != SUCCEED) { log_message(logfile, "Setup Serial port error!\n"); return ERROR; } return SUCCEED; } /*****************************************Function: ReadComDescription: read data from deviceParameter: buffer[input]: buffer of data read from device readLen[input]: length of data want to readReturn: SUCCEED: read succeed ERROR: read failedRemark:******************************************/int ReadCom(unsigned char *buffer, int readLen) { fd_set readSet; struct timeval timeout; // strut of timeout int realReadLen; // length of data really read from device int ret; // value for return from select() if (gComFp == 0) { log_message(logfile, "Read Serial error: Device not open!\n"); return ERROR; } timeout.tv_sec = ZERO_LEN; // set time for timeout (s) timeout.tv_usec = DELAY_FOR_READ_TIME; // set time for timeout (us) // set block for reading FD_ZERO(&readSet); FD_SET(gComFp, &readSet); // wait for ready state for reading ret=select(gComFp+1, &readSet, NULL, NULL, &timeout); if (ret) { // read data from device realReadLen = read(gComFp, buffer, readLen); if (realReadLen > ZERO_LEN) { buffer[realReadLen+1] = '\0'; return SUCCEED; } else { log_message(logfile, "Read Serial error: read nothing!\n"); return ERROR; // Read nothing } } else { log_message(logfile, "Read Serial error: device not ready!\n"); return ERROR; // Not ready for reading after timeout }}/*****************************************Function: WriteComDescription: Write data to deviceParameter: buffer[input]: buffer of data want to writeReturn: SUCCEED: write succeed ERROR: write failedRemark:******************************************/int WriteCom(unsigned char *buffer) { if (strlen(buffer) <= ZERO_LEN) { log_message(logfile, "Write Serial error: String is empty!\n"); return ERROR; } if (gComFp == 0) { log_message(logfile, "Write Serial error: Can not write to 3G device!\n"); return ERROR; } int writeLen; writeLen = write(gComFp, buffer, strlen(buffer)); if (writeLen <= ZERO_LEN) { log_message(logfile, "Write Serial error: Not all chars were sended!\n"); return ERROR; } return SUCCEED;}int SendAtCommand(char *comd){ WriteCom(comd); return 0;}int ReceiveResponse(char *repBuffer){ if (ReadCom(repBuffer, BUF_MAX_SIZE) != SUCCEED) // read from device { log_message(logfile, "Read Serial error: read failed!\n"); return ERROR; } return SUCCEED;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -