我在写入 Schneider Modicon M221 PLC 时遇到问题。 我正在使用 Swift wrapper LibModbus周围库(在 C 中)。 我可以读取位和寄存器没有问题 - 只是似乎无法写入它们 - 尽管我从设备收到“成功”响应。
这就像使用函数 05 将“true”写入位 0 一样简单。
Swift(4) 调用:
@objc func writeToPLC() {
swiftLibModbus.writeBit(address: 0, status: true,
success: {
print("writeBit Success")
},
failure: { (error:NSError) in
print("Error in WriteBit")
})
}
在 Swift/ObjC SwiftLibModbus 包装器中写入此函数:
func writeBit(address: Int32, status: Bool, success: @escaping () -> Void, failure: @escaping (NSError) -> Void) {
modbusQueue?.async {
if modbus_write_bit(self.mb!, address, status ? 1 : 0) >= 0 {
DispatchQueue.main.async {
success()
}
} else {
let error = self.buildNSError(errno: errno)
DispatchQueue.main.async {
failure(error)
}
}
}
}
写入库中的这些 C 函数:
int modbus_write_bit(modbus_t *ctx, int addr, int status){
return write_single(ctx, _FC_WRITE_SINGLE_COIL, addr,
status ? 0xFF00 : 0);
}
然后:
static int write_single(modbus_t *ctx, int function, int addr, int value){
int rc;
int req_length;
uint8_t req[_MIN_REQ_LENGTH];
req_length = ctx->backend->build_request_basis(ctx, function, addr, value, req);
rc = send_msg(ctx, req, req_length);
if (rc > 0) {
/* Used by write_bit and write_register */
uint8_t rsp[_MIN_REQ_LENGTH];
//printf("Print %s, %s, %d ", req, rsp, rc);
rc = _modbus_receive_msg(ctx, rsp, MSG_CONFIRMATION);
if (rc == -1)
return -1;
rc = check_confirmation(ctx, req, rsp, rc);
}
return rc;}
_FC_WRITE_SINGLE_COIL常量为0x05
_MIN_REQ_LENGTH 常量为 12
运行时控制台显示“writeBit Success”,但位状态保持不变。
开始怀疑它是否是 Schneider 的专有产品。
其他人有这方面的经验吗?
最佳答案
关于ios - 将线圈(位)写入 Modbus PLC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47616167/