void readAI(modbus_t *ctx) { int i, j, k, tmp; unsigned long YrMin; unsigned short YrMs; SOEINFO Soeinfo; UBYTE ClockArray[9]; struct itimerspec timerValues; struct itimerspec timerValuesold; timerValues.it_value.tv_sec = 0; timerValues.it_value.tv_nsec = 0; timerValues.it_interval.tv_sec = 0; timerValues.it_interval.tv_nsec = 0; timer_settime(AIcmdflag.timer, 0, &timerValues, &timerValuesold); printf("readAI.........\n"); // printf("AI.timer time %d %d \n",timerValuesold.it_value.tv_sec,timerValuesold.it_interval.tv_sec); int StartIndex = 0; int RespondAddr = 0; int SaveIndex = 0; int RespondValue = 0; int16_t val[1024]; for (i = 0; i < modbusRTU.RTUnum; i++) { modbus_set_slave(ctx, modbusRTU.RTU[i]); for (j = 0; j < 3; j++) { if (modbus_read_registers(ctx, StartIndex, 17, val) >= 0) { SaveDiValue(modbusRTU.RTU[i] - 1, 1); if (RtuStatu[i] == 0) { printf("[SF266F]:线路%d上线\n", modbusRTU.RTU[i]); Soeinfo.Value = 1; Soeinfo.Index = modbusRTU.RTU[i] - 1; GetCurrentTime(ClockArray); ArraryToRTC(ClockArray, (PDWORD)&Soeinfo.minutes, (PWORD)&Soeinfo.msec); SendSoeMsg(&Soeinfo); RtuStatu[i] = 1; } for (k = 0; k < 17; k++) { SaveAiValue(k + (modbusRTU.RTU[i] - 1) * 17, val[k] * 1000); if (printflag == 255) { printf("线路号:%d,SaveAiValue index = %d , value = %d !\n", modbusRTU.RTU[i], k + (modbusRTU.RTU[i] - 1) * 17, val[k]); } } break; } else { if (errno == EINTR) { printf("stoped by singal"); } } usleep(100000); } if (j >= 3) { SaveDiValue(modbusRTU.RTU[i] - 1, 0); if (RtuStatu[i] == 1) { printf("[SF266F]:线路%d下线\n", modbusRTU.RTU[i]); Soeinfo.Value = 0; Soeinfo.Index = modbusRTU.RTU[i] - 1; GetCurrentTime(ClockArray); ArraryToRTC(ClockArray, (PDWORD)&Soeinfo.minutes, (PWORD)&Soeinfo.msec); SendSoeMsg(&Soeinfo); RtuStatu[i] = 0; } printf("[ST266F]:RTU %d is offline\n", modbusRTU.RTU[i]); } } timerValuesold.it_value.tv_sec = timerValuesold.it_interval.tv_sec; timerValuesold.it_value.tv_nsec = timerValuesold.it_interval.tv_nsec; timer_settime(AIcmdflag.timer, 0, &timerValuesold, NULL); // printf("AIcmdflag.timer %x time %d\n",AIcmdflag.timer,timerValuesold.it_interval.tv_sec); } // 修改时间 // modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *src) 功能码0x10
时间: 2024-04-25 20:26:17 浏览: 160
c语言 modbus库文件
这段代码看起来是一个 C 语言程序,使用了 libmodbus 库进行 Modbus RTU 通信,实现了读取多个设备的 AI 值,并将其保存到数组中。其中,每次读取都会尝试三次,如果三次都读取失败,则将该设备的状态设置为下线,并发送 SOE 消息。此外,代码中还有一个修改时间的函数,使用了功能码 0x10 进行写入操作。
阅读全文