diff --git a/src/iedModbus_mgr/common.cpp b/src/iedModbus_mgr/common.cpp index af66d01..68d9c7a 100644 --- a/src/iedModbus_mgr/common.cpp +++ b/src/iedModbus_mgr/common.cpp @@ -34,7 +34,7 @@ void xUnsigned_char_hex_out(const char *msg, unsigned char* src, int start, int printf("%s:\n", msg); } for (int i = start; i < end + 1; i++) { - printf("%02X", src[i]); + printf("%02X ", src[i]); } printf("\n"); @@ -146,6 +146,26 @@ int xEnum_relay_comName(char* com_name) return false; } + +//枚举找到使用的串口线对应的端口号 +int xEnum_adc_comName(char* com_name) +{ + const char* serial_description = "USB Serial Port";//项目中adc转换使用的串口芯片描述符 + vector spInfo; + enumDetailsSerialPorts(spInfo); + + for (int i = 0; i < spInfo.size(); i++) + { + std::cout << i << " - " << spInfo[i].portName << " | " << spInfo[i].description << std::endl; + if (!strncmp(spInfo[i].description.c_str(), serial_description, strlen(serial_description))) { + memcpy(com_name, spInfo[i].portName.c_str(), spInfo[i].portName.length()); + printf("has find uart number is : %s\n", com_name); + return true; + } + } + + return false; +} int xGet_config_from_configFile() { #if 1 @@ -196,24 +216,32 @@ int xGet_config_from_configFile() const char* CO_threshold = ini_get(ini_ctl, "ALERT", "CO_THRESHOLD"); xSet_CO_threshold(CO_threshold); const char* CO_alert = ini_get(ini_ctl, "ALERT", "CO_alert"); - xSet_H2_alert(CO_alert); + xSet_CO_alert(CO_alert); const char* CO2_threshold = ini_get(ini_ctl, "ALERT", "CO2_THRESHOLD"); xSet_CO2_threshold(CO2_threshold); const char* CO2_alert = ini_get(ini_ctl, "ALERT", "CO2_alert"); - xSet_H2_alert(CO2_alert); + xSet_CO2_alert(CO2_alert); const char* CH4_threshold = ini_get(ini_ctl, "ALERT", "CH4_THRESHOLD"); xSet_CH4_period(CH4_threshold); + const char* CH4_alert = ini_get(ini_ctl, "ALERT", "CH4_alert"); + xSet_CH4_alert(CH4_alert); const char* C2H2_threshold = ini_get(ini_ctl, "ALERT", "C2H2_THRESHOLD"); xSet_C2H2_threshold(C2H2_threshold); + const char* C2H2_alert = ini_get(ini_ctl, "ALERT", "C2H2_alert"); + xSet_C2H2_alert(C2H2_alert); const char* C2H4_threshold = ini_get(ini_ctl, "ALERT", "C2H4_THRESHOLD"); xSet_C2H4_period(C2H4_threshold); + const char* C2H4_alert = ini_get(ini_ctl, "ALERT", "C2H4_alert"); + xSet_C2H4_alert(C2H4_alert); const char* C2H6_threshold = ini_get(ini_ctl, "ALERT", "C2H6_THRESHOLD"); xSet_C2H6_threshold(C2H6_threshold); + const char* C2H6_alert = ini_get(ini_ctl, "ALERT", "C2H6_alert"); + xSet_C2H6_alert(C2H6_alert); //读取程序延迟执行的时间 const char* process_delay_min = ini_get(ini_ctl, "PROCESS", "DELAY_RUN_MIN"); diff --git a/src/iedModbus_mgr/common.h b/src/iedModbus_mgr/common.h index 74a0060..35b4517 100644 --- a/src/iedModbus_mgr/common.h +++ b/src/iedModbus_mgr/common.h @@ -1,10 +1,13 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -#pragma once +#pragma once +#include void xDelay_run_process(); int xGet_config_from_configFile(); void xUnsigned_char_hex_out(const char* msg, unsigned char* src, int start, int end); int xEnum_relay_comName(char* com_name); +int xEnum_adc_comName(char* com_name); +uint16_t CRCVerify(uint8_t *puchMsg, uint16_t usDataLen); #endif \ No newline at end of file diff --git a/src/iedModbus_mgr/gas_monitor.cpp b/src/iedModbus_mgr/gas_monitor.cpp index df0ba03..f0c962c 100644 --- a/src/iedModbus_mgr/gas_monitor.cpp +++ b/src/iedModbus_mgr/gas_monitor.cpp @@ -46,6 +46,12 @@ float c2h6_threshold = C2H6_THRESHOLD; static char H2_alert_flag = 0; static char CO_alert_flag = 0; static char CO2_alert_flag = 0; +static char CH4_alert_flag = 0; +static char C2H2_alert_flag =0; +static char C2H4_alert_flag = 0; +static char C2H6_alert_flag = 0; + + #if 0 //杩欐浠g爜鏆傛椂鐣欑潃澶囦唤 鍙兘鍚庨潰纭疄鐢ㄤ笉鍒颁簡鍐嶅垹 //H2 @@ -281,6 +287,60 @@ void xSet_CO2_alert(const char* CO2_alert) printf("CO2_alert is null,use default\n"); } } + +void xSet_CH4_alert(const char* CH4_alert) +{ + if (CH4_alert) { + CH4_alert_flag = atoi(CH4_alert); + printf("CH4_alert=%s\n", CH4_alert); + printf("CH4_alert_flag=%d\n", CH4_alert_flag); + } + else { + CH4_alert_flag = 0;//涓嶅憡璀 + printf("CH4_alert is null,use default\n"); + } +} + +void xSet_C2H2_alert(const char* C2H2_alert) +{ + if (C2H2_alert) { + C2H2_alert_flag = atoi(C2H2_alert); + printf("C2H2_alert=%s\n", C2H2_alert); + printf("C2H2_alert_flag=%d\n", C2H2_alert_flag); + } + else { + C2H2_alert_flag = 0;//涓嶅憡璀 + printf("C2H2_alert is null,use default\n"); + } +} + +void xSet_C2H4_alert(const char* C2H4_alert) +{ + if (C2H4_alert) { + C2H4_alert_flag = atoi(C2H4_alert); + printf("C2H4_alert=%s\n", C2H4_alert); + printf("C2H4_alert_flag=%d\n", C2H4_alert_flag); + } + else { + C2H4_alert_flag = 0;//涓嶅憡璀 + printf("C2H4_alert is null,use default\n"); + } +} + +void xSet_C2H6_alert(const char* C2H6_alert) +{ + if (C2H6_alert) { + C2H6_alert_flag = atoi(C2H6_alert); + printf("C2H6_alert=%s\n", C2H6_alert); + printf("C2H6_alert_flag=%d\n", C2H6_alert_flag); + } + else { + C2H6_alert_flag = 0;//涓嶅憡璀 + printf("C2H6_alert is null,use default\n"); + } +} + + //杞欢鍚姩鏃剁户鐢靛櫒鐨勯粯璁ょ姸鎬 甯稿紑 void xAll_relay_normal_open() { @@ -389,20 +449,318 @@ void alert_data_compare(ysp_modbus_regs_t ysp_data) uart_signal_arr[CO2][1] = NORMAL_OPEN;//甯稿紑 } } + if (CH4_alert_flag) { + //CH4 + cout << "\n CH4 ch4_threshold = " << ch4_threshold << endl; + cout << " ysp_data.CH4 curr value = " << ysp_data.CH4 << endl; + if (ExceedThreshold(ysp_data.CH4, ch4_threshold)) { + //瓒呰繃闃堝硷紝uart杈撳嚭鎺у埗淇″彿 + cout << "CH4 over" << endl; + cout << "CH4 relay normal close" << endl; + uart_signal_arr[CH4][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[CH4][1] = NORMAL_CLOSE;//甯搁棴 + } + else { + cout << "CH4 not over" << endl; + cout << "CH4 relay normal open" << endl; + uart_signal_arr[CH4][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[CH4][1] = NORMAL_OPEN;//甯稿紑 + } + } + if (C2H2_alert_flag) { + //C2H2 + cout << "\n C2H2 c2h2_threshold = " << c2h2_threshold << endl; + cout << " ysp_data.C2H2 curr value = " << ysp_data.C2H2 << endl; + if (ExceedThreshold(ysp_data.C2H2, c2h2_threshold)) { + //瓒呰繃闃堝硷紝uart杈撳嚭鎺у埗淇″彿 + cout << "C2H2 over" << endl; + cout << "C2H2 relay normal close" << endl; + uart_signal_arr[C2H2][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[C2H2][1] = NORMAL_CLOSE;//甯搁棴 + } + else { + cout << "C2H2 not over" << endl; + cout << "C2H2 relay normal open" << endl; + uart_signal_arr[C2H2][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[C2H2][1] = NORMAL_OPEN;//甯稿紑 + } + } + + if (C2H4_alert_flag) { + //C2H4 + cout << "\n C2H4 c2h4_threshold = " << c2h4_threshold << endl; + cout << " ysp_data.C2H4 curr value = " << ysp_data.C2H4 << endl; + if (ExceedThreshold(ysp_data.C2H4, c2h4_threshold)) { + //瓒呰繃闃堝硷紝uart杈撳嚭鎺у埗淇″彿 + cout << "C2H4 over" << endl; + cout << "C2H4 relay normal close" << endl; + uart_signal_arr[C2H4][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[C2H4][1] = NORMAL_CLOSE;//甯搁棴 + } + else { + cout << "C2H4 not over" << endl; + cout << "C2H4 relay normal open" << endl; + uart_signal_arr[C2H4][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[C2H4][1] = NORMAL_OPEN;//甯稿紑 + } + } + if (C2H6_alert_flag) { + //C2H6 + cout << "\n C2H6 c2h6_threshold = " << c2h6_threshold << endl; + cout << " ysp_data.C2H6 curr value = " << ysp_data.C2H6 << endl; + if (ExceedThreshold(ysp_data.C2H6, c2h6_threshold)) { + //瓒呰繃闃堝硷紝uart杈撳嚭鎺у埗淇″彿 + cout << "C2H6 over" << endl; + cout << "C2H6 relay normal close" << endl; + uart_signal_arr[C2H6][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[C2H6][1] = NORMAL_CLOSE;//甯搁棴 + } + else { + cout << "C2H6 not over" << endl; + cout << "C2H6 relay normal open" << endl; + uart_signal_arr[C2H6][0] = DATA_HAS_CHANGE;//绗竴涓猙yte琛ㄧず閭d釜鏁版嵁闇瑕佽緭鍑轰俊鍙 + uart_signal_arr[C2H6][1] = NORMAL_OPEN;//甯稿紑 + } + } cout << "\n======================" << endl; } +/* 缁х數鍣ㄧ姸鎬佸鐞*/ +void relay_data_program(ysp_modbus_regs_t ysp_data) +{ + INT res = false; + SerialPort usb_serial0; + unsigned char rcv_buf[1024] = { 0 }; + char com_name[50] = { 0 }; + +#if 1 + memset(com_name, 0, sizeof(com_name)); + res = xEnum_relay_comName(com_name);//鑾峰彇缁х數鍣ㄥ搴旂殑涓插彛鍚嶇О + if (res != true) { + printf("not find CH340锛 synchronize signal fail,continue monitor\n"); + return; + } + else { + printf("find CH340 com: %s\n", com_name); + } +#endif + res = usb_serial0.open(com_name, buadrate, 0, 8, 1, NoFlowControl, 1); + if (res != true) { + cout << "open usb uart failed,continue" << endl;//鑰冭檻鍔犻敊璇鐞 + return; + } + + for (int i = 0; i < MAX_ALERT; i++) { + for (int j = 0; j < 2; j++) { + memset(rcv_buf, 0, sizeof(rcv_buf)); + if (DATA_HAS_CHANGE == uart_signal_arr[i][0]) { + + cout << "i ==" << i << endl; + if (NORMAL_CLOSE == uart_signal_arr[i][1]) { + xUnsigned_char_hex_out("uart send", uart_close_cmd_arr[i], 0, 2); + //鍙戦佹寚浠 + res = usb_serial0.send(uart_close_cmd_arr[i], 3); + cout << "send byte = " << res << endl; + } + else if (NORMAL_OPEN == uart_signal_arr[i][1]) { + xUnsigned_char_hex_out("uart send", uart_open_cmd_arr[i], 0, 2); + res = usb_serial0.send(uart_open_cmd_arr[i], 3); + cout << "send byte = " << res << endl; + } + + //鎺ュ彈鍗曠墖鏈哄洖澶 + cout << "try recv..." << endl; + res = usb_serial0.receive(rcv_buf, 5); + cout << "recv byte = " << res << endl; + if (0 != res) { + cout << "recv msg from uart " << com_name << endl; + xUnsigned_char_hex_out("uart recv", rcv_buf, 0, 5); + if (0xEF == (unsigned char)rcv_buf[3]) {//濡傛灉鎴愬姛锛岃繑鍥炲懡浠 + EF 锛孍F鏄鍥涗釜瀛楄妭 + cout << "uart send succ " << com_name << endl; + break;//succ + } + } + else { + cout << "not recv uart msg" << endl; + } + //try again + } + } + + Sleep(50);//姣忎釜淇″彿鏁版嵁鍖呭姞鐐归棿闅 + } + cout << "close uart" << endl; + usb_serial0.close(); +} + +/* Table of CRC values for high篓Corder byte */ +const uint8_t auchCRCHi[] = { +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40 +}; + +/* Table of CRC values for low篓Corder byte */ +const uint8_t auchCRCLo[] = { +0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, +0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, +0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, +0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, +0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, +0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, +0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, +0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, +0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, +0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, +0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, +0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, +0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, +0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, +0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, +0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, +0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, +0x40 +}; + +uint16_t CRCVerify(uint8_t *puchMsg, uint16_t usDataLen) +{ + uint8_t ucCRCHi = 0xFF; /* high byte of CRC initialized */ + uint8_t ucCRCLo = 0xFF; /* low byte of CRC initialized */ + uint16_t uIndex = 0; /* will index into CRC lookup table */ + + while (usDataLen--) /* pass through message buffer */ + { + uIndex = ucCRCHi ^ *puchMsg++; /* calculate the CRC */ + ucCRCHi = ucCRCLo ^ auchCRCHi[uIndex]; + ucCRCLo = auchCRCLo[uIndex]; + } + return (ucCRCHi << 8 | ucCRCLo); +} + +/* 鏁板瓧閲忚浆妯℃嫙閲忔暟鎹鐞*/ +void adc_data_program(ysp_modbus_regs_t ysp_data) +{ + INT res = false; + int i = 0; + uint16_t crccheck=0,uvalue=0; + SerialPort usb_serial0; + unsigned char rcv_buf[1024] = { 0 }; + unsigned char send_buf[1024] = { 0 }; + char com_name[50] = { 0 }; + +#if 1 + memset(com_name, 0, sizeof(com_name)); + res = xEnum_adc_comName(com_name);//鑾峰彇adc鏁版嵁杞崲瀵瑰簲鐨勪覆鍙e悕绉 + if (res != true) { + printf("not find usb serial port锛 synchronize signal fail,continue monitor\n"); + return; + } + else { + printf("find USB Serial Port: %s\n", com_name); + } +#endif + res = usb_serial0.open(com_name, buadrate, 0, 8, 1, NoFlowControl, 1); + if (res != true) { + cout << "open usb uart failed,continue" << endl;//鑰冭檻鍔犻敊璇鐞 + return; + } + + memset(send_buf, 0, sizeof(send_buf)); + i = 0; + send_buf[i++] = 0x01; /*璁惧鍦板潃*/ + send_buf[i++] = 0x10; /*鍔熻兘鐮*/ + send_buf[i++] = 0x00; /*寰呭啓鍏ュ瘎瀛樺櫒璧峰鍦板潃锛屼粠0000寮濮*/ + send_buf[i++] = 0x00; /*寰呭啓鍏ュ瘎瀛樺櫒璧峰鍦板潃锛屼粠0000寮濮*/ + send_buf[i++] = 0x00; /*寰呭啓鍏ュ瘎瀛樺櫒鏁伴噺*/ + send_buf[i++] = 0x08; /*寰呭啓鍏ュ瘎瀛樺櫒鏁伴噺*/ + send_buf[i++] = 0x10; /*鍐欏叆鏁版嵁瀛楄妭闀垮害*/ + /*1銆丠2鍐欏叆鏁版嵁[2-2000]ul/l*/ + uvalue = (uint16_t)(ysp_data.H2/2000*4096);/*鎶奌2娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙2000锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/2000*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*2銆丆O鍐欏叆鏁版嵁[25-5000]ul/l*/ + uvalue = (uint16_t)(ysp_data.CO/5000*4096);/*鎶奀O娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙5000锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/5000*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*3銆丆O2鍐欏叆鏁版嵁[25-15000]ul/l*/ + uvalue = (uint16_t)(ysp_data.CO2/15000*4096);/*鎶奀O2娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙15000锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/15000*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*4銆丆H4鍐欏叆鏁版嵁[0.5-1000]ul/l*/ + uvalue = (uint16_t)(ysp_data.CH4/1000*4096);/*鎶奀H4娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙1000锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/1000*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*5銆丆2H2鍐欏叆鏁版嵁[0.5-1000]ul/l*/ + uvalue = (uint16_t)(ysp_data.C2H2/1000*4096);/*鎶奀2H2娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙1000锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/1000*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*6銆丆2H4鍐欏叆鏁版嵁[0.5-1000]ul/l*/ + uvalue = (uint16_t)(ysp_data.C2H4/1000*4096);/*鎶奀2H4娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙1000锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/1000*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*7銆丆2H6鍐欏叆鏁版嵁[0.5-1000]ul/l*/ + uvalue = (uint16_t)(ysp_data.C2H6/1000*4096);/*鎶奀2H6娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙1000锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/1000*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*8銆亀ater鍐欏叆鏁版嵁[0-100]ul/l*/ + uvalue = (uint16_t)(ysp_data.Water/100*4096);/*鎶奧ater娴偣鏁板硷紝婊¢噺绋4096瀵瑰簲鍊煎煙100锛岃浆鎹㈡垚adc 12浣嶈〃杈炬暣鏁癿=a/100*4096*/ + send_buf[i++] = (u_char)(uvalue >> 8); /**/ + send_buf[i++] = (u_char)uvalue; /**/ + /*crc16鏍¢獙鐮*/ + crccheck = CRCVerify(send_buf, i); + send_buf[i++] = (u_char)(crccheck >> 8); /**/ + send_buf[i++] = (u_char)crccheck; /**/ + xUnsigned_char_hex_out("uart send", send_buf, 0, i); + //鍙戦佹寚浠 + res = usb_serial0.send(send_buf, i); + cout << "send byte = " << res << endl; + //鎺ュ彈鍗曠墖鏈哄洖澶 + cout << "try recv..." << endl; + res = usb_serial0.receive(rcv_buf, 30); + cout << "recv byte = " << res << endl; + if (0 != res) { + cout << "recv msg from uart " << com_name << endl; + xUnsigned_char_hex_out("uart recv", rcv_buf, 0, res); + } + else { + cout << "not recv uart msg" << endl; + } + + Sleep(50);//姣忎釜淇″彿鏁版嵁鍖呭姞鐐归棿闅 + cout << "close uart" << endl; + usb_serial0.close(); +} + DWORD WINAPI Gas_threadProc(LPVOID lp) { char* com = (char*)lp; unsigned char rcv_buf[1024] = {0}; - INT res = false; + INT res = false, i=0; SerialPort usb_serial0; char uart_cmd[1024] = {0}; static char last_signal_CO2 = NOT_OVER_THRESHOLD;//榛樿鏄0,琛ㄧず浣庝簬闃堝 static char last_signal_H2 = NOT_OVER_THRESHOLD;//榛樿鏄0 static char last_signal_CO = NOT_OVER_THRESHOLD;//榛樿鏄0 + static char last_signal_CH4 = NOT_OVER_THRESHOLD; + static char last_signal_C2H2 = NOT_OVER_THRESHOLD; + static char last_signal_C2H4 = NOT_OVER_THRESHOLD; + static char last_signal_C2H6 = NOT_OVER_THRESHOLD; ULONGLONG startTick = 0; char com_name[50] = { 0 }; @@ -419,6 +777,7 @@ DWORD WINAPI Gas_threadProc(LPVOID lp) { cout << "loop monitor period " << (monitor_period) << " mintues" << endl; Sleep(PERIOD_MONITOR(monitor_period));//鍛ㄦ湡澶у皬鍒嗛挓 + //Sleep(3000);//鍛ㄦ湡澶у皬鍒嗛挓 _tprintf(_T("sleep : %I64u ms\n"), GetTickCount64() - startTick); xGet_config_from_configFile();//just for test @@ -502,7 +861,9 @@ DWORD WINAPI Gas_threadProc(LPVOID lp) { cout << "try synchronize all signal to relay"<< endl; #endif -#if 1 + relay_data_program(ysp_data); + adc_data_program(ysp_data); +#if 0 memset(com_name, 0, sizeof(com_name)); res = xEnum_relay_comName(com_name);//鑾峰彇缁х數鍣ㄥ搴旂殑涓插彛鍚嶇О if (res != true) { @@ -513,6 +874,7 @@ DWORD WINAPI Gas_threadProc(LPVOID lp) { printf("find CH340 com: %s\n", com_name); } #endif +#if 0 res = usb_serial0.open(com_name, buadrate, 0, 8, 1, NoFlowControl, 1); if (res != true) { cout << "open usb uart failed,continue" << endl;//鑰冭檻鍔犻敊璇鐞 @@ -560,9 +922,9 @@ DWORD WINAPI Gas_threadProc(LPVOID lp) { } cout << "close uart" << endl; usb_serial0.close(); +#endif } - #if 0 while (1) { for (int i = 0; i < 6; i++) { diff --git a/src/iedModbus_mgr/gas_monitor.h b/src/iedModbus_mgr/gas_monitor.h index 38eb991..737e484 100644 --- a/src/iedModbus_mgr/gas_monitor.h +++ b/src/iedModbus_mgr/gas_monitor.h @@ -29,6 +29,11 @@ void xSet_C2H6_threshold(const char* C2H6_threshold); void xSet_H2_alert(const char* H2_alert); void xSet_CO_alert(const char* CO_alert); void xSet_CO2_alert(const char* CO2_alert); +void xSet_CH4_alert(const char* H2_alert); +void xSet_C2H2_alert(const char* H2_alert); +void xSet_C2H4_alert(const char* H2_alert); +void xSet_C2H6_alert(const char* H2_alert); + int xGas_alert_task_init(char* uart_port); #endif // !_GAS_MONITOR diff --git a/src/iedModbus_mgr/iedModbus_mgr.cpp b/src/iedModbus_mgr/iedModbus_mgr.cpp index fde2add..cb26b6e 100644 --- a/src/iedModbus_mgr/iedModbus_mgr.cpp +++ b/src/iedModbus_mgr/iedModbus_mgr.cpp @@ -29,7 +29,7 @@ int main(int argc, char** argv) xProcess_unique(); - std::cout << "RUN YSP MONITOR APP!\n"; + std::cout << "RUN YSP MONITOR APP! V1.11-x86\n"; #if 1 //com_test 鐢ㄦ潵寮鍙戞祴璇 鍙戝竷鏃朵笉璧蜂綔鐢 if (argc > 1) { @@ -50,5 +50,8 @@ int main(int argc, char** argv) xModbus_task_init();//MODBUS thread xGas_alert_task_init(com_test);//GAS monitor thread - while (1); + while (1) + { + Sleep(1000); + } } diff --git a/src/iedModbus_mgr/iedModbus_mgr.vcxproj b/src/iedModbus_mgr/iedModbus_mgr.vcxproj index fa3c0b8..9c547bd 100644 --- a/src/iedModbus_mgr/iedModbus_mgr.vcxproj +++ b/src/iedModbus_mgr/iedModbus_mgr.vcxproj @@ -29,13 +29,13 @@ Application true - v143 + v141 MultiByte Application false - v143 + v141 true MultiByte diff --git a/src/iedModbus_mgr/yspConfig.ini b/src/iedModbus_mgr/yspConfig.ini index 7dc8a3c..1699994 100644 --- a/src/iedModbus_mgr/yspConfig.ini +++ b/src/iedModbus_mgr/yspConfig.ini @@ -1,6 +1,6 @@ [MODBUS] -SLAVE_IP_ADDR=192.168.1.21 -SLAVE_PORT=1502 +SLAVE_IP_ADDR=192.168.50.154 +SLAVE_PORT=502 [ALERT] H2_THRESHOLD = 120 CO_THRESHOLD=120 @@ -11,7 +11,12 @@ C2H4_THRESHOLD=120 C2H6_THRESHOLD=120 H2_alert=1 CO_alert=1 -CO2_alert=0 +CO2_alert=1 +CH4_alert=1 +C2H2_alert=1 +C2H4_alert=1 +C2H6_alert=1 + [MONITOR] -GAS_MONITOR_PERIOD = 40 +GAS_MONITOR_PERIOD = 1 diff --git a/src/iedModbus_mgr/ysp_modbus_slave.cpp b/src/iedModbus_mgr/ysp_modbus_slave.cpp index de5e77f..74c2c5d 100644 --- a/src/iedModbus_mgr/ysp_modbus_slave.cpp +++ b/src/iedModbus_mgr/ysp_modbus_slave.cpp @@ -32,7 +32,7 @@ int xSlave_info_init() { salve_addr.slave_addr = SERVER_ID; memset(&ysp_modbus_data, 0, sizeof(ysp_modbus_data)); - memcpy(slave_ip_addr, IP_LOCAL_ADDR, strlen(IP_LOCAL_ADDR)); + //memcpy(slave_ip_addr, IP_LOCAL_ADDR, strlen(IP_LOCAL_ADDR)); return 0; }