增加数字量转模拟量功能,完善tcpmodbus

main
huyizhong 12 months ago
parent 808ac5fbc2
commit 69df2f0ca8

@ -34,7 +34,7 @@ void xUnsigned_char_hex_out(const char *msg, unsigned char* src, int start, int
printf("%s:\n", msg); printf("%s:\n", msg);
} }
for (int i = start; i < end + 1; i++) { for (int i = start; i < end + 1; i++) {
printf("%02X", src[i]); printf("%02X ", src[i]);
} }
printf("\n"); printf("\n");
@ -146,6 +146,26 @@ int xEnum_relay_comName(char* com_name)
return false; return false;
} }
//枚举找到使用的串口线对应的端口号
int xEnum_adc_comName(char* com_name)
{
const char* serial_description = "USB Serial Port";//项目中adc转换使用的串口芯片描述符
vector<SerialPortInfo> 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() int xGet_config_from_configFile()
{ {
#if 1 #if 1
@ -196,24 +216,32 @@ int xGet_config_from_configFile()
const char* CO_threshold = ini_get(ini_ctl, "ALERT", "CO_THRESHOLD"); const char* CO_threshold = ini_get(ini_ctl, "ALERT", "CO_THRESHOLD");
xSet_CO_threshold(CO_threshold); xSet_CO_threshold(CO_threshold);
const char* CO_alert = ini_get(ini_ctl, "ALERT", "CO_alert"); 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"); const char* CO2_threshold = ini_get(ini_ctl, "ALERT", "CO2_THRESHOLD");
xSet_CO2_threshold(CO2_threshold); xSet_CO2_threshold(CO2_threshold);
const char* CO2_alert = ini_get(ini_ctl, "ALERT", "CO2_alert"); 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"); const char* CH4_threshold = ini_get(ini_ctl, "ALERT", "CH4_THRESHOLD");
xSet_CH4_period(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"); const char* C2H2_threshold = ini_get(ini_ctl, "ALERT", "C2H2_THRESHOLD");
xSet_C2H2_threshold(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"); const char* C2H4_threshold = ini_get(ini_ctl, "ALERT", "C2H4_THRESHOLD");
xSet_C2H4_period(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"); const char* C2H6_threshold = ini_get(ini_ctl, "ALERT", "C2H6_THRESHOLD");
xSet_C2H6_threshold(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"); const char* process_delay_min = ini_get(ini_ctl, "PROCESS", "DELAY_RUN_MIN");

@ -1,10 +1,13 @@
#ifndef _COMMON_H_ #ifndef _COMMON_H_
#define _COMMON_H_ #define _COMMON_H_
#pragma once #pragma once
#include <stdint.h>
void xDelay_run_process(); void xDelay_run_process();
int xGet_config_from_configFile(); int xGet_config_from_configFile();
void xUnsigned_char_hex_out(const char* msg, unsigned char* src, int start, int end); void xUnsigned_char_hex_out(const char* msg, unsigned char* src, int start, int end);
int xEnum_relay_comName(char* com_name); int xEnum_relay_comName(char* com_name);
int xEnum_adc_comName(char* com_name);
uint16_t CRCVerify(uint8_t *puchMsg, uint16_t usDataLen);
#endif #endif

@ -46,6 +46,12 @@ float c2h6_threshold = C2H6_THRESHOLD;
static char H2_alert_flag = 0; static char H2_alert_flag = 0;
static char CO_alert_flag = 0; static char CO_alert_flag = 0;
static char CO2_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 //这段代码暂时留着备份 可能后面确实用不到了再删 #if 0 //这段代码暂时留着备份 可能后面确实用不到了再删
//H2 //H2
@ -281,6 +287,60 @@ void xSet_CO2_alert(const char* CO2_alert)
printf("CO2_alert is null,use default\n"); 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() 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;//常开 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;//第一个byte表示那个数据需要输出信号
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;//第一个byte表示那个数据需要输出信号
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;//第一个byte表示那个数据需要输出信号
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;//第一个byte表示那个数据需要输出信号
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;//第一个byte表示那个数据需要输出信号
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;//第一个byte表示那个数据需要输出信号
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;//第一个byte表示那个数据需要输出信号
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;//第一个byte表示那个数据需要输出信号
uart_signal_arr[C2H6][1] = NORMAL_OPEN;//常开
}
}
cout << "\n======================" << endl; 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 EF是第四个字节
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数据转换对应的串口名称
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、H2写入数据[2-2000]ul/l*/
uvalue = (uint16_t)(ysp_data.H2/2000*4096);/*把H2浮点数值满量程4096对应值域2000转换成adc 12位表达整数m=a/2000*4096*/
send_buf[i++] = (u_char)(uvalue >> 8); /**/
send_buf[i++] = (u_char)uvalue; /**/
/*2、CO写入数据[25-5000]ul/l*/
uvalue = (uint16_t)(ysp_data.CO/5000*4096);/*把CO浮点数值满量程4096对应值域5000转换成adc 12位表达整数m=a/5000*4096*/
send_buf[i++] = (u_char)(uvalue >> 8); /**/
send_buf[i++] = (u_char)uvalue; /**/
/*3、CO2写入数据[25-15000]ul/l*/
uvalue = (uint16_t)(ysp_data.CO2/15000*4096);/*把CO2浮点数值满量程4096对应值域15000转换成adc 12位表达整数m=a/15000*4096*/
send_buf[i++] = (u_char)(uvalue >> 8); /**/
send_buf[i++] = (u_char)uvalue; /**/
/*4、CH4写入数据[0.5-1000]ul/l*/
uvalue = (uint16_t)(ysp_data.CH4/1000*4096);/*把CH4浮点数值满量程4096对应值域1000转换成adc 12位表达整数m=a/1000*4096*/
send_buf[i++] = (u_char)(uvalue >> 8); /**/
send_buf[i++] = (u_char)uvalue; /**/
/*5、C2H2写入数据[0.5-1000]ul/l*/
uvalue = (uint16_t)(ysp_data.C2H2/1000*4096);/*把C2H2浮点数值满量程4096对应值域1000转换成adc 12位表达整数m=a/1000*4096*/
send_buf[i++] = (u_char)(uvalue >> 8); /**/
send_buf[i++] = (u_char)uvalue; /**/
/*6、C2H4写入数据[0.5-1000]ul/l*/
uvalue = (uint16_t)(ysp_data.C2H4/1000*4096);/*把C2H4浮点数值满量程4096对应值域1000转换成adc 12位表达整数m=a/1000*4096*/
send_buf[i++] = (u_char)(uvalue >> 8); /**/
send_buf[i++] = (u_char)uvalue; /**/
/*7、C2H6写入数据[0.5-1000]ul/l*/
uvalue = (uint16_t)(ysp_data.C2H6/1000*4096);/*把C2H6浮点数值满量程4096对应值域1000转换成adc 12位表达整数m=a/1000*4096*/
send_buf[i++] = (u_char)(uvalue >> 8); /**/
send_buf[i++] = (u_char)uvalue; /**/
/*8、water写入数据[0-100]ul/l*/
uvalue = (uint16_t)(ysp_data.Water/100*4096);/*把Water浮点数值满量程4096对应值域100转换成adc 12位表达整数m=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) { DWORD WINAPI Gas_threadProc(LPVOID lp) {
char* com = (char*)lp; char* com = (char*)lp;
unsigned char rcv_buf[1024] = {0}; unsigned char rcv_buf[1024] = {0};
INT res = false; INT res = false, i=0;
SerialPort usb_serial0; SerialPort usb_serial0;
char uart_cmd[1024] = {0}; char uart_cmd[1024] = {0};
static char last_signal_CO2 = NOT_OVER_THRESHOLD;//默认是0,表示低于阈值 static char last_signal_CO2 = NOT_OVER_THRESHOLD;//默认是0,表示低于阈值
static char last_signal_H2 = 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_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; ULONGLONG startTick = 0;
char com_name[50] = { 0 }; char com_name[50] = { 0 };
@ -419,6 +777,7 @@ DWORD WINAPI Gas_threadProc(LPVOID lp) {
cout << "loop monitor period " << (monitor_period) << " mintues" << endl; cout << "loop monitor period " << (monitor_period) << " mintues" << endl;
Sleep(PERIOD_MONITOR(monitor_period));//周期大小分钟 Sleep(PERIOD_MONITOR(monitor_period));//周期大小分钟
//Sleep(3000);//周期大小分钟
_tprintf(_T("sleep : %I64u ms\n"), GetTickCount64() - startTick); _tprintf(_T("sleep : %I64u ms\n"), GetTickCount64() - startTick);
xGet_config_from_configFile();//just for test 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; cout << "try synchronize all signal to relay"<< endl;
#endif #endif
#if 1 relay_data_program(ysp_data);
adc_data_program(ysp_data);
#if 0
memset(com_name, 0, sizeof(com_name)); memset(com_name, 0, sizeof(com_name));
res = xEnum_relay_comName(com_name);//获取继电器对应的串口名称 res = xEnum_relay_comName(com_name);//获取继电器对应的串口名称
if (res != true) { if (res != true) {
@ -513,6 +874,7 @@ DWORD WINAPI Gas_threadProc(LPVOID lp) {
printf("find CH340 com: %s\n", com_name); printf("find CH340 com: %s\n", com_name);
} }
#endif #endif
#if 0
res = usb_serial0.open(com_name, buadrate, 0, 8, 1, NoFlowControl, 1); res = usb_serial0.open(com_name, buadrate, 0, 8, 1, NoFlowControl, 1);
if (res != true) { if (res != true) {
cout << "open usb uart failed,continue" << endl;//考虑加错误处理 cout << "open usb uart failed,continue" << endl;//考虑加错误处理
@ -560,9 +922,9 @@ DWORD WINAPI Gas_threadProc(LPVOID lp) {
} }
cout << "close uart" << endl; cout << "close uart" << endl;
usb_serial0.close(); usb_serial0.close();
#endif
} }
#if 0 #if 0
while (1) { while (1) {
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {

@ -29,6 +29,11 @@ void xSet_C2H6_threshold(const char* C2H6_threshold);
void xSet_H2_alert(const char* H2_alert); void xSet_H2_alert(const char* H2_alert);
void xSet_CO_alert(const char* CO_alert); void xSet_CO_alert(const char* CO_alert);
void xSet_CO2_alert(const char* CO2_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); int xGas_alert_task_init(char* uart_port);
#endif // !_GAS_MONITOR #endif // !_GAS_MONITOR

@ -29,7 +29,7 @@ int main(int argc, char** argv)
xProcess_unique(); xProcess_unique();
std::cout << "RUN YSP MONITOR APP!\n"; std::cout << "RUN YSP MONITOR APP! V1.11-x86\n";
#if 1 //com_test 用来开发测试 发布时不起作用 #if 1 //com_test 用来开发测试 发布时不起作用
if (argc > 1) { if (argc > 1) {
@ -50,5 +50,8 @@ int main(int argc, char** argv)
xModbus_task_init();//MODBUS thread xModbus_task_init();//MODBUS thread
xGas_alert_task_init(com_test);//GAS monitor thread xGas_alert_task_init(com_test);//GAS monitor thread
while (1); while (1)
{
Sleep(1000);
}
} }

@ -29,13 +29,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>MultiByte</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>MultiByte</CharacterSet>
</PropertyGroup> </PropertyGroup>

@ -1,6 +1,6 @@
[MODBUS] [MODBUS]
SLAVE_IP_ADDR=192.168.1.21 SLAVE_IP_ADDR=192.168.50.154
SLAVE_PORT=1502 SLAVE_PORT=502
[ALERT] [ALERT]
H2_THRESHOLD = 120 H2_THRESHOLD = 120
CO_THRESHOLD=120 CO_THRESHOLD=120
@ -11,7 +11,12 @@ C2H4_THRESHOLD=120
C2H6_THRESHOLD=120 C2H6_THRESHOLD=120
H2_alert=1 H2_alert=1
CO_alert=1 CO_alert=1
CO2_alert=0 CO2_alert=1
CH4_alert=1
C2H2_alert=1
C2H4_alert=1
C2H6_alert=1
[MONITOR] [MONITOR]
GAS_MONITOR_PERIOD = 40 GAS_MONITOR_PERIOD = 1

@ -32,7 +32,7 @@ int xSlave_info_init()
{ {
salve_addr.slave_addr = SERVER_ID; salve_addr.slave_addr = SERVER_ID;
memset(&ysp_modbus_data, 0, sizeof(ysp_modbus_data)); 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; return 0;
} }

Loading…
Cancel
Save