📄 comm.cpp
字号:
#include "Comm.h"
#include <unistd.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h>
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h> /*错误号定义*/
int fd; // 串口设备句柄
struct termios oldtty;
/* 打开串口
* pPort: 串口名称或设备路径
* nBaudRate: 波特率
* nParity: 奇偶校验
* nByteSize: 数据字节宽度
* nStopBits: 停止位
*/
bool OpenComm(const char *pPort, char *nBaudRate, char *cParity, char *nDataBits, char *nStopBits)
{
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY |O_NDELAY );
if (fd == -1)
{
perror("打开串口失败!");
}
}
// 关闭串口
bool CloseComm()
{
close (fd);
}
/* 写串口
* pData: 待写的数据缓冲区指针
* nLength: 待写的数据长度
*/
void WriteComm(void* pData, int nLength)
{
int dwNumWrite; // 串口发出的数据长度
int count =write(fd, pData, nLength);
}
/* 读串口
* pData: 待读的数据缓冲区指针
* nLength: 待读的最大数据长度
* 返回: 实际读入的数据长度
*/
int ReadComm(char * pData)
{
int nbytes; // 串口收到的数据长度
char *bufptr = pData;
while ((nbytes = read(fd, bufptr, pData + sizeof(pData) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if(bufptr[-1]=='\n'||bufptr[-1]=='\r'||bufptr[-1]==' ')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
return nbytes;
}
/*
* 设置波特率、奇偶校验、数据位
*/
void m_setparms(int fd, char *baudr, char *par, char *bits, char *stopb, int hwf, int swf)
{
int spd = -1;
int newbaud;
int bit = bits[0];
struct termios tty;
#ifdef USE_SOCKET
if (portfd_is_socket)
return;
#endif
tcgetattr(fd, &oldtty);
tcgetattr(fd, &tty);
if (bit == '7' && (par[0] == 'M' || par[0] == 'S'))
bit = '8';
if ((newbaud = (atol(baudr) / 100)) == 0 && baudr[0] != '0') newbaud = -1;
switch(newbaud)
{
case 0: spd = B0; break;
case 3: spd = B300; break;
case 6: spd = B600; break;
case 12: spd = B1200; break;
case 24: spd = B2400; break;
case 48: spd = B4800; break;
case 96: spd = B9600; break;
case 192: spd = B19200; break;
case 384: spd = B38400; break;
case 576: spd = B57600; break;
case 1152: spd = B115200; break;
case 2304: spd = B230400; break;
}
if (spd != -1)
{
cfsetospeed(&tty, (speed_t)spd);
cfsetispeed(&tty, (speed_t)spd);
}
switch (bit)
{
case '5':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS5;
break;
case '6':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS6;
break;
case '7':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS7;
break;
case '8':
default:
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
break;
}
/* 设置输入模式, 没有回显字符 */
tty.c_iflag = IGNBRK;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
/* set raw input, 1 second timeout */
//tty.c_cflag |= (CLOCAL | CREAD);
//tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//tty.c_oflag &= ~OPOST;
//tty.c_cc[VMIN] = 0;
//tty.c_cc[VTIME] = 10;
#ifdef _DCDFLOW
tty.c_cflag &= ~CRTSCTS;/*数据流控制:无*/
#endif
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 5;
if (swf)
tty.c_iflag |= IXON | IXOFF;
else
tty.c_iflag &= ~(IXON|IXOFF|IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
if (par[0] == 'E')
tty.c_cflag |= PARENB;
else if (par[0] == 'O')
tty.c_cflag |= (PARENB | PARODD);
if (stopb[0] == '2')
tty.c_cflag |= CSTOPB;
else
tty.c_cflag &= ~CSTOPB;
tcsetattr(fd, TCSANOW, &tty);
}
/*
* Set cbreak mode.
* Mode 0 = normal.
* Mode 1 = cbreak, no echo
* Mode 2 = raw, no echo.
* Mode 3 = only return erasechar (for wkeys.c)
*
* Returns: the current erase character.
*/
int setcbreak(int mode)
{
struct termios tty,savetty;
static int init = 0;
static int erasechar;
if (init == 0)
{
tcgetattr(0, &savetty);
erasechar = savetty.c_cc[VERASE];
init++;
}
if (mode == 3) return(erasechar);
tcsetattr(0, TCSADRAIN, &savetty);
if (mode == 0)
{
return(erasechar);
}
tcgetattr(0, &tty);
if (mode == 1)
{
tty.c_oflag &= ~OPOST;
tty.c_lflag &= ~(XCASE|ECHONL|NOFLSH);
tty.c_lflag &= ~(ICANON | ISIG | ECHO);
tty.c_iflag &= ~(ICRNL|INLCR);
tty.c_cflag |= CREAD;
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = 1;
}
if (mode == 2)
{ /* raw */
tty.c_iflag &= ~(IGNBRK | IGNCR | INLCR | ICRNL | IUCLC |
IXANY | IXON | IXOFF | INPCK | ISTRIP);
tty.c_iflag |= (BRKINT | IGNPAR);
tty.c_oflag &= ~OPOST;
tty.c_lflag &= ~(XCASE|ECHONL|NOFLSH);
tty.c_lflag &= ~(ICANON | ISIG | ECHO);
tty.c_cflag |= CREAD;
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = 1;
}
tcsetattr(0, TCSADRAIN, &tty);
return(erasechar);
}
/*****************************************************************************/
int /* O - 0 = MODEM ok, -1 = MODEM bad */
init_modem() /* I - Serial port file */
{
char buffer[255]; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int tries; /* Number of tries so far */
for (tries = 0; tries < 3; tries ++)
{
/* 发送AT命令 */
if (write(fd, "AT\r", 3) < 3)
continue;
/* read characters into our string buffer until we get a CR or NL */
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
if (strncmp(buffer, "OK", 2) == 0)
return (0);
}
return (-1);
}
/*********************************************************************************/
/* Check if there is IO pending. */
int check_io(int fd1, int fd2, int tmout,char *buf, int *buflen)
{
int n = 0, i;
struct timeval tv;
fd_set fds;
extern int io_pending; /* wkeys.c */
tv.tv_sec = tmout / 1000;
tv.tv_usec = (tmout % 1000) * 1000L;
i = fd1;
if (fd2 > fd1) i = fd2;
FD_ZERO(&fds);
if (fd1 >= 0) FD_SET(fd1, &fds); else fd1 = 0;
if (fd2 >= 0) FD_SET(fd2, &fds); else fd2 = 0;
if (select(i+1, &fds, NULL, NULL, &tv) > 0)
n = 1 * (FD_ISSET(fd1, &fds) > 0) + 2 * (FD_ISSET(fd2, &fds) > 0);
/* If there is data put it in the buffer. */
if (buf) {
i = 0;
if ((n & 1) == 1) {
i = read(fd1, buf, 127);
}
buf[i > 0 ? i : 0] = 0;
if (buflen) *buflen = i;
}
return(n);
}
int sendtext(const char *destnum,const char *msg)
{
char buffer[128]; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int x;
int blen;
//==================================================================
/* send an AT command followed by a CR */
write(fd, "AT\r", 3);
memset(buffer,0,128);
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
//===============================================================
write(fd, "at+csdh=1\r", 20);
memset(buffer,0,128);
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
//================================================================
write(fd, "at+csmp=17,167\r", 15);
memset(buffer,0,128);
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
//=================================================================
write(fd, "at+cmgf=1\r", 10) ;
memset(buffer,0,128);
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
//=================================================================
char tmp[32]="at+cmgs=\"";
strncat(tmp,destnum,3);
char tmp2[32]={0};
strcpy(tmp2,destnum+3);
strcat(tmp2,"\"\r");
int num = write(fd, tmp, strlen(tmp));
usleep(1000*100);
num=write(fd,tmp2,strlen(tmp2));
memset(buffer,0,128);
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if(bufptr[-1]=='\n'||bufptr[-1]=='\r'||bufptr[-1]==' ')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
//================================================================
int msglen = strlen(msg);
int total=0;
int sendlength=0;
char tmp3[32];
while(total<msglen)
{
memset(tmp3,0,strlen(tmp3));
if((msglen-total)>12)
{
sendlength = 12;
strncpy(tmp3, msg+total,sendlength);
}
else
{
sendlength = msglen-total;
strncpy(tmp3, msg+total,sendlength);
strcat(tmp3, "\x01a");
}
total += sendlength;
write(fd, tmp3, strlen(tmp3));
usleep(1000*100);
}
memset(buffer,0,128);
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -