From 01df5523a70213e0e30f1d1da3772ce01ee31c2b Mon Sep 17 00:00:00 2001 From: chenxin Date: Thu, 23 Apr 2026 11:30:05 +0800 Subject: [PATCH] 1 --- .vscode/settings.json | 12 +- VinceMotorControllerUsb.cpp | 357 ----- VinceMotorControllerUsb.h | 50 - device_config.json | 73 - main_test.cpp | 2650 ---------------------------------- src/VSMD_command_handler.cpp | 208 ++- src/VSMD_command_handler.h | 3 + src/bluetooth_handler.cpp | 2 +- src/main.cpp | 44 +- test5.json | 23 - vsmd_IRIS.txt | 287 ++++ 11 files changed, 480 insertions(+), 3229 deletions(-) delete mode 100644 VinceMotorControllerUsb.cpp delete mode 100644 VinceMotorControllerUsb.h delete mode 100644 device_config.json delete mode 100644 main_test.cpp delete mode 100644 test5.json create mode 100644 vsmd_IRIS.txt diff --git a/.vscode/settings.json b/.vscode/settings.json index 5f4f2bf..73fdcda 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -66,6 +66,16 @@ "functional": "cpp", "tuple": "cpp", "type_traits": "cpp", - "utility": "cpp" + "utility": "cpp", + "random": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "cmath": "cpp", + "streambuf": "cpp" } } \ No newline at end of file diff --git a/VinceMotorControllerUsb.cpp b/VinceMotorControllerUsb.cpp deleted file mode 100644 index 44f6892..0000000 --- a/VinceMotorControllerUsb.cpp +++ /dev/null @@ -1,357 +0,0 @@ -#include "VinceMotorControllerUsb.h" - -VinceMotorControllerUsb::VinceMotorControllerUsb(QString serialPortNumber, QString paramJson, QObject* parent) -:QObject(parent) -{ - m_pSerialPort = new QSerialPort(this); - - m_pSerialPort->setPortName(serialPortNumber); - m_pSerialPort->setReadBufferSize(512); - - bool bRes = m_pSerialPort->setBaudRate(9600); - if (!bRes) - { - //qDebug() << "Err:setBaudRate Failed.Exit Code:1"; - //std::cout << "Err.setBaudRate Failed" << std::endl; - printf("Err:setBaudRate Failed.Exit Code:1"); - } - - bRes = m_pSerialPort->open(QIODevice::ReadWrite); - if (!bRes) - { - //qDebug() << "Err:open Failed.Exit Code:2"; - //std::cout << "Err.open Failed" << std::endl; - printf("Err:open Failed.Exit Code:2"); - } - - m_iSpeed = 3000; - - init(paramJson); -} - -VinceMotorControllerUsb::~VinceMotorControllerUsb() -{ - delete m_pSerialPort; -} - -int VinceMotorControllerUsb::sendCommand2Motor(QString cmd) -{ - QByteArray commandData = cmd.toUtf8(); // ½« QString ת»»Ϊ QByteArray - qint64 bytesWritten = m_pSerialPort->write(commandData); - - /*if (bytesWritten == -1) { - qDebug() << "дÈëʧ°Ü:" << m_pSerialPort->errorString(); - } - else if (bytesWritten != commandData.size()) { - qDebug() << "дÈ벻ÍêÕû"; - } - else { - qDebug() << "ÃüÁËͳɹ¦"; - }*/ - - return bytesWritten; -} - -int VinceMotorControllerUsb::recvData(QByteArray& dataRecv) -{ - dataRecv.clear(); - - QByteArray temp; - - temp = m_pSerialPort->readAll(); - dataRecv.append(temp); - - while (dataRecv.size() < 21) - { - m_pSerialPort->waitForReadyRead(100); - temp = m_pSerialPort->readAll(); - dataRecv.append(temp); - } - - return 0; -} - -IrisMotorErrorCode VinceMotorControllerUsb::extractOneValidFrame(QByteArray& buffer) -{ - //std::cout << "before------" << buffer.size() << std::endl; - //std::cout << buffer.toHex().toStdString() << std::endl; - - IrisMotorErrorCode errorCode = MOTOR_NO_ERROR; - - //·½ʽ1£º - /*int startPos = buffer.lastIndexOf('\xFF'); - int endPos = buffer.indexOf('\xFE'); - - if (endPos <= startPos) - { - errorCode = MOTOR_INVALID_FRAME; - return errorCode; - } - - buffer = buffer.mid(startPos, endPos - startPos + 1);*/ - - //·½ʽ2£º - //ff00ffffff000200000000000000391f6900000d42030001feff00ff000200000000000000391f6900000d42030001feff0002043458000000003c745e00000d402b001afeff00ff0002000000000000003d3b7200000d4207003efeff00020c34580000000039076e00000d4023005cfeff00ff00020000000000000038411900000d420f0022feff000204345800000000477e7700000d402b0042feff00ff00020000000000000048456a00000d420b0021feff00020434245400000049006d00000d400b0020feff00ff000200000000000000493c3100000d4203000afeff0002043458000000004d444b00000d402f004afe - //ff00ffffffff000200000000000000112d6f00000c42070018fe - QList validFrames; - int start = 0; - - while (start < buffer.size()) - { - // ²éÕÒ֡ͷ 'ff' - int frameStart = buffer.indexOf(MOTOR_SYNC, start); - if (buffer.at(frameStart + 1) == MOTOR_SYNC) - continue; - if (frameStart == -1) - { - break; // ûÓÐÕҵ½֡ͷ£¬Í˳öѭ»· - } - - // ²éÕÒ֡β 'fe' - int frameEnd = buffer.indexOf(MOTOR_ETX, frameStart + 1); - if (frameEnd == -1) - { - break; // ûÓÐÕҵ½֡⣬Í˳öѭ»· - } - - // ÌáȡÓÐЧ֡£¨°üÀ¨֡ͷºÍ֡⣩ - QByteArray frame = buffer.mid(frameStart, frameEnd - frameStart + 1); - - // ¼ì²é֡³¤¶ÈÊǷñµÈÓÚ21 - if (frame.size() == 21) - { - validFrames.append(frame); - } - - // ¸üÐÂÆðʼλÖ㬼ÌÐø²éÕÒÏÂһ¸ö֡ - start = frameEnd + 1; - } - if (validFrames.size() == 0) - { - std::cout << "error--------------: " << buffer.toHex().toStdString() << std::endl; - - - errorCode = MOTOR_INVALID_FRAME; - return errorCode; - } - - buffer = validFrames.at(validFrames.size() - 1); - - //·½ʽ3£º´ӽáβ±éÀú£¬Õҵ½ÓÐЧ֡£¨1£©Õҵ½ËùÓÐfe£¨2£©´Ó×îºóһ¸öfe¿ªʼÏòǰÕҵ½ÓÐЧ֡£¨³¤¶ÈΪ21£© - - //std::cout << "after" << buffer.size() << std::endl; - //std::cout << buffer.toHex().toStdString() << std::endl; - - return errorCode; -} - -IrisMotorErrorCode VinceMotorControllerUsb::parseOneValidFrame(QByteArray& buf, State& backdatt) -{ - IrisMotorErrorCode errorCode = MOTOR_NO_ERROR; - - backdatt.Speed = MOTOR_disconnection; - - buf.remove(0, 3); - buf.remove(buf.length() - 3, 3); - int location; - unsigned char a[15]; - memcpy(a, buf.data(), 15); - location = ((a[0] & 0x0f) << 28) | ((a[1] & 0x7f) << 21) | ((a[2] & 0x7f) << 14) | ((a[3] & 0x7f) << 7) | ((a[4] & 0x7f)); - float speed; - - memcpy(&speed, &location, 4); - backdatt.Speed = speed; - int loc; - loc = ((a[5] & 0x0f) << 28) | ((a[6] & 0x7f) << 21) | ((a[7] & 0x7f) << 14) | ((a[8] & 0x7f) << 7) | ((a[9] & 0x7f)); - backdatt.Location = loc; - backdatt.Speed = speed; - location = ((a[10] & 0x0f) << 28) | ((a[11] & 0x7f) << 21) | ((a[12] & 0x7f) << 14) | ((a[13] & 0x7f) << 7) | ((a[14] & 0x7f)); - backdatt.Stata = unsigned int(location); - - /*QString str1 = "Speed " + QString::number(backdatt.Speed, 'f', 2) + " location " + QString::number(backdatt.Location); - qDebug() << str1;*/ - - return errorCode; -} - -IrisMotorErrorCode VinceMotorControllerUsb::parseBytedata(QByteArray buf, State& backdatt) -{ - IrisMotorErrorCode errorCode = extractOneValidFrame(buf); - - backdatt.Speed = MOTOR_disconnection; - backdatt.Location = 0; - if (errorCode == MOTOR_NO_ERROR) - { - parseOneValidFrame(buf, backdatt); - return errorCode; - } - - errorCode = MOTOR_INVALID_FRAME; - return errorCode; -} - -void VinceMotorControllerUsb::init(QString jsonPath) -{ - enable(); - - JsonOperate xxx(jsonPath); - //xxx.createJsonFile(); - QStringList re = xxx.parseJsonFile(); - - for (const QString& pair : re) - { - //qDebug().noquote() << pair; - sendCommand2Motor(pair); - - QByteArray buf; - recvData(buf); - } - - //QString cmd; - - ////cmd = "cfg s1f=2\n";//Óò»ÉÏ - ////sendCommand2Motor(cmd); - - ////¹éÁãÉèÖà - //cmd = "cfg zmd=2\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg snr=0\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg osv=0\n";//¹éÁãÓô«¸ÐÆ÷³£¿ª³£±ÕÉèÖà - //sendCommand2Motor(cmd); - //cmd = "cfg zsd=3000\n";//ÕýÊýÏòԭµã×ß - //sendCommand2Motor(cmd); - //cmd = "cfg zsp=2400\n"; - //sendCommand2Motor(cmd); - - ////ÉèÖÃÕý¸º¼«ÏÞ - //cmd = "cfg msr=1\n";//¸º¼«ÏÞ - //sendCommand2Motor(cmd); - //cmd = "cfg msv=0\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg psr=2\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg psv=0\n"; - //sendCommand2Motor(cmd); - - ////¼ӼõËٶȺ͵çÁ÷ÉèÖà - //cmd = "cfg mcs=7\n";//ÉèÖÃϸ·Ö - //sendCommand2Motor(cmd); - //cmd = "cfg acc=20000\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg dec=20000\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg crn=4\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg cra=4\n"; - //sendCommand2Motor(cmd); - //cmd = "cfg crh=1\n"; - //sendCommand2Motor(cmd); -} - -bool VinceMotorControllerUsb::isConnected() -{ - State re = getState(); - if (re.Speed == MOTOR_disconnection) - { - return false; - } - else - { - return true; - } -} - -void VinceMotorControllerUsb::enable() -{ - QString cmd = "ena\n"; - sendCommand2Motor(cmd); - QByteArray buf; - recvData(buf); -} - -void VinceMotorControllerUsb::move2Pos(double pos) -{ - QString cmd = "pos " + QString::number(pos, 'f', 10) + "\n"; - sendCommand2Motor(cmd); - QByteArray buf; - recvData(buf); -} - -void VinceMotorControllerUsb::move(bool towardOrigin) -{ - if (towardOrigin) - { - setSpeed(abs(m_iSpeed) * -1); - } - else - { - setSpeed(abs(m_iSpeed)); - } - - sendCommand2Motor("mov\n"); - QByteArray buf; - recvData(buf); -} - -void VinceMotorControllerUsb::stopMove() -{ - QString cmd = "stp\n"; - sendCommand2Motor(cmd); - QByteArray buf; - recvData(buf); -} - -void VinceMotorControllerUsb::setSpeed(double speed) -{ - m_iSpeed = speed; - QString cmd = "cfg spd=" + QString::number(speed) + "\n"; - sendCommand2Motor(cmd); - QByteArray buf; - recvData(buf); -} - -double VinceMotorControllerUsb::getSpeed() -{ - State re = getState(); - return re.Speed; -} - -double VinceMotorControllerUsb::getCurrentLoc() -{ - State re = getState(); - return re.Location; -} - -void VinceMotorControllerUsb::zeroStart() -{ - QString cmd = "zero start\n"; - sendCommand2Motor(cmd); - QByteArray buf; - recvData(buf); -} - -int VinceMotorControllerUsb::getSpeedLocation(double& speed, double& locatioin) -{ - State re = getState(); - speed = re.Speed; - locatioin = re.Location; - - return 0; -} - -State VinceMotorControllerUsb::getState() -{ - QString cmd = "sts\n"; - sendCommand2Motor(cmd); - - QByteArray buf; - recvData(buf); - //std::cout << buf.size() << std::endl; - //std::cout << buf.toHex().toStdString() << std::endl; - - State back; - IrisMotorErrorCode errorCode = parseBytedata(buf, back); - - return back; -} diff --git a/VinceMotorControllerUsb.h b/VinceMotorControllerUsb.h deleted file mode 100644 index d89d4ff..0000000 --- a/VinceMotorControllerUsb.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -#include "JsonOperate.h" -#include "MotorControllerBase.h" - -#define MOTOR_SYNC (0xFF)//֡ͷ -#define MOTOR_ETX (0xFE)//֡β - -#define MOTOR_disconnection -1000000//ËٶȵÈÓÚÕâ¸öÊý¾ʹú±íÂí´ïͨѶÒ쳣 - -class VinceMotorControllerUsb : - public MotorControllerBase, QObject -{ - Q_OBJECT - -public: - VinceMotorControllerUsb(QString serialPort, QString paramJson, QObject* parent = nullptr); - ~VinceMotorControllerUsb(); - - void init(QString jsonPath); - int sendCommand2Motor(QString cmd); - bool isConnected(); - void enable(); - - void move2Pos(double pos); - void move(bool towardOrigin);//true£ºԶÀëԭµ㣬false£º¿¿½üԭµã - void stopMove(); - void setSpeed(double speed); - double getSpeed(); - double getCurrentLoc(); - void zeroStart(); - - int getSpeedLocation(double& speed, double& locatioin); - - State getState(); - int recvData(QByteArray& dataRecv); - IrisMotorErrorCode parseBytedata(QByteArray buf, State& backdatt); - IrisMotorErrorCode extractOneValidFrame(QByteArray& buffer); - IrisMotorErrorCode parseOneValidFrame(QByteArray& buffer, State& result); - -private: - QSerialPort* m_pSerialPort; - - double m_iSpeed; -}; diff --git a/device_config.json b/device_config.json deleted file mode 100644 index 2bf40d0..0000000 --- a/device_config.json +++ /dev/null @@ -1,73 +0,0 @@ -{"command": "cfg"} -{"command": "ena"} -{"command": "off"} -{"command": "mov"} -{"command": "org"} -{"command": "dev"} -{"command": "sts"} -{"command": "zero_start"} -{"command": "zero_stop"} -{"command": "sav"} - -{"command": "pos", "PA": {"value": 1000}} -{"command": "rmv", "PA": {"value": 500}} -{"command": "pps", "PA": {"value": 1000}} -{"command": "stp", "PA": {"value": 1}} - -{"command": "cfg_bdr", "PA": {"value": 115200}} -{"command": "cfg_mcs", "PA": {"value": 5}} -{"command": "cfg_spd", "PA": {"value": 10000}} -{"command": "cfg_acc", "PA": {"value": 100000}} -{"command": "cfg_dec", "PA": {"value": 100000}} -{"command": "cfg_cra", "PA": {"value": 1.5}} -{"command": "cfg_crn", "PA": {"value": 1.0}} -{"command": "cfg_crh", "PA": {"value": 0.5}} - -{"command": "cfg_zmd", "PA": {"value": 2}} -{"command": "cfg_snr", "PA": {"value": 1}} -{"command": "cfg_osv", "PA": {"value": 0}} -{"command": "cfg_zsd", "PA": {"value": 5000}} -{"command": "cfg_zsp", "PA": {"value": 100}} - -{"command": "cfg_dmd", "PA": {"value": 1}} -{"command": "cfg_dar", "PA": {"value": 30}} - -{"command": "cfg_msr", "PA": {"value": 3}} -{"command": "cfg_msv", "PA": {"value": 0}} -{"command": "cfg_psr", "PA": {"value": 4}} -{"command": "cfg_psv", "PA": {"value": 1}} - -//编码器配置命令 -{"command": "cfg_pae", "PA": {"value": 1}} -{"command": "cfg_emod", "PA": {"value": 2}} -{"command": "cfg_elns", "PA": {"value": 1000}} -{"command": "cfg_estp", "PA": {"value": 200}} -{"command": "cfg_erty", "PA": {"value": 3}} -{"command": "cfg_edir", "PA": {"value": 0}} -{"command": "cfg_ez", "PA": {"value": 5}} -{"command": "cfg_ewr", "PA": {"value": 1}} - -//S3-S6传感器触发事件配置命令 -{"command": "cfg_s3f", "PA": {"value": 5}} -{"command": "cfg_s3r", "PA": {"value": 6}} -{"command": "cfg_s4f", "PA": {"value": 7}} -{"command": "cfg_s4r", "PA": {"value": 8}} -{"command": "cfg_s5f", "PA": {"value": 9}} -{"command": "cfg_s5r", "PA": {"value": 0}} -{"command": "cfg_s6f", "PA": {"value": 1}} -{"command": "cfg_s6r", "PA": {"value": 2}} - -{"command": "cfg_s3", "PA": {"value": 1}} -{"command": "cfg_s4", "PA": {"value": 0}} -{"command": "cfg_s5", "PA": {"value": 1}} -{"command": "cfg_s6", "PA": {"value": 0}} - -//S3-S6端口输出控制命令 -{"command": "s3_on"} -{"command": "s3_off"} -{"command": "s4_on"} -{"command": "s4_off"} -{"command": "s5_on"} -{"command": "s5_off"} -{"command": "s6_on"} -{"command": "s6_off"} diff --git a/main_test.cpp b/main_test.cpp deleted file mode 100644 index 773c3e2..0000000 --- a/main_test.cpp +++ /dev/null @@ -1,2650 +0,0 @@ -#include"Define.h" -#include -#include -#include -#include -#include -#include -#include -//#include "MyWebServer.h" -#include "SDmanger.h" -//#include "HttpsOTAUpdate.h" -#include "slave.h" -#include "log.h" - -#include -#include -#include - -#include "IRIS_Method.h" -#include "DS18B20.h" -#include "Servo.h" -#include "jiaresi.h" -#include -#include "servo.h" -#include "SensorIS11.h" -#include "tuigan.h" -#include "beep.h" -#include "My74HC595.h" -#include "SolarCalculator.h" -#define LOGGING -#define wb485PORT_RX 3 -#define wb485PORT_TX 2 - -#define MY74HC595_dataPin 35 -#define MY74HC595_clockPin 33 -#define MY74HC595_latchPin 34 - -#define IS1Serial_port 1 -#define IS1Serial_RX 18 -#define IS1Serial_TX 17 -// u_char ret[17]; - -// Ticker ticker; - -/* - 任务名:Task0 - 任务功能:光谱数据采集 -*/ -TaskHandle_t Task0_Handler; -void Task0(void *pvParameters); -/* - 任务名:Task1 - 任务功能:外设控制,加热 、推杆、舵机、温度采集、时间获取 -*/ -TaskHandle_t Task1_Handler; -void Task1(void *pvParameters); - -/* - 任务名:Task2 - 任务功能:数据上传 -*/ -TaskHandle_t Task2_Handler; -void Task2(void *pvParameters); - -//work_mode 0:手动 1:时间模式 time_mode 2:高级模式 advanced_mode 3:low_power_mode -//caiji_mode 0;停止 1:单次 2:连续 3:定标 4:高级模式 5:回传数据 6:低功耗模式 -//atuo_return 0:不自动返回 1:自动返回 -//return_mode 设置返回模式 暂时没用 -//return_data_type 详情见你懂的O.o -//time_interval 时间间隔不能等于0; -//caiji_state 0 :未采集(no) 1:采集完成(finish) 2:采集中(working) -//dingbiao_time 0自动曝光 非0使用这个时长曝光 -volatile u_int32_t work_mode = 0,caiji_mode = 0,return_mode = 1,atuo_return = 0,return_data_type = 0x00,time_interval = 60 * 2,caiji_state = 0 ,db_shutter,db_avn; -volatile float start_time = 9.0,stop_time = 15.30,maximum_temperature = 40,Minimum_temperature = 0,bc_b1,bc_b2,bc_b3,bc_b4; - - - -/// @brief 高级模式参数 -// advanced_direction 0:up 1:down 2:dark -// advanced_shutter_time 曝光时间 -// advanced_collect_times 采集次数 -// advanced_remove_dark 0:不去暗 1:去暗 -volatile u_int32_t advanced_direction=0,advanced_shutter_time=0,advanced_collect_times=0,advanced_remove_dark=0; - -u_int32_t timeOut = 10000; -// SoftwareSerial *wb485port; - -SemaphoreHandle_t xMutexInventory = NULL; //创建信号量Handler - -//json -DynamicJsonDocument doc(1024);//接收串口json -DynamicJsonDocument sys_sd_doc(1024);//读取系统json -DynamicJsonDocument ds18b20_doc(1024);//读取ds18b20 json - - -void json_command(); -void sys_info_init(); - -//////ds1302时间///////// -Ds1302 rtc(0, 8, 7); -Ds1302::DateTime now; -bool sys_set_time(uint32_t year,uint32_t month,uint32_t day); -// const static char* WeekDays[] = -// { -// "Monday", -// "Tuesday", -// "Wednesday", -// "Thursday", -// "Friday", -// "Saturday", -// "Sunday" -// }; - -struct IS11_datastruct *IS11_datastructure; - -//////is11///////// -SensorIS11 *is11Sensor; -STRSensorInfo STRSensorInfos_structure; - -// SoftwareSerial *IS1Serial; -HardwareSerial *IS1Serial; - -//原来 -HardwareSerial wb485Serial(0); - -size_t serialwrite(u_char* data,size_t lenth) -{ - // return IS1Serial->write(data,lenth); - while (IS1Serial->available() > 0) - { - IS1Serial->read(); - - } - - int rec=IS1Serial->write(data,lenth); - - return rec; -} - -size_t serialread(u_char *data, size_t lenth) -{ - lenth=IS1Serial->available(); - - if (lenth<=0 ) - { - /* code */ - return 0; - } - int lenthofread = IS1Serial->readBytes(data,lenth); - - return lenthofread; -} -////////////////////////////////AIR780EG/////////////////////////////////////////// -GSMMannger *gsmmanger; -struct gps_struct{ - uint16_t year; - uint8_t month; - uint8_t day; - uint8_t hour; - uint8_t minute; - uint8_t second; - float latitude; - float longitude; -}; -String get_GPS(gps_struct *gps); -String getnetData(); -HttpClient *http; -//////////////////////////////////////////////////////////////////////////////////// - -// void save_sd_ds18b20_address(uint8_t num,uint8_t *addr); -// void sd_read_set_ds18b20_address(); - -bool read_dingbiao(String dingbiao_path,uint8_t *dianbiao_data_buff); -void send_dingbiao(String dingbiao_path); -void dingbiao_init(String dingbiao_path); - -void rm_dir_or_file(const char * dirname); -void low_power_mode(uint8_t state); -void setup() -{ - //关闭光谱仪 - pinMode(36,INPUT); - vTaskDelay(1000); - - log_init(); - // Serial.begin(115200); - - // wb485Serial.begin(115200); - wb485Serial.begin(115200, SERIAL_8N1, wb485PORT_RX, wb485PORT_TX, false); - - - vTaskDelay(1000); - - vTaskDelay(5000); - IS1Serial =new HardwareSerial(IS1Serial_port); - IS1Serial->begin(115200, SERIAL_8N1, IS1Serial_RX, IS1Serial_TX); - IS1Serial->setTimeout(1); - - - - //初始化SD卡 - sdcard::init_sdcard(); - sdcard::testwriet(); - sdcard::Mkdir("/guangpu_data"); - sdcard::Mkdir("/up"); - sdcard::Mkdir("/down"); - sdcard::Mkdir("/other"); - sdcard::Mkdir("/gps"); - sdcard::Mkdir("/log"); - sdcard::Mkdir("/dingbiao"); - - /// - - // while(1) - // { - // write_log(log_path,"hello world",10); - // if(SD_MMC.exists("/guangpu_data/0")) - // { - // rm_dir_or_file("/guangpu_data/0"); - // } - // } - - dingbiao_init("/dingbiao/dingbiao_up_gain.bin"); - dingbiao_init("/dingbiao/dingbiao_up_offset.bin"); - dingbiao_init("/dingbiao/dingbiao_down_gain.bin"); - dingbiao_init("/dingbiao/dingbiao_down_offset.bin"); - //检查SD卡是否有log.log文件 没有则创建 - // unsigned char log_level = 10; - if(!SD_MMC.exists(log_path)) - { - File file; - file = SD_MMC.open(log_path,"ab+"); - file.flush(); - file.close(); - } - if(!SD_MMC.exists(gp_log)) - { - //检查SD卡是否有log.log文件 没有则创建 - File file; - file = SD_MMC.open(gp_log,"ab+"); - file.flush(); - file.close(); - } - // 初始化DS18b20 - uint8_t temp_number = DS18b20_init(); - // float temp[temp_number]; - // getall_temp(temp); - // while (1) - // { - // getall_temp(temp); - // for (size_t i = 0; i < temp_number; i++) - // { - // write_log(log_path,"temp " + String(i) + ": " + String(temp[i]),10); - // } - // vTaskDelay(1000 * 2); - // } - - // sd_read_set_ds18b20_address(); - - - //初始化舵机 - servo_init(21,0); - - //初始化蜂鸣器 - beep_init(6,2000,12); - // while (1) - // { - // servo_set_angle(120); - // vTaskDelay(1000 * 1.5); - // servo_set_angle(0); - - // beep_run(2000,1000); - // vTaskDelay(1000 * 3); - // beep_stop(); - // } - - - // while(1) - // { - // beep_run(2000,1000); - // vTaskDelay(1000 * 3); - // beep_stop(); - // vTaskDelay(1000 * 3); - // } - - - - //初始化时间 - rtc.init(); - // now.year = 24; - // now.month = 5; - // now.day = 21; - // now.hour = 13; - // now.minute = 45; - // now.second = 0; - // now.dow = Ds1302::DOW_WED; - // rtc.setDateTime(&now); - - - rtc.getDateTime(&now); - - - - //初始化74hc595 - my74hc595_init(); -///////////////////////////加热测试//////////////////////////////////// -// while(1) -// { -// write_log(log_path,"start jiaresi",10); -// jiaresi(0,start_jiare); -// jiaresi(1,start_jiare); -// jiaresi(2,start_jiare); -// jiaresi(3,start_jiare); -// jiaresi(4,start_jiare); -// jiaresi(5,start_jiare); - -// tuigan(tui); -// vTaskDelay(1000 * 15); - -// write_log(log_path,"stop jiaresi",10); -// jiaresi(0,start_jiare); -// jiaresi(1,stop_jiare); -// jiaresi(2,stop_jiare); -// jiaresi(3,stop_jiare); -// jiaresi(4,stop_jiare); -// jiaresi(5,stop_jiare); - -// tuigan(suo); -// vTaskDelay(1000 * 15); - -// } - - -///////////////////////////加热测试//////////////////////////////////// - -///////////////////////////推杆测试///////////////////////////////////// - // uint32_t x = 0; - // float n = 5; - // uint32_t data_length; - // uint8_t read_buf[100]; - // while (1) - // { - // while(wb485Serial.available()) - // { - // data_length += wb485Serial.readBytes(&read_buf[data_length],100); - // for (uint32_t i = 0; i < 5000; i++) - // { - // if(wb485Serial.available() > 0) - // { - // break; - // } - // } - // } - // //如果接收到数据 - // if (data_length > 0) - // { - // if(read_buf[0] == 'n') - // { - // //将转换为数字 - // n = atof((const char *) (read_buf+1)); - // if(n<5) n = 5; - // } - // if(read_buf[0] == 'H') - // { - // wb485Serial.println("n = " + String(n)); - // } - // memset(read_buf, 0, sizeof(read_buf)); - // data_length = 0; - // } - - - // if(x == 1) - // { - // wb485Serial.println("start tuigan tui"); - // tuigan(tui); - // } - // else if(x == 1000 *n) - // { - // wb485Serial.println("start tuigan suo"); - // tuigan(suo); - // } - // else if(x == 1000 * 2 * n) - // { - // x = 0; - // } - - // x++; - // vTaskDelay(1); - // } -///////////////////////////推杆测试///////////////////////////////////// - - - - // //初始化AIR780EG - gsmmanger = new GSMMannger(2, 19, 20); - http = new HttpClient(*gsmmanger->client, "82.156.1.111"); - String Date = getnetData(); - write_log(log_path,"date is :"+Date,10); - - // while(1) - // { - // String Date = getnetData(); - // write_log(log_path,"date is :"+Date,10); - // vTaskDelay(1000); - // } - // //初始化AIR780EG - // gsmmanger = new GSMMannger(); - // //获取GPS - gps_struct gps_structure; - - String gpsbac = get_GPS(&gps_structure); - - //计算日出日落 - double transit, sunrise, sunset; - double utc_offset = 8; - - - // int i=0; - // while(1) - // { - // gpsbac = get_GPS(&gps_structure); - // write_log(log_path,String(i)+"Getting GPS " + gpsbac,10); - // vTaskDelay(1000); - // i++; - // if (gpsbac != "-1") - // { - // calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset); - // write_log(log_path,"sunrise: " + String(sunrise),10); - // write_log(log_path,"sunset: " + String(sunset),10); - // write_log(log_path,"transit: " + String(transit),10); - // write_log(log_path,"latitude: " + String(gps_structure.latitude),10); - // write_log(log_path,"longitude: " + String(gps_structure.longitude),10); - // write_log(log_path,"gps_date: " + gpsbac,10); - // } - // } - - - if (gpsbac != "-1") - { - calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset); - } - else - { - write_log(log_path,"no gps",10); - } - - //开启光谱仪 - pinMode(36,OUTPUT); - digitalWrite(36,LOW); - vTaskDelay(1000 * 2); - InitFunction(serialwrite,serialread); - is11Sensor = new SensorIS11(); - STRSensorInfos_structure = is11Sensor->initSensor(); - - - // while(1) - // { - // is11Sensor->SetShutter(2); - // vTaskDelay(1000); - // is11Sensor->SetShutter(1); - // vTaskDelay(1000); - // } - // 删除原来SD卡的系统信息 - - - sys_info_init(); - - - - - servo_set_angle(120); - vTaskDelay(1000 * 1.5); - servo_set_angle(0); - vTaskDelay(1000 * 1.5); - - beep_run(2000,1000); - vTaskDelay(1000 * 1.5); - beep_stop(); - - // sys_sd_doc["is_spacial"] = "yes"; - - // deserializeJson(sys_sd_doc,system_info); - // while (1) - // { - // } - - xMutexInventory = xSemaphoreCreateMutex(); //创建MUTEX - if (xMutexInventory == NULL) { - - } else{ - //创建并启动Core1的任务 xTaskCreatePinnedToCore - xTaskCreatePinnedToCore( - Task0, /* 任务函数 */ - "Task0", /* 任务名称 */ - 1024 * 50, /* 栈大小 */ - NULL, /* 任务参数 */ - 2, /* 任务优先级 */ - &Task0_Handler, /* 任务句柄 */ - 0); /* Core 0 */ - - xTaskCreatePinnedToCore( - Task1, /* 任务函数 */ - "Task1", /* 任务名称 */ - 1024 * 5, /* 栈大小 */ - NULL, /* 任务参数 */ - 1, /* 任务优先级 */ - &Task1_Handler, /* 任务句柄 */ - 0); - - //创建并启动Core0的任务 - xTaskCreatePinnedToCore( - Task2, /* 任务函数 */ - "Task2", /* 任务名称 */ - 1024 * 30, /* 栈大小 */ - NULL, /* 任务参数 */ - 20, /* 任务优先级 */ - &Task2_Handler, /* 任务句柄 */ - 1); - } - // write_log(log_path,"setup ok",log_level); - // beep_run(2000,512); - // vTaskDelay(1000 * 3); - // beep_stop(); -} - -void loop() -{ - - -} -void Task0(void *pvParameters) -{ - vTaskDelay(1); - write_log(log_path,"task 0",10); - uint32_t uint_work_task0 = 100; - while (1) - { - if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - { - if(uint_work_task0 == 100 && caiji_mode != 2) - { - caiji_mode = 0; - uint_work_task0 = 0; - } - if (caiji_state == 2) - { - caiji_state = 1; - } - uint_work_task0 = caiji_mode; - } - xSemaphoreGive(xMutexInventory); - //caiji_mode 0停止 1单次 2连续 3定标 4定标 5回传数据 - switch (uint_work_task0) - { - case 0: - - break; - case 1: - if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - { - caiji_state = 2; - } - xSemaphoreGive(xMutexInventory); - is11Sensor->TakeOneJob(false); - if (atuo_return == 1) - { - is11Sensor->senddata(&wb485Serial,return_data_type); - } - uint_work_task0 = 100; - break; - case 2: - if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - { - caiji_state = 2; - } - xSemaphoreGive(xMutexInventory); - is11Sensor->TakeOneJob(false); - if (atuo_return == 1) - { - is11Sensor->senddata(&wb485Serial,return_data_type); - } - vTaskDelay(1000 * time_interval); - break; - - case 3: - is11Sensor->senddata(&wb485Serial,return_data_type); - - uint_work_task0 = 100; - break; - - case 4: - if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - { - caiji_state = 2; - } - xSemaphoreGive(xMutexInventory); - is11Sensor->advanced_mode(advanced_direction,advanced_shutter_time,advanced_collect_times,advanced_remove_dark); - uint_work_task0 = 100; - break; - - case 5: - if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - { - caiji_state = 2; - } - xSemaphoreGive(xMutexInventory); - is11Sensor->opt(advanced_direction); - uint_work_task0 = 100; - break; - - default: - break; - } - vTaskDelay(10); - } -} - - -//Task1的实现 -void Task1(void *pvParameters) -{ - vTaskDelay(10); - write_log(log_path,"task 1",10); - - while(1) - { - // vTaskDelay(1000); - // //获取时间 - // rtc.getDateTime(&now); - - // //获取温度 - // // float temp[8]; - // // getall_temp(temp); - // // //平均温度 - // // float temp_ave = 0; - // // for (size_t i = 0; i < 8; i++) - // // { - // // temp_ave += temp[i]; - // // } - // // temp_ave = temp_ave / 8; - // // write_log(log_path,"temp_ave: " + String(temp_ave),log_level); - - // //获取GPS - // gps_struct gps_structure; - - // String gpsbac = get_GPS(&gps_structure); - // //计算日出日落 - // double transit, sunrise, sunset; - // double utc_offset = 8; - // if (gpsbac != "-1") - // { - // calcSunriseSunset(now.year+2000, now.month, now.day,gps_structure.latitude, gps_structure.longitude, transit, sunrise, sunset); - // } - // else - // { - // write_log(log_path,"no gps",10); - // } - - // //日出日落控制 太阳升起前推杆缩,太阳落下后推杆伸 - // write_log(log_path,"now.hour: " + String(now.hour),log_level); - // write_log(log_path,"sunrise: " + String((int)sunrise),log_level); - // write_log(log_path,"sunset: " + String((int)sunset),log_level); - // if (now.hour+1 == (int)sunrise) - // { - // tuigan(suo); - // } - // else if (now.hour+1 >= (int)sunset) - // { - // tuigan(tui); - // } - - // if(sys_sd_doc["work_mode"] == "time_mode") - // { - // float now_time = now.hour + now.minute/100; - // if(now_time > start_time && now_time < stop_time) - // { - - // } - // else - // { - // if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - // { - // caiji_mode = 0; - // } - // xSemaphoreGive(xMutexInventory); - // } - // } - - // write_log(log_path,"temp_ave: " + String(temp_ave),log_level); - // if(temp_ave < Minimum_temperature) - // { - // write_log(log_path,"start jiaresi",10); - // jiaresi(5,start_jiare); - // vTaskDelay(1000 * 10); - // } - // else if (temp_ave > Minimum_temperature + 5) - // { - // write_log(log_path,"stop jiaresi",10); - // jiaresi(5,stop_jiare); - // } - // for(int ab ; ab < 10; ab++) - // { - - rtc.getDateTime(&now); - // String path = "/guangpu_data/"+ String(now.year) + "/" + String(now.month) + "/" + String(now.day); - // if(!SD_MMC.exists(log_path)) - - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - double cardSize = SD_MMC.cardSize() / (1024 * 1024); - - double used_size = SD_MMC.usedBytes() / (1024 * 1024); - - float used_percent = used_size / cardSize; - write_log(log_path,"used_percent: " + String(used_percent),10); - //数据清除规则:1、满十年清除第一年数据 2、当数据>90%时删除最前一个月数据 - if(used_percent >= 0.8) - { - if (used_percent >= 0.9) - { - //删除文件 - String path_b = "/gaugpu_data"; - for(int i=24;i<255 ;i++) - { - for (uint8_t j = 1; j < 13; i++) - { - String path_a = path_b + "/" +String(i)+ "/" +String(j); - if(SD_MMC.exists(path_a)) - { - rm_dir_or_file(path_a.c_str()); - } - } - } - } - //报警 - write_log(log_path,"sdcard will full," + String(used_percent * 100) + "%" +"used.",10); - } - - if(SD_MMC.exists("/guangpu_data/"+ String(now.year - 10) )) - { - String path = "/guangpu_data/"+ String(now.year - 10); - rm_dir_or_file(path.c_str()); - } - - - // write_log(log_path,"delete /guangpu_data/0 1111111111",10); - - // if(SD_MMC.exists("/guangpu_data/0")) - // { - // write_log(log_path,"delete /guangpu_data/0 2222222222222222",10); - // // sdcard::deleteFolderOrFile("/gaugpu_data/0"); - // rm_dir_or_file("/guangpu_data/0"); - // } - /////////////////////////////////////////////////////////////////////////////////////////// - - //////////////////////////////////////////////时间//////////////////////////////////////////////////////////// - //获取RTC时间 - rtc.getDateTime(&now); - - // 获取网络时间 - gps_struct net_structure; - String net_date = getnetData(); - write_log(log_path,"net_date is :"+net_date,10); - // 2024-07-12 07:12:45 - - //截取时间 - if(net_date != "-1") - { - net_structure.year = net_date.substring(0,4).toInt() - 2000; - net_structure.month = net_date.substring(5,7).toInt(); - net_structure.day = net_date.substring(8,10).toInt(); - net_structure.hour = net_date.substring(11,13).toInt(); - net_structure.minute = net_date.substring(14,16).toInt(); - net_structure.second = net_date.substring(17,-1).toInt(); - - if(now.year != net_structure.year || now.month != net_structure.month || now.day != net_structure.day || now.hour - net_structure.hour || (fabs(now.minute - net_structure.minute)>5 )) - { - now.year = net_structure.year; - now.month = net_structure.month; - now.day = net_structure.day; - now.hour = net_structure.hour; - now.minute = net_structure.minute; - now.second = net_structure.second; - rtc.setDateTime(&now); - } - } - ////获取GPS - gps_struct gps_structure; - String gpsbac = get_GPS(&gps_structure); - - if(net_date != "-1" && gpsbac != "-1") - { - gps_structure.year = gps_structure.year - 2000; - if(now.year != gps_structure.year || now.month != gps_structure.month || now.day != gps_structure.day || now.hour != gps_structure.hour || (fabs(now.minute - net_structure.minute)>5 )) - { - now.year = gps_structure.year; - now.month = gps_structure.month; - now.day = gps_structure.day; - now.hour = gps_structure.hour; - now.minute = gps_structure.minute; - now.second = gps_structure.second; - rtc.setDateTime(&now); - }else write_log(log_path,"no gps",10); - } - ////////////////////////////////////////////////////////////////////////////////////////////////////////// - - - //获取时间 - rtc.getDateTime(&now); - if(sys_sd_doc["work_mode"] == "time_mode") - { - float now_time = now.hour + now.minute/100; - if(now_time > start_time && now_time < stop_time) - { - - } - else - { - if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - { - caiji_mode = 0; - } - xSemaphoreGive(xMutexInventory); - } - } - - - - // sdcard::Mkdir(path); - - float temp4 = getone_temp(4); - float temp6 = getone_temp(6); - write_log(log_path,"temp4 : " + String(temp4),10); - write_log(log_path,"temp6 : " + String(temp6),10); - - if((temp4+temp6)/2 < 0) - { - write_log(log_path,"start jiaresi",10); - jiaresi(5,start_jiare); - vTaskDelay(1000 * 10); - } - else if ((temp4+temp6)/2 > 5) - { - write_log(log_path,"stop jiaresi",10); - jiaresi(5,stop_jiare); - } - vTaskDelay(1000 * 10); - } -} - -// Task2的实现 -void Task2(void *pvParameters) -{ - vTaskDelay(20); - write_log(log_path,"task 2",10); - unsigned char read_buf[1024*10]; - unsigned char command_data[1024 * 10]; - unsigned char data_type; - String send_data ; - int send_lenth; - u_char send_buff[300]; - - while(1) - { - unsigned short data_length = 0; - //监测接收串口数据 - while(wb485Serial.available()) - { - data_length += wb485Serial.readBytes(&read_buf[data_length],1024*10); - for (uint32_t i = 0; i < 5000; i++) - { - if(wb485Serial.available() > 0) - { - break; - } - } - } - //如果接收到数据 - if (data_length > 0) - { - // 测试数据 - write_log(log_path,"data_length: " + String(data_length),log_level); - - data_length = IRIS_Cut_Befor_Header(read_buf,data_length); - if (data_length < 0) - { - send_data = "send data error"; - write_log(log_path,send_data,log_level); - send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff); - wb485Serial.write(send_buff,send_lenth); - // return; - } - int ret = IRIS_STM32_Protocol_Unpack(read_buf,data_length,&data_type,command_data); - write_log(log_path,"ret:" +String(ret),log_level); - if (ret > 0) - { - write_log(log_path,"data_type:" +String(data_type),log_level); - if(data_type == 0x00)//json - { - deserializeJson(doc,command_data); - write_log(log_path,"start json_command",log_level); - json_command(); - doc.clear(); - } - else if (data_type == 0x01)//String - { - // write_log(command_data,0); - } - else if (data_type == 0x03) //hex - { - write_log(log_path,"",log_level); - String dingbiao_path; - if (command_data[0] == 4 || command_data[0] == 5) - { - // is11Sensor->save_dingbiao(command_data); - //保存到sd卡 - if(command_data[0] == 4) - { - if(command_data[1] == 0) dingbiao_path = "/dingbiao/dingbiao_up_gain.bin"; - else if(command_data[1] == 1) dingbiao_path = "/dingbiao/dingbiao_up_offset.bin"; - } - else if(command_data[0] == 5) - { - if(command_data[1] == 0) dingbiao_path = "/dingbiao/dingbiao_down_gain.bin"; - else if(command_data[1] == 1) dingbiao_path = "/dingbiao/dingbiao_down_offset.bin"; - } - is11Sensor->save_dingbiao(command_data); - File file = SD_MMC.open(dingbiao_path, "wb"); - file.write((uint8_t *)&command_data, ret); - file.flush(); - file.close(); - } - } - else if(data_type == 0x04) - { - uint8_t num = command_data[0]; - set_ds18b20_address(num,command_data+1); - } - - send_data = "wb485 success"; - write_log(log_path,send_data,log_level); - // send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff); - // wb485Serial.write(send_buff,send_lenth); - } - else - { - send_data = "send data error"; - write_log(log_path,send_data,log_level); - send_lenth = IRIS_Protocol_Pack(0x01,(uint16_t)send_data.length(), (uint8_t *)send_data.c_str(),send_buff); - wb485Serial.write(send_buff,send_lenth); - } - - memset(read_buf, 0, sizeof(read_buf)); - memset(command_data, 0, sizeof(command_data)); - } - // vTaskDelay(1); - } -} -/* -//处理json命令 -command : 1、get_sensor_info - 2、set_sensor_info - 3、set_work_mode - 4、get_work_mode - 5、set_time_interval - 6、start_work - 7、stop_work - 8、set_return_mode - 9、set_atuo_return - 10、set_return_data_type - 11、get_9 - 12、 - 13、get_data - 14、set_bochangxishu - 15、get_bochangxishu - 16、get_chongcaiyangbochang - - 18、get_darkcurrent - 19、set_servo_offset - 20、set_start_time - 21、set_stop_time - 22、set_maximum_temperature - 23、set_Minimum_temperature - 24、 -*/ - -/*{"command":"","name":"gaoguangpu","version":"v2.0","SN":"123456","date":"","caiji_mode":"","time_interval":"","return_data_type":"","bochangxishu":""}*/ -void json_command() -{ - int ret; - bool save = false; - String send_str; - u_char send_buff[1000]; - - if(xSemaphoreTake(xMutexInventory, timeOut) == pdPASS) - { - //command 1 - if (doc["command"] == "get_sensor_info") - { - rtc.getDateTime(&now); - sys_sd_doc["date"] = String(now.year+2000) + "-" + String(now.month) + "-" + String(now.day) + " " + String(now.hour) + ":" + String(now.minute) + ":" + String(now.second); - send_str = ""; - serializeJson(sys_sd_doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - serializeJson(sys_sd_doc, send_str); - wb485Serial.write(send_buff, ret); - } - //command 2 - else if (doc["command"] == "set_sensor_info") - { - write_log(log_path,"set_sensor_info",log_level); - if(doc["name"] == NULL) - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "Name cannot be null"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - save = true ; - sys_sd_doc["name"] = doc["name"].as(); - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - - if(doc["version"] == NULL) - { - - } - else - { - save = true ; - sys_sd_doc["version"] = doc["version"].as(); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 3 - else if (doc["command"] == "set_work_mode") - { - - if(doc["work_mode"] == "manual_mode") - { - if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1); - save = true ; - sys_sd_doc["work_mode"] = "manual_mode"; - sys_sd_doc["caiji_mode"] = "single"; - sys_sd_doc["atuo_return"] ="no"; - sys_sd_doc["return_data_type"] = 0x00; - work_mode = 0; - caiji_mode = 0; - atuo_return = 0; - return_data_type = 0x00; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else if (doc["work_mode"] == "time_mode") - { - if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1); - save = true; - sys_sd_doc["work_mode"] = "time_mode"; - sys_sd_doc["caiji_mode"] = "continue"; - sys_sd_doc["atuo_return"] = "yes"; - sys_sd_doc["return_data_type"] = 0x00; - work_mode = 1; - caiji_mode = 0; - atuo_return = 1; - return_data_type = 0x00; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else if(doc["work_mode"] == "advanced_mode") - { - if(sys_sd_doc["work_mode"]=="low_power_mode") low_power_mode(1); - - save = true; - sys_sd_doc["work_mode"] = "advanced_mode"; - sys_sd_doc["caiji_mode"] = "advanced"; - sys_sd_doc["atuo_return"] = "no"; - sys_sd_doc["return_data_type"] = 0x00; - - work_mode = 2; - caiji_mode = 0; - atuo_return = 0; - return_data_type = 0x00; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else if(doc["work_mode"] == "low_power_mode") - { - save = true; - sys_sd_doc["work_mode"] = "low_power_mode"; - sys_sd_doc["caiji_mode"] = "low_power"; - sys_sd_doc["atuo_return"] = "no"; - sys_sd_doc["return_data_type"] = 0x00; - - work_mode = 3; - - low_power_mode(0); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "work mode error"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 4.1 - else if (doc["command"] == "set_caiji_mode") - { - if(doc["caiji_mode"] == "start") - { - if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 2; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else if(doc["caiji_mode"] == "stop") - { - if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 0; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else if(doc["caiji_mode"] == "single") - { - save = true; - sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); - caiji_mode = 1; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else if(doc["caiji_mode"] == "continue") - { - save = true; - sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); - caiji_mode = 2; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "work mode error"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 4.2 - else if (doc["command"] == "take_one_job") - { - - - if(caiji_mode == 0) - { - caiji_mode = 1; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "Working"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 4.3 - else if (doc["command"] == "set_time_interval") - { - if (doc["time_interval"]>0) - { - save = true; - sys_sd_doc["time_interval"] = doc["time_interval"]; - time_interval = doc["time_interval"]; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "time_interval error"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 5 - else if (doc["command"] == "get_work_mode") - { - doc.clear(); - send_str = ""; - doc["work_mode"] = sys_sd_doc["work_mode"].as(); - doc["caiji_mode"] = sys_sd_doc["caiji_mode"].as(); - doc["atuo_return"] = sys_sd_doc["atuo_return"].as(); - doc["return_data_type"] =sys_sd_doc["return_data_type"]; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - //command 6 - else if(doc["command"] == "set_atuo_return") - { - if(doc["atuo_return"] == "yes") - { - save = true; - sys_sd_doc["atuo_return"] = doc["atuo_return"].as(); - atuo_return = 1; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else if (doc["atuo_return"] == "no") - { - save = true; - sys_sd_doc["atuo_return"] = doc["atuo_return"].as(); - atuo_return = 0; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "atuo_return error"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 7 - else if (doc["command"] == "set_return_data_type") - { - u_int8_t a = 0; - u_int32_t b = doc["return_data_type"]; - switch (b) - { - case 0x00: - a=1; - break; - case 0x01: - a=1; - break; - case 0x02: - a=1; - break; - case 0x03: - a=1; - break; - case 0x04: - a=1; - break; - case 0x05: - a=1; - break; - case 0x10: - a=1; - break; - case 0x11: - a=1; - break; - case 0x12: - a=1; - break; - case 0x13: - a=1; - break; - default: - break; - } - if(a) - { - save = true; - sys_sd_doc["return_data_type"] = doc["return_data_type"]; - return_data_type = doc["return_data_type"]; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "return_data_type error"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 8 - //保留,没用 - else if (doc["command"] == "set_return_mode") - { - save = true; - sys_sd_doc["return_mode"] = doc["return_mode"].as(); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - /*设置回传模式 */ - } - //command 9 - else if (doc["command"] == "get_data") - { - u_int8_t a = 0; - u_int32_t b = doc["return_data_type"]; - switch (b) - { - case 0x00: - a=1; - break; - case 0x01: - a=1; - break; - case 0x02: - a=1; - break; - case 0x03: - a=1; - break; - case 0x04: - a=1; - break; - case 0x05: - a=1; - break; - case 0x06: - a=1; - break; - case 0x10: - a=1; - break; - case 0x11: - a=1; - break; - case 0x12: - a=1; - break; - case 0x13: - a=1; - break; - default: - break; - } - if(a && caiji_mode !=3) - { - return_data_type = doc["return_data_type"]; - caiji_mode = 3; - } - else - { - send_str = ""; - doc["caiji_state"] = "working"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - /* code */ - } - //command 10 - else if (doc["command"] == "set_bochangxishu") - { - float b1=0,b2=0,b3=0,b4=0; - b1 = doc["bochangxishu"]["b0"].as(); - b2 = doc["bochangxishu"]["b1"].as(); - b3 = doc["bochangxishu"]["b2"].as(); - b4 = doc["bochangxishu"]["b3"].as(); - - if(b1!=0 && b2 !=0 && b3 != 0 && b4 != 0) - { - save = true; - bc_b1 = doc["bochangxishu"]["b0"].as(); - bc_b2 = doc["bochangxishu"]["b1"].as(); - bc_b3 = doc["bochangxishu"]["b2"].as(); - bc_b4 = doc["bochangxishu"]["b3"].as(); - - sys_sd_doc["bochangxishu"]["b0"] = doc["bochangxishu"]["b0"].as(); - sys_sd_doc["bochangxishu"]["b1"] = doc["bochangxishu"]["b1"].as(); - sys_sd_doc["bochangxishu"]["b2"] = doc["bochangxishu"]["b2"].as(); - sys_sd_doc["bochangxishu"]["b3"] = doc["bochangxishu"]["b3"].as(); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "bochangxishu error or null"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - - //command 11 - else if (doc["command"] == "get_bochangxishu") - { - doc.clear(); - doc["bochangxishu"]["a0"] = sys_sd_doc["bochangxishu"]["a0"].as(); - doc["bochangxishu"]["a1"] = sys_sd_doc["bochangxishu"]["a1"].as(); - doc["bochangxishu"]["a2"] = sys_sd_doc["bochangxishu"]["a2"].as(); - doc["bochangxishu"]["a3"] = sys_sd_doc["bochangxishu"]["a3"].as(); - - doc["bochangxishu"]["b0"] = sys_sd_doc["bochangxishu"]["b0"].as(); - doc["bochangxishu"]["b1"] = sys_sd_doc["bochangxishu"]["b1"].as(); - doc["bochangxishu"]["b2"] = sys_sd_doc["bochangxishu"]["b2"].as(); - doc["bochangxishu"]["b3"] = sys_sd_doc["bochangxishu"]["b3"].as(); - - send_str = ""; - doc["state"] = "ok"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - - //command 12 - else if (doc["command"] == "get_chongcaiyangbochang") - { - // String chongcaiyangbochang; - /* code */ - } - - //command 14 - else if(doc["command"] == "set_servo_offset") - { - float angle = doc["servo_offset_angle"]; - if(angle > 0 || angle < 180) - { - save = true; - sys_sd_doc["servo_offset_angle"] = doc["servo_offset_angle"].as(); - servo_offset(angle); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "servo_offset_angle error or out of range"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - - //command 15 - else if(doc["command"] == "set_start_time") - { - float t = doc["start_time"].as(); - if(t>0 && t < 24) - { - save = true; - sys_sd_doc["start_time"] = doc["start_time"].as(); - start_time = doc["start_time"].as(); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "start_time error or out of range"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 16 - else if(doc["command"] == "set_stop_time") - { - float tt = doc["stop_time"].as(); - if(tt>0 && tt < 24 && tt > start_time) - { - save = true; - sys_sd_doc["stop_time"] = doc["stop_time"].as(); - stop_time = doc["stop_time"]; - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "stop_time error or out of range"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 17 - else if(doc["command"] == "set_maximum_temperature") - { - float te = doc["maximum_temperature"].as(); - if(te < 100) - { - save = true; - maximum_temperature = doc["maximum_temperature"].as(); - sys_sd_doc["maximum_temperature"] = doc["maximum_temperature"].as(); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "maximum_temperature error or out of range"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - - //command 18 - else if(doc["command"] == "set_Minimum_temperature") - { - float tte = doc["Minimum_temperature"].as(); - if(tte < maximum_temperature) - { - save = true; - maximum_temperature = doc["Minimum_temperature"].as(); - sys_sd_doc["Minimum_temperature"] = doc["Minimum_temperature"].as(); - - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "Minimum_temperature error or out of range"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 19 - else if(doc["command"] == "get_date") - { - send_str = ""; - doc.clear(); - rtc.getDateTime(&now); - doc["state"] = "ok"; - doc["date"]["year"]= now.year; - doc["date"]["month"]= now.month; - doc["date"]["day"]= now.day; - doc["date"]["hour"]= now.hour; - doc["date"]["minute"]= now.minute; - doc["date"]["second"]= now.second; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - //command 20 - else if(doc["command"] == "get_caiji_state") - { - uint32_t percent; - send_str = ""; - doc.clear(); - if(caiji_state == 1) - { - doc["caiji_state"] = "finish"; - percent = is11Sensor->work_progress; - doc["percent"] = percent; - doc["info"] = "end"; - } - else if(caiji_state == 2 ) - { - doc["caiji_state"] = "working"; - percent = is11Sensor->work_progress; - doc["percent"] = percent; - if(percent < 100) - { - doc["info"] = "working"; - } - else - { - doc["info"] = "end"; - } - } - else doc["caiji_state"] = "no"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - //command 21 - else if(doc["command"] == "start_opt") - { - if(sys_sd_doc["work_mode"] == "advanced_mode") - { - caiji_mode = 5; - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "The working mode is not advanced_mode"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 22 - else if(doc["command"] == "get_opt") - { - if(sys_sd_doc["work_mode"] == "advanced_mode") - { - send_str = ""; - uint32_t aa = is11Sensor->dingbiao_shutter; - doc["opt"] = aa; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "The working mode is not advanced_mode"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - else if(doc["command"] == "start_collect") - { - bool collect = true; - if(sys_sd_doc["work_mode"] == "advanced_mode") - { - // caiji_mode = 3; - if (doc["direction"] == "up") - { - advanced_direction = 0; - } - else if (doc["direction"] == "down") - { - advanced_direction = 1; - } - else if (doc["direction"] == "dark") - { - advanced_direction = 2; - } - else - { - collect = false; - // send_str = ""; - // doc["state"] = "failed"; - // doc["info"] = "direction error"; - // serializeJson(doc, send_str); - // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - // wb485Serial.write(send_buff, ret); - } - - if(doc["shutter_time"]>=1) - { - advanced_shutter_time = doc["shutter_time"]; - } - else - { - collect = false; - // send_str = ""; - // doc["state"] = "failed"; - // doc["info"] = "shutter_time error"; - // serializeJson(doc, send_str); - // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - // wb485Serial.write(send_buff, ret); - } - - if(doc["collect_times"]>0) - { - advanced_collect_times = doc["collect_times"]; - } - else - { - collect = false; - // send_str = ""; - // doc["state"] = "failed"; - // doc["info"] = "collect_times error"; - // serializeJson(doc, send_str); - // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - // wb485Serial.write(send_buff, ret); - } - if(doc["remove_dark"] == "yes") - { - advanced_remove_dark = 1; - } - else if(doc["remove_dark"] == "no") - { - advanced_remove_dark = 0; - } - else - { - collect = false; - // send_str = ""; - // doc["state"] = "failed"; - // doc["info"] = "remove_dark error"; - // serializeJson(doc, send_str); - // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - // wb485Serial.write(send_buff, ret); - } - - if (collect) - { - caiji_mode = 4; - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "parameter error"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "The working mode is not advanced_mode"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - //command 25 - else if(doc["command"] == "set_sys_time") - { - uint32_t year,month,day,hour,minute,second; - - year = doc["year"]; - month = doc["month"]; - day = doc["day"]; - hour = doc["hour"]; - minute = doc["minute"]; - second = doc["second"]; - write_log(log_path,"year" + String(year) + "month" + String(month) + "day" + String(day) + "hour" + String(hour) + "minute" + String(minute) + "second" + String(second),10); - if(year>99 || month>12 || day>31 || hour > 23 || minute >59 || second > 59) - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "date error1"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - if(sys_set_time(year,month,day)) - { - now.year = year; - now.month = month; - now.day = day; - now.hour = hour; - now.minute =minute; - now.second =second; - // now.dow = 5; - rtc.setDateTime(&now); - send_str = ""; - doc["state"] = "success"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "date error2"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - } - } - //command 26 - // else if(doc["command"] == "set_ds18b20_address") - // { - // uint8_t num = doc["num"]; - // uint8_t addr[8]; - // if (num>=0 && num<8) - // { - // addr[0] = doc["addr"]["0"]; - // addr[1] = doc["addr"]["1"]; - // addr[2] = doc["addr"]["2"]; - // addr[3] = doc["addr"]["3"]; - // addr[4] = doc["addr"]["4"]; - // addr[5] = doc["addr"]["5"]; - // addr[6] = doc["addr"]["6"]; - // addr[7] = doc["addr"]["7"]; - - // set_ds18b20_address(num, addr); - // save_sd_ds18b20_address(num,addr); - // send_str = ""; - // doc["state"] = "success"; - // serializeJson(doc, send_str); - // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - // wb485Serial.write(send_buff, ret); - // } - // else - // { - // send_str = ""; - // doc["state"] = "failed"; - // doc["info"] = "num error"; - // serializeJson(doc, send_str); - // ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - // wb485Serial.write(send_buff, ret); - // } - // } - else if(doc["command"] == "get_dingbiao") - { - send_dingbiao("/dingbiao/dingbiao_up_gain.bin"); - send_dingbiao("/dingbiao/dingbiao_up_offset.bin"); - send_dingbiao("/dingbiao/dingbiao_down_gain.bin"); - send_dingbiao("/dingbiao/dingbiao_down_offset.bin"); - } - //command error - else - { - send_str = ""; - doc["state"] = "failed"; - doc["info"] = "no command"; - serializeJson(doc, send_str); - ret = IRIS_Protocol_Pack(0x00,(uint16_t)send_str.length(), (uint8_t *)send_str.c_str(),send_buff); - wb485Serial.write(send_buff, ret); - } - //将修改后的系统信息写入内存卡 - if(save == true) - { - send_str = ""; - serializeJson(sys_sd_doc, send_str); - - File file; - file = SD_MMC.open("/system_info.json","w+"); - file.println(send_str); - file.flush(); - file.close(); - } - } - xSemaphoreGive(xMutexInventory); -} - - -String get_GPS(gps_struct *gps) -{ - write_log(log_path,"start get GPS ",10); - String gpsbac,Date,temp,Latitude,Longitude; - gsmmanger->modem->sendAT(GF("+CGNSPWR?")); - gsmmanger->modem->waitResponse("OK"); - - gsmmanger->modem->sendAT(GF("+CGNSPWR=1")); - gsmmanger->modem->waitResponse("OK"); - - gsmmanger->modem->sendAT(GF("+CGNSAID=31,1,1,1")); - gsmmanger->modem->waitResponse("OK"); - - gsmmanger->modem->sendAT(GF("+CGNSINF")); - - //获取gps信息 - gsmmanger->modem->waitResponse(5000,gpsbac); - write_log(log_path,"gpsbac is " + gpsbac,10); - - //检测是否有效 - temp = gpsbac.substring(16,17); - // write_log(log_path,"hahaha :" + temp,10); - - if(temp != "1") - { - // write_log(log_path,"gps error,hahahaha",10); - return "-1"; - } - //截取时间 - Date = gpsbac.substring(18,32); - temp = Date.substring(0,4) + "-" + Date.substring(4,6) + "-" +Date.substring(6,8) + " " + Date.substring(8,10) + ":" +Date.substring(10,12) + ":" +Date.substring(12,-1); - - gps->year = Date.substring(0,4).toInt(); - gps->month = Date.substring(4,6).toInt(); - gps->day = Date.substring(6,8).toInt(); - gps->hour = Date.substring(8,10).toInt(); - gps->minute = Date.substring(10,12).toInt(); - gps->second = Date.substring(12,-1).toInt(); - - Date = temp; -// +CGNSINF: 1,1,20240430052525,40.040938,116.328013,51.900,0.26,0.00,3,,2.14,2.42,4.00,,10,8,,,34,, - -// OK -////////获取经纬度////////// - temp = gpsbac.substring(33); - // write_log(log_path,"Longitude is :" + temp,10); - int pos = temp.indexOf(","); - Latitude = temp.substring(0,pos); - gps->latitude = Latitude.toFloat(); - // write_log(log_path,"Latitude ::::" + Latitude,10); - - temp = temp.substring(pos+1); - // write_log(log_path,"Longitude is :" + temp,10); - pos = temp.indexOf(","); - Longitude = temp.substring(0,pos); - gps->longitude = Longitude.toFloat(); - // write_log(log_path,"Longitude ::::" + Longitude,10); - - if(Date.indexOf(",") != -1) - { - return "-1"; - } - - return Date; -} - - - -void sys_info_init() -{ - gps_struct gps_structure; - String system_info; - String rtcdate_now ="20" + String(now.year) + "-" + String(now.month) + "-" + String(now.day) + " " + String(now.hour) + ":" + String(now.minute) + ":" + String(now.second); - write_log(log_path,rtcdate_now,10); - if(SD_MMC.exists("/system_info.json")) - { - write_log(log_path,"has system_info.json file",log_level); - File file; - file = SD_MMC.open("/system_info.json","rb+"); - system_info = file.readString(); - file.flush(); - file.close(); - - deserializeJson(doc, system_info); - //初始化系统信息 - sys_sd_doc["name"] = doc["name"].as(); - sys_sd_doc["serialnumber"] = STRSensorInfos_structure.serialnumber; - // sys_sd_doc["serialnumber"] = "is110008"; - sys_sd_doc["version"] = doc["version"].as(); - sys_sd_doc["date"] = rtcdate_now; - - sys_sd_doc["work_mode"] = doc["work_mode"].as(); //一会 - if(doc["work_mode"] == "manual_mode") - { - work_mode = 0; - } - else if (doc["work_mode"] == "time_mode") - { - work_mode = 1; - } - else - { - work_mode = 0; - } - - // if(caiji_mode == 7) - // { - // sys_sd_doc["work_mode"] = "specify"; - // } - - - sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as();//一会 - if(doc["caiji_mode"] == "start") - { - if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 2; - } - else if(doc["caiji_mode"] == "stop") - { - if(sys_sd_doc["caiji_mode"]=="continue") caiji_mode = 0; - } - else if(doc["caiji_mode"] == "single") - { - sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); - caiji_mode = 1; - } - else if(doc["caiji_mode"] == "continue") - { - sys_sd_doc["caiji_mode"] = doc["caiji_mode"].as(); - caiji_mode = 2; - } - else - { - - } - - sys_sd_doc["time_interval"] = doc["time_interval"]; - time_interval = doc["time_interval"].as(); - sys_sd_doc["start_time"] = doc["start_time"].as(); - start_time = doc["start_time"].as(); - sys_sd_doc["stop_time"] = doc["stop_time"].as(); - stop_time = doc["stop_time"].as(); - sys_sd_doc["maximum_temperature"] = doc["maximum_temperature"].as(); - maximum_temperature = doc["maximum_temperature"].as(); - sys_sd_doc["Minimum_temperature"] = doc["Minimum_temperature"].as(); - Minimum_temperature = doc["Minimum_temperature"].as(); - sys_sd_doc["servo_offset_angle"] = doc["servo_offset_angle"].as(); - float a = doc["servo_offset_angle"].as(); - servo_offset(a); - sys_sd_doc["bochangxishu"]["a0"] = STRSensorInfos_structure.a1; - sys_sd_doc["bochangxishu"]["a1"] = STRSensorInfos_structure.a2; - sys_sd_doc["bochangxishu"]["a2"] = STRSensorInfos_structure.a3; - sys_sd_doc["bochangxishu"]["a3"] = STRSensorInfos_structure.a4; - - sys_sd_doc["bochangxishu"]["b0"] = doc["bochangxishu"]["b0"].as(); - sys_sd_doc["bochangxishu"]["b1"] = doc["bochangxishu"]["b1"].as(); - sys_sd_doc["bochangxishu"]["b2"] = doc["bochangxishu"]["b2"].as(); - sys_sd_doc["bochangxishu"]["b3"] = doc["bochangxishu"]["b3"].as(); - sys_sd_doc["return_data_type"] = doc["return_data_type"].as(); - sys_sd_doc["return_mode"] = doc["return_mode"]; //暂定 - sys_sd_doc["atuo_return"] = doc["atuo_return"].as();//一会 - if(doc["atuo_return"] == "yes") - { - atuo_return = 1; - } - else - { - atuo_return = 0; - } - sys_sd_doc["gps_info"]["latitude"] = gps_structure.latitude; - sys_sd_doc["gps_info"]["longitude"] = gps_structure.longitude; - - system_info = ""; - serializeJson(sys_sd_doc, system_info); - write_log(log_path, system_info,log_level); - - doc.clear(); - } - else{ - write_log(log_path,"do not has system_info.json file" ,log_level); - - sys_sd_doc["name"] = "HS"; - // sys_sd_doc["serialnumber"] = "is110008"; - sys_sd_doc["serialnumber"] = STRSensorInfos_structure.serialnumber; - sys_sd_doc["version"] = "v2.0"; - sys_sd_doc["date"] = rtcdate_now; - sys_sd_doc["work_mode"] = "manual_mode"; - sys_sd_doc["caiji_mode"] = "stop"; - sys_sd_doc["time_interval"] = time_interval; - sys_sd_doc["start_time"] = start_time; - sys_sd_doc["stop_time"] = stop_time; - sys_sd_doc["maximum_temperature"] = maximum_temperature; - sys_sd_doc["Minimum_temperature"] = Minimum_temperature; - sys_sd_doc["servo_offset_angle"] = 0; - - sys_sd_doc["bochangxishu"]["a0"] = STRSensorInfos_structure.a1; - sys_sd_doc["bochangxishu"]["a1"] = STRSensorInfos_structure.a2; - sys_sd_doc["bochangxishu"]["a2"] = STRSensorInfos_structure.a3; - sys_sd_doc["bochangxishu"]["a3"] = STRSensorInfos_structure.a4; - - sys_sd_doc["bochangxishu"]["b0"] = bc_b1; - sys_sd_doc["bochangxishu"]["b1"] = bc_b2; - sys_sd_doc["bochangxishu"]["b2"] = bc_b3; - sys_sd_doc["bochangxishu"]["b3"] = bc_b4; - sys_sd_doc["return_data_type"] = return_data_type; - sys_sd_doc["return_mode"] = ""; //暂定 - sys_sd_doc["atuo_return"] = "no"; - sys_sd_doc["gps_info"]["latitude"] = gps_structure.latitude; - sys_sd_doc["gps_info"]["longitude"] = gps_structure.longitude; - - serializeJson(sys_sd_doc, system_info); - write_log(log_path, system_info,log_level); - - File file; - file = SD_MMC.open("/system_info.json","w+"); - file.println(system_info); - file.flush(); - file.close(); - } -} - - - -bool sys_set_time(uint32_t year,uint32_t month,uint32_t day) -{ - uint32_t m[4] ={4,6,9,11}; - year = year + 2000; - for(int i = 0; i < 4; i++) - { - if(month == m[i]) - { - if(day >= 31) return false; - } - } - if(month == 2) - { - if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) - { - - // write_log(log_path,"is ruannian" + String(day),10); - if(day > 29) return false; - } - else - { - // write_log(log_path,"not is " + String(day),10); - if(day > 28) return false; - - } - } - return true; -} - -// void save_ds18b20_address(uint8_t *data) -// { -// uint8_t add[8][8]={0}; -// uint8_t num = data[0]; -// for(uint8_t i = 0; i < 8; i++) -// { -// add[num][i] = data[i+1]; -// } - -// File file = SD_MMC.open("/ds18b20_address.bin", "wb"); -// file.write((uint8_t *)add, 64); -// file.flush(); -// file.close(); -// } - -// void read_ds18b20_address() -// { -// uint8_t add[8][8]={0}; -// File file = SD_MMC.open("/ds18b20_address.bin", "rb"); -// file.read((uint8_t *)add, 64); -// file.flush(); -// file.close(); - -// for(uint8_t i = 0; i < 8; i++) -// { -// set_ds18b20_address(i,add[i]); -// } -// } - - - -// void sd_read_set_ds18b20_address() -// { -// uint8_t add[8]; -// String str1[8] = {"sensor0","sensor1","sensor2","sensor3","sensor4","sensor5","sensor6","sensor7"}; -// String str2[8] = {"byte0","byte1","byte2","byte3","byte4","byte5","byte6","byte7"}; - -// if(SD_MMC.exists("/temp_sensor.json")) -// { -// File file; -// file = SD_MMC.open("/temp_sensor.json","rb+"); -// String temp_sensor = file.readString(); -// file.flush(); -// file.close(); -// deserializeJson(doc, temp_sensor); -// for(uint8_t a = 0; a < 8; a++) -// { -// for(uint8_t b = 0; b < 8; b++) -// { -// add[b] = doc[str1[a]][str2[b]]; -// ds18b20_doc[a][b] = doc[str1[a]][str2[b]]; -// } -// set_ds18b20_address(a,add); -// } -// temp_sensor = ""; -// serializeJson(doc, temp_sensor); -// write_log(log_path, temp_sensor,log_level); -// } -// else -// { -// write_log(log_path,"do not has file",10); -// for(uint8_t a = 0; a < 8; a++) -// { -// for(uint8_t b = 0; b < 8; b++) -// { -// ds18b20_doc[a][b] = ""; -// } -// } -// File file; -// file = SD_MMC.open("/temp_sensor.json","w+"); -// file.flush(); -// file.close(); -// } -// doc.clear(); -// } - -// void save_sd_ds18b20_address(uint8_t num,uint8_t *addr) -// { -// String str1[8] = {"sensor0","sensor1","sensor2","sensor3","sensor4","sensor5","sensor6","sensor7"}; -// String str2[8] = {"byte0","byte1","byte2","byte3","byte4","byte5","byte6","byte7"}; -// String temp_sensor; - -// for(uint8_t i = 0; i < 8; i++) -// { -// ds18b20_doc[str1[num]][str2[i]] = addr[i]; -// } - -// temp_sensor = ""; -// serializeJson(ds18b20_doc, temp_sensor); -// write_log(log_path, temp_sensor,log_level); -// File file; -// file = SD_MMC.open("/temp_sensor.json","w+"); -// file.println(temp_sensor); -// file.flush(); -// file.close(); -// } - -//dingbiao_path -//1、dingbiao/dingbiao_up_gain.txt -//2、dingbiao/dingbiao_up_offset.txt -//3、dingbiao/dingbiao_down_gain.txt -//4、dingbiao/dingbiao_down_offset.txt - - -bool read_dingbiao(String dingbiao_path,uint8_t *dianbiao_data_buff) -{ - if(SD_MMC.exists(dingbiao_path)) - { - File file; - file = SD_MMC.open(dingbiao_path,"rb"); - file.read(dianbiao_data_buff,sizeof(IS11_datastruct)); - file.flush(); - file.close(); - } - else - { - return false; - } - return true; -} - -void send_dingbiao(String dingbiao_path) -{ - uint32_t dingbiao_len = 0; - uint8_t *send_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)+7]; - uint8_t *read_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)]; - - if(read_dingbiao(dingbiao_path,read_dingbiao_buff)) - { - dingbiao_len = IRIS_Protocol_Pack(0x02,(uint16_t)sizeof(IS11_datastruct), read_dingbiao_buff,send_dingbiao_buff); - wb485Serial.write(send_dingbiao_buff,dingbiao_len); - } - - delete read_dingbiao_buff; - delete send_dingbiao_buff; -} - -void dingbiao_init(String dingbiao_path) -{ - uint8_t *read_dingbiao_buff = new uint8_t[sizeof(IS11_datastruct)]; - - if(read_dingbiao(dingbiao_path,read_dingbiao_buff)) - { - is11Sensor->save_dingbiao(read_dingbiao_buff); - } - delete read_dingbiao_buff; -} - - - - -// /////////////////////////////////上传数据///////////////////////////////////// -// bool UpdateData(String path, char *str, size_t lenth, String Contenttype = "") -// { -// http->beginRequest(); -// http->post(path); -// if (Contenttype != "") -// { -// http->sendHeader(HTTP_HEADER_CONTENT_TYPE, Contenttype); - -// /* code */ -// } - -// http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, lenth); -// http->endRequest(); -// int err = http->write((const byte *)str, lenth); -// delay(1000); -// Serial.print("send date size"); -// Serial.println(err); - -// /* -// for (size_t i = 0; i < lenth; i++) -// { -// Serial.print("0x"); -// Serial.print(str[i],HEX); -// Serial.print(" "); -// if (i%20==0) -// { -// Serial.println(" "); -// } -// } -// */ -// // http->write((const byte*)IS1Sensor.UpData, IS1Sensor.SensorInfo.BandNum*2); -// // http->stop(); -// Serial.println("hear1"); -// if (err == 0) -// { -// http->stop(); -// return false; -// } -// int status = http->responseStatusCode(); -// Serial.println("hear3"); -// if (!status) -// { -// http->stop(); -// return false; -// } - -// int length = http->contentLength(); -// Serial.println("hear4"); -// if (length == 0) -// { -// http->stop(); -// return false; -// } - -// String body = http->responseBody(); -// Serial.println("body:"+body); -// if (body != "ok") -// { -// http->stop(); -// return false; -// } - -// http->stop(); -// return true; -// } - - -// void ReuploadData(String path, String webpath, String content = "") -// { -// vTaskDelay(1); -// Vector files; -// String vec[20]; //每次处理20个 -// files.setStorage(vec); -// while (!sdcard::ListDir(path.c_str(), files)) -// { -// if (files.size() != 0) -// { -// Serial.println("find " + String(files.size()) + "file"); -// for (size_t i = 0; i < files.size(); i++) -// { -// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb"); -// size_t size = nowfile.size(); -// char *arr = new char[size]; -// nowfile.readBytes(arr, size); -// bool flagsucc = UpdateData(webpath, arr, size, content); -// if (!flagsucc) -// { -// return; -// /* code */ -// } -// delete[] arr; -// sdcard::deleteFolderOrFile(files.at(i).c_str()); -// vTaskDelay(1); -// /* code */ -// } -// } -// vTaskDelay(1); -// } -// if (files.size() != 0) -// { -// Serial.println("find " + String(files.size()) + "file not enough 20"); -// for (size_t i = 0; i < files.size(); i++) -// { -// vTaskDelay(1); -// File nowfile = SD_MMC.open(files.at(i).c_str(), "rb"); -// size_t size = nowfile.size(); -// char *arr = new char[size]; -// nowfile.readBytes(arr, size); -// Serial.println("run here now"); -// bool flagsucc = UpdateData(webpath, arr, size, content); -// if (!flagsucc) -// { -// return; -// /* code */ -// } - -// delete[] arr; -// sdcard::deleteFolderOrFile(files.at(i).c_str()); -// /* code */ -// } -// } -// vTaskDelay(1); -// } -// /////////////////////////////////////////////////////////////////////////////////////////////////// - - - - - -String getnetData() -{ - write_log(log_path,"start get net data",10); - int err = http->get("/weather/php/Date.php"); - if (err != 0) - { - return "-1"; - } - write_log(log_path,"responseStatusCode",10); - int status = http->responseStatusCode(); - if (!status) - { - return "-1"; - } - - int times_try = 0; - write_log(log_path,"headerAvailable",10); - while (http->headerAvailable()) - { - if(times_try > 100) - { - http->stop(); - return "-1"; - } - write_log(log_path,"readHeaderName",10); - String headerName = http->readHeaderName(); - write_log(log_path,"readHeaderValue",10); - String headerValue = http->readHeaderValue(); - - vTaskDelay(100); - times_try++; - } - write_log(log_path,"isResponseChunked",10); - if (http->isResponseChunked()) - { - } - write_log(log_path,"contentLength",10); - String body = http->responseBody(); - http->stop(); - return body; -} - - -void rm_dir_or_file( const char * dirname) -{ - File root = SD_MMC.open(dirname); - String dir = String(dirname); - if(root.isDirectory()) - { - File file = root.openNextFile(); - if(file) - { - if (file.isDirectory()) - { - dir = String(dirname) + "/" + String(file.name()); - write_log(log_path,dir,10); - rm_dir_or_file(dir.c_str()); - } - else - { - do - { - dir = String(dirname) + "/" + String(file.name()); - if(SD_MMC.remove(dir)) - { - write_log(log_path,"remove file success",10); - } - else - { - write_log(log_path,"remove file failed",10); - break; - } - file = root.openNextFile(); - if(!file) break; - } while (!file.isDirectory()); - - } - - } - else - { - SD_MMC.rmdir(dir); - } - } - else - { - SD_MMC.remove(dir); - } -} - - - - -// void rm_dir_or_file( const char * dirname) -// { -// File root = SD_MMC.open(dirname); -// String dir = String(dirname); - -// if(root.isDirectory()) -// { -// File file = root.openNextFile(); -// if(file) -// { -// while (file.isDirectory()) -// { -// dir = String(dirname) + "/" + String(file.name()); -// write_log(log_path,dir,10); -// rm_dir_or_file(dir.c_str()); -// file = root.openNextFile(); -// } -// if(!file.isDirectory()) -// { -// do -// { -// dir = String(dirname) + "/" + String(file.name()); -// if(SD_MMC.remove(dir)) -// { -// write_log(log_path,"remove file success",10); -// } -// else -// { -// write_log(log_path,"remove file failed",10); -// break; -// } -// file = root.openNextFile(); -// if(!file) break; -// } while (!file.isDirectory()); - -// } -// } -// else -// { -// SD_MMC.rmdir(dir); -// } -// } -// else -// { -// SD_MMC.remove(dir); -// } -// } - - - - - - - - - - - - -//0:关闭 1:开启 -void low_power_mode(uint8_t state) -{ - if(state == 0) - { - //关闭光谱仪 - pinMode(36,INPUT); - //关闭AIR780E - pinMode(37,OUTPUT); - digitalWrite(37,LOW); - } - else - { - //开启光谱仪 - pinMode(36,OUTPUT); - digitalWrite(36,LOW); - //开启AIR780E - pinMode(37,OUTPUT); - digitalWrite(37,HIGH); - } -} \ No newline at end of file diff --git a/src/VSMD_command_handler.cpp b/src/VSMD_command_handler.cpp index f8486e6..c866709 100644 --- a/src/VSMD_command_handler.cpp +++ b/src/VSMD_command_handler.cpp @@ -8,6 +8,10 @@ // 外部串口对象声明 extern HardwareSerial DebugSerial; +// 全局变量用于CID和RS485模式管理 +static uint8_t currentTargetCID = 0; // 当前目标设备CID +static bool isCurrentRS485Mode = false; // 当前是否为RS485模式 + void sendJsonResponse(const char* command, const char* status, int value) { StaticJsonDocument responseDoc; @@ -63,218 +67,244 @@ void processJsonCommand(const char* jsonString) { return; } + // 解析命令中的CID + uint8_t cid = 0; + char actualCommand[64]; + bool isRS485 = parse_command_with_cid(command, &cid, actualCommand); + + // 设置全局变量供其他函数使用 + currentTargetCID = cid; + isCurrentRS485Mode = isRS485; + + if (isRS485) { + DebugSerial.print("RS485 Command - CID: "); + DebugSerial.print(cid); + DebugSerial.print(", Command: "); + DebugSerial.println(actualCommand); + } else { + DebugSerial.print("RS232 Command: "); + DebugSerial.println(actualCommand); + } + // 获取 PA 参数对象(可选) JsonObject params = doc["PA"]; - // 根据command执行相应操作 - if (strcmp(command, "cfg") == 0) { + // 根据actualCommand执行相应操作 + if (strcmp(actualCommand, "cfg") == 0) { handle_cfg_cmd(); - } else if (strcmp(command, "ena") == 0) { + } else if (strcmp(actualCommand, "ena") == 0) { handle_ena_cmd(); - } else if (strcmp(command, "off") == 0) { + } else if (strcmp(actualCommand, "off") == 0) { handle_off_cmd(); - } else if (strcmp(command, "mov") == 0) { + } else if (strcmp(actualCommand, "mov") == 0) { handle_mov_cmd(); - } else if (strcmp(command, "pos") == 0) { + } else if (strcmp(actualCommand, "pos") == 0) { int value = params.containsKey("value") ? params["value"].as() : 0; handle_pos_cmd(value); - } else if (strcmp(command, "rmv") == 0) { + } else if (strcmp(actualCommand, "rmv") == 0) { int value = params.containsKey("value") ? params["value"].as() : 0; handle_rmv_cmd(value); - } else if (strcmp(command, "pps") == 0) { + } else if (strcmp(actualCommand, "pps") == 0) { if (params.containsKey("value")) { int value = params["value"].as(); handle_pps_cmd(value); } else { handle_pps_cmd(); } - } else if (strcmp(command, "org") == 0) { + } else if (strcmp(actualCommand, "org") == 0) { handle_org_cmd(); - } else if (strcmp(command, "stp") == 0) { + } else if (strcmp(actualCommand, "stp") == 0) { if (params.containsKey("value")) { int value = params["value"].as(); handle_stp_cmd(value); } else { handle_stp_cmd(); // 无参数,默认为0(减速停止) } - } else if (strcmp(command, "dev") == 0) { + } else if (strcmp(actualCommand, "dev") == 0) { handle_dev_cmd(); - } else if (strcmp(command, "sts") == 0) { + } else if (strcmp(actualCommand, "sts") == 0) { handle_sts_cmd(); - } else if (strcmp(command, "zero_start") == 0) { + } else if (strcmp(actualCommand, "zero_start") == 0) { handle_zero_start_cmd(); - } else if (strcmp(command, "zero_stop") == 0) { + } else if (strcmp(actualCommand, "zero_stop") == 0) { handle_zero_stop_cmd(); - } else if (strcmp(command, "cfg_bdr") == 0) { + } else if (strcmp(actualCommand, "cfg_bdr") == 0) { int baudrate = params["value"].as(); handle_cfg_bdr_cmd(baudrate); - } else if (strcmp(command, "cfg_mcs") == 0) { + } else if (strcmp(actualCommand, "cfg_mcs") == 0) { //设置微步细分 int mcs = params["value"].as(); handle_cfg_mcs_cmd(mcs); - } else if (strcmp(command, "cfg_spd") == 0) { + } else if (strcmp(actualCommand, "cfg_spd") == 0) { //设置运行速度 int spd = params["value"].as(); handle_cfg_spd_cmd(spd); - } else if (strcmp(command, "cfg_acc") == 0) { + } else if (strcmp(actualCommand, "cfg_acc") == 0) { //设置加速度 int acc = params["value"].as(); handle_cfg_accSpeed_cmd(acc); - } else if (strcmp(command, "cfg_dec") == 0) { + } else if (strcmp(actualCommand, "cfg_dec") == 0) { //设置减速度 int dec = params["value"].as(); handle_cfg_decSpeed_cmd(dec); - } else if (strcmp(command, "cfg_cra") == 0) { + } else if (strcmp(actualCommand, "cfg_cra") == 0) { //设置加速时电流 float cra = params["value"].as(); handle_cfg_cra_cmd(cra); - } else if (strcmp(command, "cfg_crn") == 0) { + } else if (strcmp(actualCommand, "cfg_crn") == 0) { //设置匀速时电流 float crn = params["value"].as(); handle_cfg_crn_cmd(crn); - } else if (strcmp(command, "cfg_crh") == 0) { + } else if (strcmp(actualCommand, "cfg_crh") == 0) { //设置HOLD电流 float crh = params["value"].as(); handle_cfg_crh_cmd(crh); // ***************************S3-S6传感器触发事件配置(参数范围0-9)**********************/ - } else if (strcmp(command, "cfg_s3f") == 0) { + } else if (strcmp(actualCommand, "cfg_s3f") == 0) { int s3f = params["value"].as(); handle_cfg_sensor_cmd("s3f", s3f); - } else if (strcmp(command, "cfg_s3r") == 0) { + } else if (strcmp(actualCommand, "cfg_s3r") == 0) { int s3r = params["value"].as(); handle_cfg_sensor_cmd("s3r", s3r); - } else if (strcmp(command, "cfg_s4f") == 0) { + } else if (strcmp(actualCommand, "cfg_s4f") == 0) { int s4f = params["value"].as(); handle_cfg_sensor_cmd("s4f", s4f); - } else if (strcmp(command, "cfg_s4r") == 0) { + } else if (strcmp(actualCommand, "cfg_s4r") == 0) { int s4r = params["value"].as(); handle_cfg_sensor_cmd("s4r", s4r); - } else if (strcmp(command, "cfg_s5f") == 0) { + } else if (strcmp(actualCommand, "cfg_s5f") == 0) { int s5f = params["value"].as(); handle_cfg_sensor_cmd("s5f", s5f); - } else if (strcmp(command, "cfg_s5r") == 0) { + } else if (strcmp(actualCommand, "cfg_s5r") == 0) { int s5r = params["value"].as(); handle_cfg_sensor_cmd("s5r", s5r); - } else if (strcmp(command, "cfg_s6f") == 0) { + } else if (strcmp(actualCommand, "cfg_s6f") == 0) { int s6f = params["value"].as(); handle_cfg_sensor_cmd("s6f", s6f); - } else if (strcmp(command, "cfg_s6r") == 0) { + } else if (strcmp(actualCommand, "cfg_s6r") == 0) { int s6r = params["value"].as(); handle_cfg_sensor_cmd("s6r", s6r); // ***************************S3-S6传感器工作模式配置(参数范围0-1)**********************/ - } else if (strcmp(command, "cfg_s3") == 0) { + } else if (strcmp(actualCommand, "cfg_s3") == 0) { int s3 = params["value"].as(); handle_cfg_sensor_cmd("s3", s3); - } else if (strcmp(command, "cfg_s4") == 0) { + } else if (strcmp(actualCommand, "cfg_s4") == 0) { int s4 = params["value"].as(); handle_cfg_sensor_cmd("s4", s4); - } else if (strcmp(command, "cfg_s5") == 0) { + } else if (strcmp(actualCommand, "cfg_s5") == 0) { int s5 = params["value"].as(); handle_cfg_sensor_cmd("s5", s5); - } else if (strcmp(command, "cfg_s6") == 0) { + } else if (strcmp(actualCommand, "cfg_s6") == 0) { int s6 = params["value"].as(); handle_cfg_sensor_cmd("s6", s6); //**************************************************************** */ - } else if (strcmp(command, "cfg_zmd") == 0) { + } else if (strcmp(actualCommand, "cfg_zmd") == 0) { //设置归零功能 int zmd = params["value"].as(); handle_cfg_zmd_cmd(zmd); - } else if (strcmp(command, "cfg_snr") == 0) { + } else if (strcmp(actualCommand, "cfg_snr") == 0) { //设置归零用传感器 int snr = params["value"].as(); handle_cfg_snr_cmd(snr); - } else if (strcmp(command, "cfg_osv") == 0) { + } else if (strcmp(actualCommand, "cfg_osv") == 0) { //设置归零传感器OPEN时电平 int osv = params["value"].as(); handle_cfg_osv_cmd(osv); - } else if (strcmp(command, "cfg_zsd") == 0) { + } else if (strcmp(actualCommand, "cfg_zsd") == 0) { //设置归零速度 int zsd = params["value"].as(); handle_cfg_zsd_cmd(zsd); - } else if (strcmp(command, "cfg_zsp") == 0) { + } else if (strcmp(actualCommand, "cfg_zsp") == 0) { //设置归零后的安全位置 int zsp = params["value"].as(); handle_cfg_zsp_cmd(zsp); - } else if (strcmp(command, "cfg_dmd") == 0) { + } else if (strcmp(actualCommand, "cfg_dmd") == 0) { //设置离线运行模式 int dmd = params["value"].as(); handle_cfg_dmd_cmd(dmd); - } else if (strcmp(command, "cfg_dar") == 0) { + } else if (strcmp(actualCommand, "cfg_dar") == 0) { //设置无握手启动时间 int dar = params["value"].as(); handle_cfg_dar_cmd(dar); - } else if (strcmp(command, "cfg_msr") == 0) { + } else if (strcmp(actualCommand, "cfg_msr") == 0) { //设置负极限传感器 int msr = params["value"].as(); handle_cfg_msr_cmd(msr); - } else if (strcmp(command, "cfg_msv") == 0) { + } else if (strcmp(actualCommand, "cfg_msv") == 0) { //设置负极限触发电平 int msv = params["value"].as(); handle_cfg_msv_cmd(msv); - } else if (strcmp(command, "cfg_psr") == 0) { + } else if (strcmp(actualCommand, "cfg_psr") == 0) { //设置正极限传感器 int psr = params["value"].as(); handle_cfg_psr_cmd(psr); - } else if (strcmp(command, "cfg_psv") == 0) { + } else if (strcmp(actualCommand, "cfg_psv") == 0) { //设置正极限触发电平 int psv = params["value"].as(); handle_cfg_psv_cmd(psv); - } else if (strcmp(command, "cfg_pae") == 0) { + } else if (strcmp(actualCommand, "cfg_pae") == 0) { //设置上电使能 int pae = params["value"].as(); handle_cfg_pae_cmd(pae); - } else if (strcmp(command, "cfg_emod") == 0) { + } else if (strcmp(actualCommand, "cfg_emod") == 0) { //设置编码器模式 int emod = params["value"].as(); handle_cfg_emod_cmd(emod); - } else if (strcmp(command, "cfg_elns") == 0) { + } else if (strcmp(actualCommand, "cfg_elns") == 0) { //设置编码器线数 int elns = params["value"].as(); handle_cfg_elns_cmd(elns); - } else if (strcmp(command, "cfg_estp") == 0) { + } else if (strcmp(actualCommand, "cfg_estp") == 0) { //设置电机每圈整步数 int estp = params["value"].as(); handle_cfg_estp_cmd(estp); - } else if (strcmp(command, "cfg_erty") == 0) { + } else if (strcmp(actualCommand, "cfg_erty") == 0) { //设置堵转后重试次数 int erty = params["value"].as(); handle_cfg_erty_cmd(erty); - } else if (strcmp(command, "cfg_edir") == 0) { + } else if (strcmp(actualCommand, "cfg_edir") == 0) { //设置编码器方向 int edir = params["value"].as(); handle_cfg_edir_cmd(edir); - } else if (strcmp(command, "cfg_ez") == 0) { + } else if (strcmp(actualCommand, "cfg_ez") == 0) { //设置编码器灵敏度 int ez = params["value"].as(); handle_cfg_ez_cmd(ez); - } else if (strcmp(command, "cfg_ewr") == 0) { + } else if (strcmp(actualCommand, "cfg_ewr") == 0) { //设置堵转后动作 int ewr = params["value"].as(); handle_cfg_ewr_cmd(ewr); - } else if (strcmp(command, "sav") == 0) { + } else if (strcmp(actualCommand, "sav") == 0) { handle_sav_cmd(); // ***************************S3-S6端口输出控制**********************/ - } else if (strcmp(command, "s3_on") == 0) { + } else if (strcmp(actualCommand, "s3_on") == 0) { handle_port_output_cmd("s3", "on"); - } else if (strcmp(command, "s3_off") == 0) { + } else if (strcmp(actualCommand, "s3_off") == 0) { handle_port_output_cmd("s3", "off"); - } else if (strcmp(command, "s4_on") == 0) { + } else if (strcmp(actualCommand, "s4_on") == 0) { handle_port_output_cmd("s4", "on"); - } else if (strcmp(command, "s4_off") == 0) { + } else if (strcmp(actualCommand, "s4_off") == 0) { handle_port_output_cmd("s4", "off"); - } else if (strcmp(command, "s5_on") == 0) { + } else if (strcmp(actualCommand, "s5_on") == 0) { handle_port_output_cmd("s5", "on"); - } else if (strcmp(command, "s5_off") == 0) { + } else if (strcmp(actualCommand, "s5_off") == 0) { handle_port_output_cmd("s5", "off"); - } else if (strcmp(command, "s6_on") == 0) { + } else if (strcmp(actualCommand, "s6_on") == 0) { handle_port_output_cmd("s6", "on"); - } else if (strcmp(command, "s6_off") == 0) { + } else if (strcmp(actualCommand, "s6_off") == 0) { handle_port_output_cmd("s6", "off"); //************************************************************* */ } else { - sendJsonResponse(command, "UNKNOWN_COMMAND"); + sendJsonResponse(actualCommand, "UNKNOWN_COMMAND"); } + + // // 存储当前使用的CID供命令处理函数使用 + // if (isRS485) { + // // 可以在这里设置全局变量或传递给处理函数 + // DebugSerial.print("Command will be sent to device CID: "); + // DebugSerial.println(cid); + // } } void processStringCommand(const char* stringData) { @@ -283,21 +313,69 @@ void processStringCommand(const char* stringData) { sendStringResponse(response.c_str()); } -void send_vsmd_cmd(const char* cmd, int value) { +// 解析包含CID的命令字符串 +bool parse_command_with_cid(const char* command, uint8_t* cid, char* actualCommand) { + // 检查命令是否以数字开头(CID) + if (isdigit(command[0])) { + // 解析CID + char* endPtr; + long parsedCID = strtol(command, &endPtr, 10); + + // 检查CID范围和格式 + if (parsedCID >= 0 && parsedCID <= 32 && *endPtr == ' ') { + *cid = (uint8_t)parsedCID; + // 跳过CID和空格,获取实际命令 + strcpy(actualCommand, endPtr + 1); + return true; // RS485模式 + } + } + + // 没有CID前缀,直接复制命令 + *cid = 0; // 默认CID + strcpy(actualCommand, command); + return false; // RS232模式 +} + +// 带CID的命令发送函数 +void send_vsmd_cmd_with_cid(uint8_t cid, const char* cmd, int value) { + // 发送CID前缀 + Serial.print(cid); + Serial.print(" "); + + // 发送命令 int len = strlen(cmd); for (int i = 0; i < len; i++) { Serial.print(cmd[i]); } + // 如果有参数值,则添加参数 if (value != -1) { Serial.print(" "); Serial.print(value); } Serial.println(); - delay(100); } +void send_vsmd_cmd(const char* cmd, int value) { + if (isCurrentRS485Mode) { + send_vsmd_cmd_with_cid(currentTargetCID, cmd, value); + } else { + // 原有RS232模式 + int len = strlen(cmd); + for (int i = 0; i < len; i++) { + Serial.print(cmd[i]); + } + // 如果有参数值,则添加参数 + if (value != -1) { + Serial.print(" "); + Serial.print(value); + } + Serial.println(); + delay(100); + } +} + void printSerialRawData(const uint8_t* buffer, int bytesRead) { // 打印Serial串口收到的原始数据 DebugSerial.print("Serial received data (bytes): "); diff --git a/src/VSMD_command_handler.h b/src/VSMD_command_handler.h index 0c61da7..c4b8203 100644 --- a/src/VSMD_command_handler.h +++ b/src/VSMD_command_handler.h @@ -16,6 +16,9 @@ void send_vsmd_cmd(const char* cmd, int value = -1); // send_vsmd_cmd("dev"); // 不带参数,使用默认值-1 // send_vsmd_cmd("pos", value); // 带参数 +// RS485通信函数 +void send_vsmd_cmd_with_cid(uint8_t cid, const char* cmd, int value = -1); +bool parse_command_with_cid(const char* command, uint8_t* cid, char* actualCommand); // 打印Serial串口收到的原始数据 void printSerialRawData(const uint8_t* buffer, int bytesRead); //VSMD串口数据解析 diff --git a/src/bluetooth_handler.cpp b/src/bluetooth_handler.cpp index b9e45a4..251c93e 100644 --- a/src/bluetooth_handler.cpp +++ b/src/bluetooth_handler.cpp @@ -200,7 +200,7 @@ void initBluetooth() { btAdvertising->setMinPreferred(0x0); BLEDevice::startAdvertising(); - DebugSerial.println("VSMD_Ble_Controller initialized"); + DebugSerial.println("Ble Initialized"); // 初始化包接收状态 resetPacketReceiving(); diff --git a/src/main.cpp b/src/main.cpp index 62365a1..c3ea4ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,11 @@ VSMDParser parser; +#define USE_RS232 // 注释掉这行表示使用 RS-485;取消注释表示使用 RS-232 +//#define USE_RS485 +#define PWR_232CTL 5 // 232电源控制引脚 +#define PWR_485CTL 15 // 485电源控制引脚 + #define DEBUG_TX_PIN 17 // TX #define DEBUG_RX_PIN 16 // RX #define BUFFER_SIZE 1024 @@ -29,27 +34,48 @@ int32_t pulsePerSecond; void printDeviceStatus(); void handleSerialData(); +bool Dev_Mode = false; + void printDeviceStatus() { - DebugSerial.println("\n=== Device Status ==="); - DebugSerial.print("Enabled: "); + DebugSerial.print("Device Enabled: "); DebugSerial.println(deviceEnabled ? "Yes" : "No"); - DebugSerial.print("Bluetooth: "); - DebugSerial.println(isBluetoothConnected() ? "Connected" : "Disconnected"); - DebugSerial.println("==================\n"); + DebugSerial.print("Ble Connected: "); + DebugSerial.println(isBluetoothConnected() ? "Yes" : "No"); + DebugSerial.print("Dev Mode: "); + DebugSerial.println(Dev_Mode ? "RS232" : "RS485"); +} + +void Power_Enable() +{ + pinMode(PWR_232CTL, OUTPUT); + pinMode(PWR_485CTL, OUTPUT); + + #ifdef USE_RS232 + digitalWrite(PWR_232CTL, HIGH); + digitalWrite(PWR_485CTL, LOW); + Dev_Mode = true; + #elif defined(USE_RS485) + digitalWrite(PWR_485CTL, HIGH); + digitalWrite(PWR_232CTL, LOW); + Dev_Mode = false; + #endif } void setup() { // 调试串口 DebugSerial.begin(9600, SERIAL_8N1, DEBUG_RX_PIN, DEBUG_TX_PIN); - DebugSerial.setTimeout(1); + DebugSerial.setTimeout(100); // 电机数据通信串口 Serial.begin(9600); Serial.setTimeout(100); - DebugSerial.println("VSMD INIT"); - DebugSerial.println("CMD: dev, sts, cfg, ena, off, mov, pos, rmv, pps, org, stp, zero, sav"); - DebugSerial.println("cfg CMD: bdr, mcs, spd, acc, dec, cra, crn, crh, s1f, s1r, s2f, s2r, zmd, snr, osv, zsd, zsp, dmd, dar, msr, msv, psr, psv"); + // DebugSerial.println("VSMD INIT"); + // DebugSerial.println("CMD: dev, sts, cfg, ena, off, mov, pos, rmv, pps, org, stp, zero, sav"); + // DebugSerial.println("cfg CMD: bdr, mcs, spd, acc, dec, cra, crn, crh, s1f, s1r, s2f, s2r, zmd, snr, osv, zsd, zsp, dmd, dar, msr, msv, psr, psv"); + + //初始化电源控制引脚 + Power_Enable(); // 初始化蓝牙 initBluetooth(); diff --git a/test5.json b/test5.json deleted file mode 100644 index c18c2eb..0000000 --- a/test5.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "limit": { - "cfg msr": 1, //设置负极限传感器 - "cfg msv": 0, //设置负极限触发电平 - "cfg psr": 2, //设置正极限传感器 - "cfg psv": 0 //设置正极限触发电平 - }, - "other": { - "cfg acc": 20000, //设置加速度 - "cfg cra": 4, //设置加速时电流 - "cfg crh": 1, //设置HOLD电流 - "cfg crn": 4, //设置匀速时电流 - "cfg dec": 20000, //设置减速度 - "cfg mcs": 7 //设置微步细分 - }, - "zero start": { - "cfg osv": 0, //设置归零传感器OPEN时电平 - "cfg snr": 0, //设置归零用传感器 - "cfg zmd": 2, //设置归零功能 - "cfg zsd": 3000, //设置归零速度(正负代表方向) - "cfg zsp": 2400 //设置归零后的安全位置 - } -} diff --git a/vsmd_IRIS.txt b/vsmd_IRIS.txt new file mode 100644 index 0000000..573e9fa --- /dev/null +++ b/vsmd_IRIS.txt @@ -0,0 +1,287 @@ +***********RS485模式下的JSON命令********* +JSON: {"command": "1 dev"} +HEX: 55 AA 00 00 13 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 31 20 64 65 76 22 7D EE EE +**************************************** + +// 使能设备 +JSON: {"command": "ena"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 65 6E 61 22 7D EE EE + +// 关闭设备 +JSON: {"command": "off"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 6F 66 66 22 7D EE EE + +// 开始运行 +JSON: {"command": "mov"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 6D 6F 76 22 7D EE EE + +// 停止运行(减速停止) +JSON: {"command": "stp", "value": 0} +HEX: 55 AA 00 00 1B 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 74 70 22 2C 22 76 61 6C 75 65 22 3A 30 7D EE EE + +// 停止运行(立即停止) +JSON: {"command": "stp", "value": 1} +HEX: 55 AA 00 00 1B 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 74 70 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + + + +// 绝对位置移动 +JSON: {"command": "pos", "value": 1000} +HEX: 55 AA 00 00 1E 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 70 6F 73 22 2C 22 76 61 6C 75 65 22 3A 31 30 30 30 7D EE EE + +// 相对位置移动 +JSON: {"command": "rmv", "value": 500} +HEX: 55 AA 00 00 1D 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 72 6D 76 22 2C 22 76 61 6C 75 65 22 3A 35 30 30 7D EE EE + +// 脉冲频率设置 +JSON: {"command": "pps", "value": 2000} +HEX: 55 AA 00 00 1E 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 70 70 73 22 2C 22 76 61 6C 75 65 22 3A 32 30 30 30 7D EE EE + +// 查询脉冲频率 +JSON: {"command": "pps"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 70 70 73 22 7D EE EE + + + +// 原点归零 +JSON: {"command": "org"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 6F 72 67 22 7D EE EE + +// 开始归零 +JSON: {"command": "zero_start"} +HEX: 55 AA 00 00 18 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 7A 65 72 6F 5F 73 74 61 72 74 22 7D EE EE + +// 停止归零 +JSON: {"command": "zero_stop"} +HEX: 55 AA 00 00 17 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 7A 65 72 6F 5F 73 74 6F 70 22 7D EE EE + + + +// 设备信息查询 +JSON: {"command": "dev"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 64 65 76 22 7D EE EE + +// 状态查询 +JSON: {"command": "sts"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 74 73 22 7D EE EE + +// 配置查询 +JSON: {"command": "cfg"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 22 7D EE EE + + + + +// 波特率配置 +JSON: {"command": "cfg_bdr", "value": 9600} +HEX: 55 AA 00 00 22 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 62 64 72 22 2C 22 76 61 6C 75 65 22 3A 39 36 30 30 7D EE EE + +// 微步细分配置 +JSON: {"command": "cfg_mcs", "value": 8} +HEX: 55 AA 00 00 1F 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 6D 63 73 22 2C 22 76 61 6C 75 65 22 3A 38 7D EE EE + +// 运行速度配置 +JSON: {"command": "cfg_spd", "value": 1000} +HEX: 55 AA 00 00 22 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 70 64 22 2C 22 76 61 6C 75 65 22 3A 31 30 30 30 7D EE EE + +// 加速度配置 +JSON: {"command": "cfg_acc", "value": 500} +HEX: 55 AA 00 00 21 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 61 63 63 22 2C 22 76 61 6C 75 65 22 3A 35 30 30 7D EE EE + +// 减速度配置 +JSON: {"command": "cfg_dec", "value": 500} +HEX: 55 AA 00 00 21 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 64 65 63 22 2C 22 76 61 6C 75 65 22 3A 35 30 30 7D EE EE + + + + +// 加速电流配置 +JSON: {"command": "cfg_cra", "value": 80} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 63 72 61 22 2C 22 76 61 6C 75 65 22 3A 38 30 7D EE EE + +// 匀速电流配置 +JSON: {"command": "cfg_crn", "value": 60} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 63 72 6E 22 2C 22 76 61 6C 75 65 22 3A 36 30 7D EE EE + +// HOLD电流配置 +JSON: {"command": "cfg_crh", "value": 40} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 63 72 68 22 2C 22 76 61 6C 75 65 22 3A 34 30 7D EE EE + + + + + +// 传感器S3触发事件配置 +JSON: {"command": "cfg_s3f", "value": 1} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 33 66 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + +// 传感器S4触发事件配置 +JSON: {"command": "cfg_s4f", "value": 2} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 34 66 22 2C 22 76 61 6C 75 65 22 3A 32 7D EE EE + +// 传感器S5触发事件配置 +JSON: {"command": "cfg_s5f", "value": 3} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 35 66 22 2C 22 76 61 6C 75 65 22 3A 33 7D EE EE + +// 传感器S6触发事件配置 +JSON: {"command": "cfg_s6f", "value": 4} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 36 66 22 2C 22 76 61 6C 75 65 22 3A 34 7D EE EE + +// 传感器S3工作模式配置 +JSON: {"command": "cfg_s3", "value": 1} +HEX: 55 AA 00 00 1F 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 33 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + +// 传感器S4工作模式配置 +JSON: {"command": "cfg_s4", "value": 0} +HEX: 55 AA 00 00 1F 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 34 22 2C 22 76 61 6C 75 65 22 3A 30 7D EE EE + +// 传感器S5工作模式配置 +JSON: {"command": "cfg_s5", "value": 1} +HEX: 55 AA 00 00 1F 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 35 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + +// 传感器S6工作模式配置 +JSON: {"command": "cfg_s6", "value": 0} +HEX: 55 AA 00 00 1F 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 36 22 2C 22 76 61 6C 75 65 22 3A 30 7D EE EE + + + +// S3端口输出开启 +JSON: {"command": "s3_on"} +HEX: 55 AA 00 00 13 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 33 5F 6F 6E 22 7D EE EE + +// S3端口输出关闭 +JSON: {"command": "s3_off"} +HEX: 55 AA 00 00 14 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 33 5F 6F 66 66 22 7D EE EE + +// S4端口输出开启 +JSON: {"command": "s4_on"} +HEX: 55 AA 00 00 13 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 34 5F 6F 6E 22 7D EE EE + +// S4端口输出关闭 +JSON: {"command": "s4_off"} +HEX: 55 AA 00 00 14 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 34 5F 6F 66 66 22 7D EE EE + +// S5端口输出开启 +JSON: {"command": "s5_on"} +HEX: 55 AA 00 00 13 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 35 5F 6F 6E 22 7D EE EE + +// S5端口输出关闭 +JSON: {"command": "s5_off"} +HEX: 55 AA 00 00 14 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 35 5F 6F 66 66 22 7D EE EE + +// S6端口输出开启 +JSON: {"command": "s6_on"} +HEX: 55 AA 00 00 13 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 36 5F 6F 6E 22 7D EE EE + +// S6端口输出关闭 +JSON: {"command": "s6_off"} +HEX: 55 AA 00 00 14 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 36 5F 6F 66 66 22 7D EE EE + + + + +// 归零功能配置 +JSON: {"command": "cfg_zmd", "value": 1} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 7A 6D 64 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + +// 归零用传感器配置 +JSON: {"command": "cfg_snr", "value": 3} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 73 6E 72 22 2C 22 76 61 6C 75 65 22 3A 33 7D EE EE + +// 归零传感器OPEN时电平配置 +JSON: {"command": "cfg_osv", "value": 0} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 6F 73 76 22 2C 22 76 61 6C 75 65 22 3A 30 7D EE EE + +// 归零速度配置 +JSON: {"command": "cfg_zsd", "value": 200} +HEX: 55 AA 00 00 21 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 7A 73 64 22 2C 22 76 61 6C 75 65 22 3A 32 30 30 7D EE EE + +// 归零后安全位置配置 +JSON: {"command": "cfg_zsp", "value": 100} +HEX: 55 AA 00 00 21 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 7A 73 70 22 2C 22 76 61 6C 75 65 22 3A 31 30 30 7D EE EE + + + + +// 离线运行模式配置 +JSON: {"command": "cfg_dmd", "value": 1} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 64 6D 64 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + +// 无握手启动时间配置 +JSON: {"command": "cfg_dar", "value": 1000} +HEX: 55 AA 00 00 22 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 64 61 72 22 2C 22 76 61 6C 75 65 22 3A 31 30 30 30 7D EE EE + + + + + + + + +// 负极限传感器配置 +JSON: {"command": "cfg_msr", "value": 3} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 6D 73 72 22 2C 22 76 61 6C 75 65 22 3A 33 7D EE EE + +// 负极限触发电平配置 +JSON: {"command": "cfg_msv", "value": 0} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 6D 73 76 22 2C 22 76 61 6C 75 65 22 3A 30 7D EE EE + +// 正极限传感器配置 +JSON: {"command": "cfg_psr", "value": 4} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 70 73 72 22 2C 22 76 61 6C 75 65 22 3A 34 7D EE EE + +// 正极限触发电平配置 +JSON: {"command": "cfg_psv", "value": 1} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 70 73 76 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + +// 上电使能配置 +JSON: {"command": "cfg_pae", "value": 1} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 70 61 65 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + + + + + + + +// 编码器模式配置 +JSON: {"command": "cfg_emod", "value": 1} +HEX: 55 AA 00 00 21 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 65 6D 6F 64 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + +// 编码器线数配置 +JSON: {"command": "cfg_elns", "value": 1000} +HEX: 55 AA 00 00 22 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 65 6C 6E 73 22 2C 22 76 61 6C 75 65 22 3A 31 30 30 30 7D EE EE + +// 电机每圈整步数配置 +JSON: {"command": "cfg_estp", "value": 200} +HEX: 55 AA 00 00 22 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 65 73 74 70 22 2C 22 76 61 6C 75 65 22 3A 32 30 30 7D EE EE + +// 堵转后重试次数配置 +JSON: {"command": "cfg_erty", "value": 3} +HEX: 55 AA 00 00 21 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 65 72 74 79 22 2C 22 76 61 6C 75 65 22 3A 33 7D EE EE + +// 编码器方向配置 +JSON: {"command": "cfg_edir", "value": 0} +HEX: 55 AA 00 00 21 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 65 64 69 72 22 2C 22 76 61 6C 75 65 22 3A 30 7D EE EE + +// 编码器灵敏度配置 +JSON: {"command": "cfg_ez", "value": 5} +HEX: 55 AA 00 00 1F 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 65 7A 22 2C 22 76 61 6C 75 65 22 3A 35 7D EE EE + +// 堵转后动作配置 +JSON: {"command": "cfg_ewr", "value": 1} +HEX: 55 AA 00 00 20 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 63 66 67 5F 65 77 72 22 2C 22 76 61 6C 75 65 22 3A 31 7D EE EE + + + + + + +// 保存配置 +JSON: {"command": "sav"} +HEX: 55 AA 00 00 11 7B 22 63 6F 6D 6D 61 6E 64 22 3A 22 73 61 76 22 7D EE EE + + + + +