|
@@ -0,0 +1,192 @@
|
|
|
|
+#include "hcp2.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static bool checknmeasen(const char * strsen,const unsigned int nlen)
|
|
|
|
+{
|
|
|
|
+ if(nlen< 4)return false;
|
|
|
|
+
|
|
|
|
+ int i;
|
|
|
|
+ char check;
|
|
|
|
+ int nstarpos = -1;
|
|
|
|
+ check = strsen[1]^strsen[2];
|
|
|
|
+ for(i=3;i<static_cast<int>(nlen);i++)
|
|
|
|
+ {
|
|
|
|
+ if(strsen[i] == '*')
|
|
|
|
+ {
|
|
|
|
+ nstarpos = i;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ check = check^strsen[i];
|
|
|
|
+ }
|
|
|
|
+ if(nstarpos < 0)return false;
|
|
|
|
+ if(nstarpos >(static_cast<int>(nlen) -3))return false;
|
|
|
|
+ char strcheck[3];
|
|
|
|
+ int ncheck = check;
|
|
|
|
+ snprintf(strcheck,3,"%02X",ncheck);
|
|
|
|
+ char strsencheck[3];
|
|
|
|
+ strsencheck[2] = 0;
|
|
|
|
+ strsencheck[0] = strsen[nstarpos + 1];
|
|
|
|
+ strsencheck[1] = strsen[nstarpos + 2];
|
|
|
|
+ if(strncmp(strcheck,strsencheck,2) == 0)
|
|
|
|
+ {
|
|
|
|
+ // qDebug(" r sen is %s",strsen);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ std::cout<<std::chrono::system_clock::now().time_since_epoch().count()<<" cal check is "<<strcheck<<" sen check is "<<strsencheck<<std::endl;
|
|
|
|
+
|
|
|
|
+// qDebug(" sen is %s",strsen);
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <fcntl.h>
|
|
|
|
+//#include <asm/termios.h>
|
|
|
|
+#include <termios.h>
|
|
|
|
+
|
|
|
|
+static void set_baudrate (struct termios *opt, unsigned int baudrate)
|
|
|
|
+{
|
|
|
|
+ cfsetispeed(opt, baudrate);
|
|
|
|
+ cfsetospeed(opt, baudrate);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void set_data_bit (struct termios *opt, unsigned int databit)
|
|
|
|
+{
|
|
|
|
+ opt->c_cflag &= ~CSIZE;
|
|
|
|
+ switch (databit) {
|
|
|
|
+ case 8:
|
|
|
|
+ opt->c_cflag |= CS8;
|
|
|
|
+ break;
|
|
|
|
+ case 7:
|
|
|
|
+ opt->c_cflag |= CS7;
|
|
|
|
+ break;
|
|
|
|
+ case 6:
|
|
|
|
+ opt->c_cflag |= CS6;
|
|
|
|
+ break;
|
|
|
|
+ case 5:
|
|
|
|
+ opt->c_cflag |= CS5;
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ opt->c_cflag |= CS8;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void set_parity (struct termios *opt, char parity)
|
|
|
|
+{
|
|
|
|
+ switch (parity) {
|
|
|
|
+ case 'N': /* no parity check */
|
|
|
|
+ opt->c_cflag &= ~PARENB;
|
|
|
|
+ break;
|
|
|
|
+ case 'E': /* even */
|
|
|
|
+ opt->c_cflag |= PARENB;
|
|
|
|
+ opt->c_cflag &= ~PARODD;
|
|
|
|
+ break;
|
|
|
|
+ case 'O': /* odd */
|
|
|
|
+ opt->c_cflag |= PARENB;
|
|
|
|
+ opt->c_cflag |= ~PARODD;
|
|
|
|
+ break;
|
|
|
|
+ default: /* no parity check */
|
|
|
|
+ opt->c_cflag &= ~PARENB;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void set_stopbit (struct termios *opt, const char *stopbit)
|
|
|
|
+{
|
|
|
|
+ if (0 == strcmp (stopbit, "1")) {
|
|
|
|
+ opt->c_cflag &= ~CSTOPB; /* 1 stop bit */
|
|
|
|
+ } else if (0 == strcmp (stopbit, "1")) {
|
|
|
|
+ opt->c_cflag &= ~CSTOPB; /* 1.5 stop bit */
|
|
|
|
+ } else if (0 == strcmp (stopbit, "2")) {
|
|
|
|
+ opt->c_cflag |= CSTOPB; /* 2 stop bits */
|
|
|
|
+ } else {
|
|
|
|
+ opt->c_cflag &= ~CSTOPB; /* 1 stop bit */
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//https://blog.csdn.net/morixinguan/article/details/80898172
|
|
|
|
+//串口设置
|
|
|
|
+int set_port_attr (
|
|
|
|
+ int fd,
|
|
|
|
+ int baudrate, // B1200 B2400 B4800 B9600 .. B115200
|
|
|
|
+ int databit, // 5, 6, 7, 8
|
|
|
|
+ const char *stopbit, // "1", "1.5", "2"
|
|
|
|
+ char parity, // N(o), O(dd), E(ven)
|
|
|
|
+ int vtime,
|
|
|
|
+ int vmin )
|
|
|
|
+{
|
|
|
|
+ struct termios opt;
|
|
|
|
+ tcgetattr(fd, &opt);
|
|
|
|
+ //设置波特率
|
|
|
|
+ set_baudrate(&opt, baudrate);
|
|
|
|
+ opt.c_cflag |= CLOCAL | CREAD; /* | CRTSCTS */
|
|
|
|
+ //设置数据位
|
|
|
|
+ set_data_bit(&opt, databit);
|
|
|
|
+ //设置校验位
|
|
|
|
+ set_parity(&opt, parity);
|
|
|
|
+ //设置停止位
|
|
|
|
+ set_stopbit(&opt, stopbit);
|
|
|
|
+ //其它设置
|
|
|
|
+ opt.c_oflag = 0;
|
|
|
|
+ opt.c_lflag |= 0;
|
|
|
|
+ opt.c_oflag &= ~OPOST;
|
|
|
|
+ opt.c_cc[VTIME] = vtime;
|
|
|
|
+ opt.c_cc[VMIN] = vmin;
|
|
|
|
+ tcflush (fd, TCIFLUSH);
|
|
|
|
+ return (tcsetattr (fd, TCSANOW, &opt));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+hcp2::hcp2(std::string strportname)
|
|
|
|
+{
|
|
|
|
+ mstrportname = strportname;
|
|
|
|
+ mbthreadrun = true;
|
|
|
|
+ mpthread = new std::thread(&hcp2::threadrecv,this);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+hcp2::~hcp2()
|
|
|
|
+{
|
|
|
|
+ mbthreadrun = false;
|
|
|
|
+ mpthread->join();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void hcp2::threadrecv()
|
|
|
|
+{
|
|
|
|
+ int fd;
|
|
|
|
+ int len, i,ret;
|
|
|
|
+ (void)i;
|
|
|
|
+ (void)ret;
|
|
|
|
+
|
|
|
|
+ fd = open(mstrportname.data(), O_RDWR | O_NOCTTY);
|
|
|
|
+ if(fd < 0) {
|
|
|
|
+ perror(mstrportname.data());
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ set_port_attr(fd,B230400,8,"1",'N',0,0);
|
|
|
|
+
|
|
|
|
+ char strbuf[1000];
|
|
|
|
+ bool bSerialPortOpen = true;
|
|
|
|
+ (void)bSerialPortOpen;
|
|
|
|
+
|
|
|
|
+ while(mbthreadrun)
|
|
|
|
+ {
|
|
|
|
+ len = read(fd, strbuf, 999);
|
|
|
|
+ if(len > 0)
|
|
|
|
+ {
|
|
|
|
+ strbuf[len] = 0;
|
|
|
|
+ mstrgpsdata = mstrgpsdata + strbuf;
|
|
|
|
+// mstrHCP2.append(strbuf);
|
|
|
|
+// SerialGPSDecode();
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|