From 6f5b24fb3fc563c268325e833a5d7019942d609a Mon Sep 17 00:00:00 2001 From: huyizhong Date: Wed, 5 Jun 2024 17:18:36 +0800 Subject: [PATCH] weathercomm --- TestComm/app/src/main/cpp/CMakeLists.txt | 3 +- TestComm/app/src/main/cpp/GPIOControl.cpp | 10 + TestComm/app/src/main/cpp/WeatherComm.cpp | 534 ++++++++++++++++++++++ TestComm/app/src/main/cpp/WeatherComm.h | 51 +++ TestComm/app/src/main/cpp/native-lib.cpp | 489 ++++++++++++++++---- app/src/main/cpp/CMakeLists.txt | 2 +- gradle.properties | 10 +- 7 files changed, 1002 insertions(+), 97 deletions(-) create mode 100644 TestComm/app/src/main/cpp/WeatherComm.cpp create mode 100644 TestComm/app/src/main/cpp/WeatherComm.h diff --git a/TestComm/app/src/main/cpp/CMakeLists.txt b/TestComm/app/src/main/cpp/CMakeLists.txt index 282af7cc..c8679151 100644 --- a/TestComm/app/src/main/cpp/CMakeLists.txt +++ b/TestComm/app/src/main/cpp/CMakeLists.txt @@ -26,7 +26,8 @@ add_library( # Sets the name of the library. # NRSEC3000ctl.cpp SpiLib.cpp native-lib.cpp - GPIOControl.cpp) + GPIOControl.cpp + WeatherComm.cpp) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by diff --git a/TestComm/app/src/main/cpp/GPIOControl.cpp b/TestComm/app/src/main/cpp/GPIOControl.cpp index 187f7e33..783025af 100644 --- a/TestComm/app/src/main/cpp/GPIOControl.cpp +++ b/TestComm/app/src/main/cpp/GPIOControl.cpp @@ -31,6 +31,16 @@ typedef struct char str[MAX_STRING_LEN]; }IOT_PARAM; +typedef struct{ + float airtemp; /* 空气温度*/ + float RH; /* 相对湿度*/ + float atmos; /* 大气压*/ + float windspeed; /* 风速*/ + float winddirection; /* 风向*/ + float rainfall; /* 雨量*/ + float sunshine; /* 日照*/ +}Weather; + void GpioControl::setInt(int cmd, int value) { int fd = open("/dev/mtkgpioctrl", O_RDONLY); diff --git a/TestComm/app/src/main/cpp/WeatherComm.cpp b/TestComm/app/src/main/cpp/WeatherComm.cpp new file mode 100644 index 00000000..01da9869 --- /dev/null +++ b/TestComm/app/src/main/cpp/WeatherComm.cpp @@ -0,0 +1,534 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "GPIOControl.h" +#include "WeatherComm.h" + +SIO_PARAM_SERIAL_DEF serialport; +float weatherpntmsg[10]; + +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':/* 无校验 */ + case 'n': + opt->c_cflag &= ~PARENB; + break; + case'E':/*偶校验*/ + case 'e': + opt->c_cflag |= PARENB; + opt->c_cflag &= ~PARODD; + break; + case'O':/* 奇校验 */ + case 'o': + opt->c_cflag |= PARENB; + opt->c_cflag |= ~PARODD; + break; + default: /*其它选择为无校验 */ + opt->c_cflag &= ~PARENB; + break; + } +} + +static void set_stopbit (struct termios *opt, const char *stopbit) +{ + if (strcmp(stopbit, "1") == 0) + { + opt->c_cflag &= ~CSTOPB;/*1 位停止位 t */ + } + else if(0 == strcmp(stopbit, "1.5")) + { + opt->c_cflag &= ~CSTOPB;/*1.5 位停止位 */ + } + else if(0 == strcmp (stopbit,"2")) + { + opt->c_cflag |= CSTOPB; /*2 位停止位 */ + } + else + { + opt->c_cflag &= ~CSTOPB; /*1 位停止位 */ + } +} + +int set_port_attr (int fd, int baudrate, int databit, const char *stopbit, char parity, int vtime, int vmin ) +{ + struct termios opt; + tcgetattr(fd, &opt); + set_baudrate(&opt, baudrate); + //opt.c_cflag |= CLOCAL|CREAD; /*|CRTSCTS */ + opt.c_lflag &= ~(ICANON | ECHO |ECHOE |ISIG); + 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)); +} + +static void setInt(int cmd, int value) +{ + int fd = open("/dev/mtkgpioctrl", O_RDONLY); + IOT_PARAM param; + param.cmd = cmd; + param.value = value; + // LOGE("set_int fd=%d,cmd=%d,value=%d\r\n",fd, cmd, value); + if( fd > 0 ) + { + int res = ioctl(fd, IOT_PARAM_WRITE, ¶m); + // LOGE("set_int22 cmd=%d,value=%d,result=%d\r\n",param.cmd, param.value, param.result); + close(fd); + } + return; +} +static void setRS485Enable(bool z) { + setInt(CMD_SET_485_EN_STATE, z ? 1 : 0); +} + +static void set485WriteMode() { + setInt(CMD_SET_485_STATE, 1); +} + +static void set485ReadMode() { + setInt(CMD_SET_485_STATE, 0); +} +static void set12VEnable(bool z) { + setInt(CMD_SET_12V_EN_STATE, z ? 1 : 0); +} + +static void setCam3V3Enable(bool enabled) +{ + setInt(CMD_SET_CAM_3V3_EN_STATE, enabled ? 1 : 0); +} + + +/********************************************************************************* +* 气象数据处理 * +**********************************************************************************/ +void PortDataProcess( void ) +{ + float fvalue, fcorvalue, *fvalua, frnb/*, fwind*/; + //WORD uDevAddr; + unsigned char cmdidx; + int i, j, aipnt, datanum; + SIO_PARAM_SERIAL_DEF *pPortParam; + char szbuf[64]; + + pPortParam = &serialport; + //取出装置地址,开始处理地址+++ + if(0x02 == pPortParam->m_au8RecvBuf[5]) + { + //pPortParam->devaddr = pPortParam->m_au8RecvBuf[4]; + return; + } + cmdidx = pPortParam->m_au8RecvBuf[5]; +#if 0 + aipnt = pPortParam->SameTypeDevIdx; + uDevAddr = serialport->m_au8RecvBuf[4]; + if(0 == srdt.IsReadWireTem) + { + if(uDevAddr != pPortParam->devaddr) + return; + } +#endif + fvalua = &fvalue; + datanum = pPortParam->m_au8RecvBuf[6]; + if((0x08 != cmdidx) && (0x09 != cmdidx)) + return; + + for(i = 0, j=7; (im_au8RecvBuf[1]); i++, j+=5 ) + { + if(0x08 == cmdidx) + fvalue = (pPortParam->m_au8RecvBuf[j+1]<<24)+(pPortParam->m_au8RecvBuf[j+2]<<16) + +(pPortParam->m_au8RecvBuf[j+3]<<8)+pPortParam->m_au8RecvBuf[j+4]; + else + { + *(u_char *)fvalua = pPortParam->m_au8RecvBuf[j+4]; + *((u_char *)fvalua+1) = pPortParam->m_au8RecvBuf[j+3]; + *((u_char *)fvalua+2) = pPortParam->m_au8RecvBuf[j+2]; + *((u_char *)fvalua+3) = pPortParam->m_au8RecvBuf[j+1]; + } + switch(pPortParam->m_au8RecvBuf[j]) + { + case 1: /*温度*/ + weatherpntmsg[0] = fvalue; + LOGE("温度:%0.3f ", fvalue); + break; + case 2: /*气压*/ + weatherpntmsg[5] = fvalue; + LOGE("气压:%0.3f ", fvalue); + break; + case 3: /*湿度*/ + weatherpntmsg[1] = fvalue; + LOGE("湿度:%0.3f ", fvalue); + break; + case 4: /*雨量*/ + break; + case 5: /*日照*/ + break; + case 6: /*风速*/ + weatherpntmsg[2] = fvalue; + LOGE("风速:%0.3f ", fvalue); + break; + case 7: /*风向*/ + weatherpntmsg[3] = fvalue; + LOGE("风向:%0.3f ", fvalue); + break; + case 8: /*拉力*/ + case 9: /*倾角传感器X轴倾角*/ + case 10: /*倾角传感器Y轴倾角*/ + case 11: /*测温球导线温度*/ + case 12: /*测温球内部温度*/ + break; + case 13: /*测温球导线X轴倾角*/ + break; + case 14: /*测温球导线Y轴倾角*/ + break; + case 15: /*测温球导线电流*/ + break; + case 16: /*测温球电池电压*/ + break; + case 17: /*A相泄漏电流平均值;*/ + break; + case 18: /*A相泄漏电流最大值;*/ + break; + case 19: /*A相超过3mA的脉冲频次*/ + break; + case 20: /*A相超过10mA的脉冲频次*/ + break; + case 21: /*B相泄漏电流平均值;*/ + break; + case 22: /*B相泄漏电流最大值;*/ + break; + case 23: /*B相超过3mA的脉冲频次*/ + break; + case 24: /*B相超过10mA的脉冲频次*/ + case 25: /*C相泄漏电流平均值;*/ + case 26: /*C相泄漏电流最大值;*/ + case 27: /*C相超过3mA的脉冲频次*/ + case 28: /*C相超过10mA的脉冲频次*/ + break; + } + } +} + +//*************************************************************** +//* 按照协议格式化接收数据 * +//*************************************************************** +static void RecvData(u_char *buf, int len)// 规约读数据处理 +{ + int i, ictime; + //WORD crc, check; + SIO_PARAM_SERIAL_DEF *pPortParam; + + pPortParam = &serialport; + ictime = (int)time(NULL); + + if(pPortParam->m_iRecvLen == 0) + { + pPortParam->iRecvTime = ictime; + } + else + { + if((ictime-pPortParam->iRecvTime > 6) || (ictime - pPortParam->iRecvTime < 0)) + pPortParam->iRecvTime = ictime; + else if(ictime - pPortParam->iRecvTime > 2) + { + pPortParam->m_iRecvLen = 0; + pPortParam->m_iRevStatus = 0; + } + } + + for(i=0; im_iRevStatus) + { + case 0: // 0x68 + pPortParam->m_iRecvLen = 0; + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(0x68 == buf[i]) + pPortParam->m_iRevStatus++; + else + pPortParam->m_iRevStatus = 18; + break; + case 1: // len1 + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + pPortParam->m_iRevStatus++; + break; + case 2: // len2 + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(buf[i] == pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen-2]) + { + pPortParam->m_iRevStatus++; + pPortParam->m_iNeedRevLength = buf[i]+5; + } + else + pPortParam->m_iRevStatus = 18; + break; + case 3: // 0x68 + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + pPortParam->m_iNeedRevLength--; + if(0x68 == buf[i]) + pPortParam->m_iRevStatus++; + else + pPortParam->m_iRevStatus = 18; + break; + case 4: // 正确接收数据 + pPortParam->m_iNeedRevLength--; + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(pPortParam->m_iNeedRevLength > 0) + break; + if(buf[i] != 0x16) + { + pPortParam->m_iRevStatus=18; + break; + } + + //if(CheckLpcError(serialport->m_au8RecvBuf, pPortParam->m_iRecvLen) == TRUE) + { + PortDataProcess(); + pPortParam->m_iRevStatus = 0; + pPortParam->RevCmdFlag = 1; + } + pPortParam->m_iRecvLen = 0; + break; + case 255:// 错误接收数据 + default: + if(buf[i] == 0x68) + { + pPortParam->m_iRevStatus = 1; + pPortParam->m_iRecvLen = 1; + pPortParam->m_au8RecvBuf[0] = buf[i]; + } + else if(buf[i] == 0x16) + { + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + pPortParam->m_iRevStatus = 0; + pPortParam->m_iRecvLen = 0; + } + else + { + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(pPortParam->m_iRecvLen > 200) + { + pPortParam->m_iRecvLen = 0; + } + } + break; + } + } +} + +//int inum =0; +//int itimecnt=0; +static int weather_comm(SERIAL_PARAM weatherport) +{ + int fd = -1; + int len, i,ret, ictime, iruntime, isendtime, irecvtime, icnt=0; + unsigned char sendbuf[] = {0x68,0x00,0x00,0x68,0x01,0x09,0x0a,0x16}; + char recvbuf[256], szbuf[512]; + //char serial_description[] = "/dev/ttyS0"; + +#if 0 + DIR *dir = opendir("/dev"); + if (dir == NULL) { + LOGE("_test_ opendir"); + return -1; + } + + // 读取目录项 + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { + // 过滤出串口设备,通常以"ttyS"或"ttyUSB"开头 + if ((strncmp(entry->d_name, "ttyS2", 5) == 0) || + (strncmp(entry->d_name, "ttyS0", 5) == 0)) { + LOGE("_test_ Found serial port: %s\n", entry->d_name); + } + } + + // 关闭目录 + closedir(dir); +#endif + serialport.RevCmdFlag = 1; + serialport.m_iRecvLen = 0; + serialport.m_iRevStatus = 0; + + set12VEnable(true); + setCam3V3Enable(true); + setRS485Enable(true); + + ictime = (int)time(NULL); + for(;;) + { + if(fd < 0) + { + fd = open(weatherport.pathname, O_RDWR | O_NDELAY); + //fd = open(weatherport.pathname, O_RDWR | O_NOCTTY); + if(fd < 0) + { + LOGE("_test_ open serial error \n"); + perror(weatherport.pathname); + return -1; + } + + ret= set_port_attr (fd, weatherport.baudrate,weatherport.databit,weatherport.stopbit,weatherport.parity,0,0 );/*9600 8n1 */ + if(ret < 0) + { + LOGE("_test_ set uart arrt faile \n"); + return -1; + } + } + + usleep(10000); + iruntime = (int)time(NULL); + if((iruntime - ictime > 120) || (iruntime - ictime < 0)) + ictime = iruntime; + if(iruntime - ictime > 19) + { + memset(szbuf, 0, sizeof(szbuf)); + sprintf(szbuf, "气象采样时间=%d,停止采样!", iruntime-ictime); + LOGE("%s", szbuf); + break; + } + + if(1 == serialport.RevCmdFlag) + { + set485WriteMode(); + + len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */ + serialport.RevCmdFlag = 0; + isendtime = time(NULL); + if (len < 0) { + LOGE("write data error \n"); + return -1; + } else { + memset(szbuf, 0, sizeof(szbuf)); + sprintf(szbuf, "Send:"); + for (i = 0; i < len; i++) { + sprintf(szbuf, "%s %02X", szbuf, sendbuf[i]); + } + LOGE("%s", szbuf); + //icnt = 0; + //inum++; + } + tcdrain(fd); + //usleep(50000); + } + else + { + irecvtime = time(NULL); + if((irecvtime-isendtime > 6) ||(irecvtime - isendtime < 0)) + isendtime = irecvtime; + if (irecvtime-isendtime > 1) + { + LOGE("传感器超过%d秒未应答", irecvtime-isendtime); + serialport.RevCmdFlag = 1; + serialport.m_iRecvLen = 0; + serialport.m_iRevStatus = 0; + close(fd); + //set12VEnable(false); + //setCam3V3Enable(false); + //setRS485Enable(false); + fd = -1; + continue; + } + } + set485ReadMode(); + memset(recvbuf, 0, sizeof(recvbuf)); + len = read(fd, recvbuf, sizeof(recvbuf));/* 在串口读取字符串 */ + if (len < 0) { + LOGE("serial read error \n"); + continue; + } + if(0 == len) + { + //icnt++; + continue; + } + + memset(szbuf, 0, sizeof(szbuf)); + sprintf(szbuf, "Recv:"); + for (i = 0; i < len; i++) { + sprintf(szbuf, "%s %02X", szbuf, recvbuf[i]); + } + __android_log_print(ANDROID_LOG_INFO, "serial", "%s", szbuf); + RecvData((u_char*)recvbuf, len); + //LOGE("一周期空循环次数%d, 读取次数%d, 时间:%d %d", icnt, inum, (int)time(NULL), itimecnt); + icnt = 0; + //serialport.RevCmdFlag =1; + } + + close(fd); + set12VEnable(false); + setCam3V3Enable(false); + setRS485Enable(false); + + //exit(-1); + return(0); +} + +int serial_port_comm() +{ + SERIAL_PARAM portparm; +#if 1 + memset(portparm.pathname, 0, sizeof(portparm.pathname)); + sprintf(portparm.pathname, "/dev/ttyS0"); + portparm.parity = 'N'; + portparm.databit = 8; + portparm.baudrate = B9600; + memset(portparm.stopbit, 0, sizeof(portparm.stopbit)); + sprintf(portparm.stopbit, "1"); +#endif + //itimecnt = (int)time(NULL); + + for(;;) + weather_comm(portparm); + return 0; +} + diff --git a/TestComm/app/src/main/cpp/WeatherComm.h b/TestComm/app/src/main/cpp/WeatherComm.h new file mode 100644 index 00000000..80c47f98 --- /dev/null +++ b/TestComm/app/src/main/cpp/WeatherComm.h @@ -0,0 +1,51 @@ +// +// Created by hyz on 2024/6/5. +// + +#ifndef WEATHERCOMM_H +#define WEATHERCOMM_H + +#include +#include "GPIOControl.h" + +#define MAX_STRING_LEN 32 +#define IOT_PARAM_WRITE 0xAE +#define IOT_PARAM_READ 0xAF + +#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, "serial_port_comm", fmt, ##args) + +// 串口参数 +typedef struct +{ + int baudrate; /* 波特率*/ + int databit; /* 数据位*/ + char stopbit[8]; /* 停止位*/ + char parity; /* 校验位*/ + char pathname[128];/* 串口文件名及路径*/ +} SERIAL_PARAM; + +typedef struct +{ + int m_iRevStatus; /* */ + int m_iRecvLen; /* */ + int m_iNeedRevLength; /* */ + int iRecvTime; /* */ + int RevCmdFlag; + unsigned char m_au8RecvBuf[128];/* */ +} SIO_PARAM_SERIAL_DEF; + +typedef struct +{ + int cmd; + int value; + int result; + long value2; + char str[MAX_STRING_LEN]; +}IOT_PARAM; + +void PortDataProcess( void ); +int serial_port_comm(); +static int weather_comm(SERIAL_PARAM weatherport); +int set_port_attr (int fd, int baudrate, int databit, const char *stopbit, char parity, int vtime, int vmin ); + +#endif //WEATHERCOMM_H diff --git a/TestComm/app/src/main/cpp/native-lib.cpp b/TestComm/app/src/main/cpp/native-lib.cpp index 9d693290..d88f2496 100644 --- a/TestComm/app/src/main/cpp/native-lib.cpp +++ b/TestComm/app/src/main/cpp/native-lib.cpp @@ -16,58 +16,37 @@ #include #include #include +#include #include "GPIOControl.h" +#include "WeatherComm.h" #if 0 -int main() { - int fd; - struct termios options; - char *serialPort = "/dev/ttyS0"; // 串口设备文件路径 - char *message = "Hello, serial port!\n"; - int messageLength = strlen(message); - char buffer[50]; - int bytesRead; - - // 打开串口设备 - fd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY); - if (fd == -1) { - perror("open_port: Unable to open serial port - "); - return(-1); - } - - // 获取并配置串口选项 - tcgetattr(fd, &options); - cfsetispeed(&options, B9600); // 输入波特率 - cfsetospeed(&options, B9600); // 输出波特率 - options.c_cflag |= (CLOCAL | CREAD); // 开启接收 - options.c_cflag &= ~CSIZE; // 清除当前数据位设置 - options.c_cflag |= CS8; // 8位数据位 - options.c_cflag &= ~PARENB; // 关闭校验位 - options.c_cflag &= ~CSTOPB; // 1位停止位 - options.c_cc[VTIME] = 0; // 非阻塞读取 - options.c_cc[VMIN] = 10; // 读取字符的最小数目 - tcflush(fd, TCIFLUSH); // 清空输入缓冲区 - - // 使用配置好的选项 - tcsetattr(fd, TCSANOW, &options); - - // 写入数据到串口 - write(fd, message, messageLength); - - // 从串口读取数据 - bytesRead = read(fd, buffer, sizeof(buffer)); - if (bytesRead > 0) { - buffer[bytesRead] = '\0'; // 确保字符串以null结尾 - printf("Response from serial port: %s\n", buffer); - } - - // 关闭串口 - close(fd); +#define BYTE u_char; +// 串口参数 +typedef struct +{ + int baudrate; /* 波特率*/ + int databit; /* 数据位*/ + char stopbit[8]; /* 停止位*/ + char parity; /* 校验位*/ + char pathname[128];/* 串口文件名及路径*/ + } SERIAL_PARAM; - return 0; -} +typedef struct +{ + int m_iRevStatus; /* */ + int m_iRecvLen; /* */ + int m_iNeedRevLength; /* */ + int iRecvTime; /* */ + int RevCmdFlag; + unsigned char m_au8RecvBuf[128];/* */ +} SIO_PARAM_SERIAL_DEF; + +extern SIO_PARAM_SERIAL_DEF serialport; +extern float weatherpntmsg[10]; #endif +//set_port_attr (int fd, int baudrate, int databit, const char *stopbit, char parity, int vtime, int vmin ) class NrsecSpiPort : public SpiPort { public: NrsecSpiPort(const std::string& path) : SpiPort(path) { @@ -114,6 +93,7 @@ public: } }; +#if 0 static void set_baudrate (struct termios *opt, unsigned int baudrate) { cfsetispeed(opt, baudrate); @@ -192,12 +172,13 @@ int set_port_attr (int fd, int baudrate, int databit, const char *stopbit, char struct termios opt; tcgetattr(fd, &opt); set_baudrate(&opt, baudrate); - opt.c_cflag |= CLOCAL|CREAD; /*|CRTSCTS */ + //opt.c_cflag |= CLOCAL|CREAD; /*|CRTSCTS */ + opt.c_lflag &= ~(ICANON | ECHO |ECHOE |ISIG); set_data_bit(&opt, databit); set_parity(&opt, parity); set_stopbit(&opt, stopbit); opt.c_oflag = 0; - opt.c_lflag |= 0; + //opt.c_lflag |= 0; opt.c_oflag &= ~OPOST; opt.c_cc[VTIME] = vtime; opt.c_cc[VMIN] = vmin; @@ -217,8 +198,8 @@ typedef struct char str[MAX_STRING_LEN]; }IOT_PARAM; -#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, "_test_", fmt, ##args) - +#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, "serial_port_comm", fmt, ##args) +#endif void setInt(int cmd, int value) { int fd = open("/dev/mtkgpioctrl", O_RDONLY); @@ -242,21 +223,247 @@ static void set485WriteMode() { setInt(CMD_SET_485_STATE, 1); } + static void set485ReadMode() { setInt(CMD_SET_485_STATE, 0); } static void set12VEnable(bool z) { setInt(CMD_SET_12V_EN_STATE, z ? 1 : 0); } +#if 0 +static void setCam3V3Enable(bool enabled) +{ + setInt(CMD_SET_CAM_3V3_EN_STATE, enabled ? 1 : 0); +} -int serial_port_comm() +/********************************************************************************* +* 气象数据处理 * +**********************************************************************************/ +void PortDataProcess( void ) +{ + float fvalue, fcorvalue, *fvalua, frnb/*, fwind*/; + //WORD uDevAddr; + unsigned char cmdidx; + int i, j, aipnt, datanum; + SIO_PARAM_SERIAL_DEF *pPortParam; + char szbuf[64]; + + pPortParam = &serialport; + //取出装置地址,开始处理地址+++ + if(0x02 == pPortParam->m_au8RecvBuf[5]) + { + //pPortParam->devaddr = pPortParam->m_au8RecvBuf[4]; + return; + } + cmdidx = pPortParam->m_au8RecvBuf[5]; +#if 0 + aipnt = pPortParam->SameTypeDevIdx; + uDevAddr = serialport->m_au8RecvBuf[4]; + if(0 == srdt.IsReadWireTem) + { + if(uDevAddr != pPortParam->devaddr) + return; + } +#endif + fvalua = &fvalue; + datanum = pPortParam->m_au8RecvBuf[6]; + if((0x08 != cmdidx) && (0x09 != cmdidx)) + return; + + for(i = 0, j=7; (im_au8RecvBuf[1]); i++, j+=5 ) + { + if(0x08 == cmdidx) + fvalue = (pPortParam->m_au8RecvBuf[j+1]<<24)+(pPortParam->m_au8RecvBuf[j+2]<<16) + +(pPortParam->m_au8RecvBuf[j+3]<<8)+pPortParam->m_au8RecvBuf[j+4]; + else + { + *(u_char *)fvalua = pPortParam->m_au8RecvBuf[j+4]; + *((u_char *)fvalua+1) = pPortParam->m_au8RecvBuf[j+3]; + *((u_char *)fvalua+2) = pPortParam->m_au8RecvBuf[j+2]; + *((u_char *)fvalua+3) = pPortParam->m_au8RecvBuf[j+1]; + } + switch(pPortParam->m_au8RecvBuf[j]) + { + case 1: /*温度*/ + weatherpntmsg[0] = fvalue; + LOGE("温度:%0.3f ", fvalue); + break; + case 2: /*气压*/ + weatherpntmsg[5] = fvalue; + LOGE("气压:%0.3f ", fvalue); + break; + case 3: /*湿度*/ + weatherpntmsg[1] = fvalue; + LOGE("湿度:%0.3f ", fvalue); + break; + case 4: /*雨量*/ + break; + case 5: /*日照*/ + break; + case 6: /*风速*/ + weatherpntmsg[2] = fvalue; + LOGE("风速:%0.3f ", fvalue); + break; + case 7: /*风向*/ + weatherpntmsg[3] = fvalue; + LOGE("风向:%0.3f ", fvalue); + break; + case 8: /*拉力*/ + case 9: /*倾角传感器X轴倾角*/ + case 10: /*倾角传感器Y轴倾角*/ + case 11: /*测温球导线温度*/ + case 12: /*测温球内部温度*/ + break; + case 13: /*测温球导线X轴倾角*/ + break; + case 14: /*测温球导线Y轴倾角*/ + break; + case 15: /*测温球导线电流*/ + break; + case 16: /*测温球电池电压*/ + break; + case 17: /*A相泄漏电流平均值;*/ + break; + case 18: /*A相泄漏电流最大值;*/ + break; + case 19: /*A相超过3mA的脉冲频次*/ + break; + case 20: /*A相超过10mA的脉冲频次*/ + break; + case 21: /*B相泄漏电流平均值;*/ + break; + case 22: /*B相泄漏电流最大值;*/ + break; + case 23: /*B相超过3mA的脉冲频次*/ + break; + case 24: /*B相超过10mA的脉冲频次*/ + case 25: /*C相泄漏电流平均值;*/ + case 26: /*C相泄漏电流最大值;*/ + case 27: /*C相超过3mA的脉冲频次*/ + case 28: /*C相超过10mA的脉冲频次*/ + break; + } + } +} + +//*************************************************************** +//* 按照协议格式化接收数据 * +//*************************************************************** +void RecvData(u_char *buf, int len)// 规约读数据处理 +{ + int i, ictime; + //WORD crc, check; + SIO_PARAM_SERIAL_DEF *pPortParam; + + pPortParam = &serialport; + ictime = (int)time(NULL); + + if(pPortParam->m_iRecvLen == 0) + { + pPortParam->iRecvTime = ictime; + } + else + { + if((ictime-pPortParam->iRecvTime > 6) || (ictime - pPortParam->iRecvTime < 0)) + pPortParam->iRecvTime = ictime; + else if(ictime - pPortParam->iRecvTime > 2) + { + pPortParam->m_iRecvLen = 0; + pPortParam->m_iRevStatus = 0; + } + } + + for(i=0; im_iRevStatus) + { + case 0: // 0x68 + pPortParam->m_iRecvLen = 0; + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(0x68 == buf[i]) + pPortParam->m_iRevStatus++; + else + pPortParam->m_iRevStatus = 18; + break; + case 1: // len1 + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + pPortParam->m_iRevStatus++; + break; + case 2: // len2 + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(buf[i] == pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen-2]) + { + pPortParam->m_iRevStatus++; + pPortParam->m_iNeedRevLength = buf[i]+5; + } + else + pPortParam->m_iRevStatus = 18; + break; + case 3: // 0x68 + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + pPortParam->m_iNeedRevLength--; + if(0x68 == buf[i]) + pPortParam->m_iRevStatus++; + else + pPortParam->m_iRevStatus = 18; + break; + case 4: // 正确接收数据 + pPortParam->m_iNeedRevLength--; + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(pPortParam->m_iNeedRevLength > 0) + break; + if(buf[i] != 0x16) + { + pPortParam->m_iRevStatus=18; + break; + } + + //if(CheckLpcError(serialport->m_au8RecvBuf, pPortParam->m_iRecvLen) == TRUE) + { + PortDataProcess(); + pPortParam->m_iRevStatus = 0; + pPortParam->RevCmdFlag = 1; + } + pPortParam->m_iRecvLen = 0; + break; + case 255:// 错误接收数据 + default: + if(buf[i] == 0x68) + { + pPortParam->m_iRevStatus = 1; + pPortParam->m_iRecvLen = 1; + pPortParam->m_au8RecvBuf[0] = buf[i]; + } + else if(buf[i] == 0x16) + { + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + pPortParam->m_iRevStatus = 0; + pPortParam->m_iRecvLen = 0; + } + else + { + pPortParam->m_au8RecvBuf[pPortParam->m_iRecvLen++] = buf[i]; + if(pPortParam->m_iRecvLen > 200) + { + pPortParam->m_iRecvLen = 0; + } + } + break; + } + } +} + +//int inum =0; +//int itimecnt=0; +int weather_comm(SERIAL_PARAM weatherport) { - int fd; - int len, i,ret; - unsigned char sendbuf[] = {0x68,0x00,0x00,0x68,0xFF,0x02,0x01,0x16, 0x0D,0x0A}; - char recvbuf[256]; - char serial_description[] = "/dev/ttyS0"; + int fd = -1; + int len, i,ret, ictime, iruntime, isendtime, irecvtime, icnt=0; + unsigned char sendbuf[] = {0x68,0x00,0x00,0x68,0x01,0x09,0x0a,0x16}; + char recvbuf[256], szbuf[512]; + //char serial_description[] = "/dev/ttyS0"; +#if 0 DIR *dir = opendir("/dev"); if (dir == NULL) { LOGE("_test_ opendir"); @@ -275,42 +482,144 @@ int serial_port_comm() // 关闭目录 closedir(dir); +#endif + serialport.RevCmdFlag = 1; + serialport.m_iRecvLen = 0; + serialport.m_iRevStatus = 0; - //fd = open(serial_description, O_RDWR | O_NDELAY); - fd = open(serial_description, O_RDWR | O_NOCTTY); - if(fd < 0) - { - LOGE("_test_ open serial error \n"); - perror(serial_description); - return -1; - } - ret= set_port_attr (fd, B9600,8,"1",'N',150,0 );/*9600 8n1 */ - if(ret < 0) - { - LOGE("_test_ set uart arrt faile \n"); - exit(-1); - } - set485WriteMode(); - len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */ - if (len < 0) - { - LOGE("_test_ write data error \n"); - return -1; - } - set485ReadMode(); - memset(recvbuf, 0, sizeof(recvbuf)); - len = read(fd, recvbuf, sizeof(recvbuf));/* 在串口读取字符串 */ - if (len < 0) + set12VEnable(true); + setCam3V3Enable(true); + setRS485Enable(true); + + ictime = (int)time(NULL); + for(;;) { - LOGE("_test_ read error \n"); - return -1; + if(fd < 0) + { + fd = open(weatherport.pathname, O_RDWR | O_NDELAY); + //fd = open(weatherport.pathname, O_RDWR | O_NOCTTY); + if(fd < 0) + { + LOGE("_test_ open serial error \n"); + perror(weatherport.pathname); + return -1; + } + + ret= set_port_attr (fd, weatherport.baudrate,weatherport.databit,weatherport.stopbit,weatherport.parity,0,0 );/*9600 8n1 */ + if(ret < 0) + { + LOGE("_test_ set uart arrt faile \n"); + return -1; + } + } + + usleep(10000); + iruntime = (int)time(NULL); + if((iruntime - ictime > 120) || (iruntime - ictime < 0)) + ictime = iruntime; + if(iruntime - ictime > 19) + { + memset(szbuf, 0, sizeof(szbuf)); + sprintf(szbuf, "气象采样时间=%d,停止采样!", iruntime-ictime); + LOGE("%s", szbuf); + break; + } + + if(1 == serialport.RevCmdFlag) + { + set485WriteMode(); + + len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */ + serialport.RevCmdFlag = 0; + isendtime = time(NULL); + if (len < 0) { + LOGE("write data error \n"); + return -1; + } else { + memset(szbuf, 0, sizeof(szbuf)); + sprintf(szbuf, "Send:"); + for (i = 0; i < len; i++) { + sprintf(szbuf, "%s %02X", szbuf, sendbuf[i]); + } + LOGE("%s", szbuf); + //icnt = 0; + //inum++; + } + tcdrain(fd); + //usleep(50000); + } + else + { + irecvtime = time(NULL); + if((irecvtime-isendtime > 6) ||(irecvtime - isendtime < 0)) + isendtime = irecvtime; + if (irecvtime-isendtime > 1) + { + LOGE("传感器超过%d秒未应答", irecvtime-isendtime); + serialport.RevCmdFlag = 1; + serialport.m_iRecvLen = 0; + serialport.m_iRevStatus = 0; + close(fd); + //set12VEnable(false); + //setCam3V3Enable(false); + //setRS485Enable(false); + fd = -1; + continue; + } + } + set485ReadMode(); + memset(recvbuf, 0, sizeof(recvbuf)); + len = read(fd, recvbuf, sizeof(recvbuf));/* 在串口读取字符串 */ + if (len < 0) { + LOGE("serial read error \n"); + continue; + } + if(0 == len) + { + //icnt++; + continue; + } + + memset(szbuf, 0, sizeof(szbuf)); + sprintf(szbuf, "Recv:"); + for (i = 0; i < len; i++) { + sprintf(szbuf, "%s %02X", szbuf, recvbuf[i]); + } + __android_log_print(ANDROID_LOG_INFO, "serial", "%s", szbuf); + RecvData((u_char*)recvbuf, len); + //LOGE("一周期空循环次数%d, 读取次数%d, 时间:%d %d", icnt, inum, (int)time(NULL), itimecnt); + icnt = 0; + //serialport.RevCmdFlag =1; } - LOGE("_test_ recv=%s \n", recvbuf); /* 打印在串口读取的字符串 */ - //lxy modify + close(fd); + set12VEnable(false); + setCam3V3Enable(false); + setRS485Enable(false); + + //exit(-1); return(0); } +int serial_port_comm() +{ + SERIAL_PARAM portparm; +#if 1 + memset(portparm.pathname, 0, sizeof(portparm.pathname)); + sprintf(portparm.pathname, "/dev/ttyS0"); + portparm.parity = 'N'; + portparm.databit = 8; + portparm.baudrate = B9600; + memset(portparm.stopbit, 0, sizeof(portparm.stopbit)); + sprintf(portparm.stopbit, "1"); +#endif + //itimecnt = (int)time(NULL); + + for(;;) + weather_comm(portparm); + return 0; +} +#endif extern "C" JNIEXPORT jstring JNICALL Java_com_xinyingpower_testcomm_MainActivity_stringFromJNI( JNIEnv* env, @@ -346,15 +655,15 @@ Java_com_xinyingpower_testcomm_MainActivity_testSpi( //a.SM2VerifySign(0x00,outpub,outsign); //lxy modify modify - LOGE("_test_ setRS485Enable true"); - setRS485Enable(true); + //LOGE("_test_ setRS485Enable true"); + //setRS485Enable(true); - set12VEnable(true); + //set12VEnable(true); serial_port_comm(); //lxy modify modify - LOGE("_test_ setRS485Enable false"); - setRS485Enable(false); + //LOGE("_test_ setRS485Enable false"); + //setRS485Enable(false); return env->NewStringUTF(""); #if 0 diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 72dfad43..348464f8 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -74,7 +74,7 @@ find_package(ncnn REQUIRED) SET(YAMC_INC_DIR ${CMAKE_SOURCE_DIR}) # SET(TERM_CORE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../xymp/Core) -#SET(TERM_CORE_ROOT D:/Workspace/xymp/Core) +#SET(TERM_CORE_ROOT D:/shxy/xymp/Core) SET(JSONCPP_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp) SET(JSONCPP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp/include) diff --git a/gradle.properties b/gradle.properties index 47a75250..0cc565fc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,11 +23,11 @@ COMPILE_SDK_VERSION=33 TARGET_SDK_VERSION=28 COMPILE_MIN_SDK_VERSION=25 -opencvsdk=D:/Workspace/deps/opencv-mobile-4.9.0-android +opencvsdk=D:/shxy/android_lib/opencv-mobile-4.9.0-android # opencvsdk=D:/Workspace/deps/opencv-mobile-3.4.20-android -coreroot=D:/Workspace/Github/xymp/xymp/Core +coreroot=D:/shxy/xymp/Core # opencvsdk=D:/Workspace/deps/opencv-v5 -asioroot=D:/Workspace/deps/asio-1.28.0 +asioroot=D:/shxy/android_lib/asio-1.28.0 evpproot=D:/Workspace/Github/evpp -ncnnroot=D:/Workspace/deps/ncnn-20240410-android-vulkan -libzipsdkroot=D:/Workspace/deps/libzip-android-sdk +ncnnroot=D:/shxy/android_lib/ncnn-20230517-android-vulkan +libzipsdkroot=D:/shxy/android_lib/libzip-android-sdk