void sinkPChanged(cdeP prop) { if (MyBaseEngine.GetEngineState().IsSimulated || !IsConnected) { return; } var field = MyModFieldStore.MyMirrorCache.GetEntryByFunc(s => s.PropertyName == prop.Name); if (field == null) { return; } var error = OpenModBus(); if (!string.IsNullOrEmpty(error)) { MyBaseThing.LastMessage = $"{DateTime.Now} - Modbus Device could not be opened: {error}"; TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l1_Error)); return; } try { ushort tMainOffset = (ushort)(TheThing.GetSafePropertyNumber(MyBaseThing, "Offset") + field.SourceOffset); byte tSlaveAddress = (byte)TheThing.GetSafePropertyNumber(MyBaseThing, "SlaveAddress"); int tReadWay = (int)TheThing.GetSafePropertyNumber(MyBaseThing, "ConnectionType"); switch (tReadWay) { case 1: MyModMaster.WriteSingleCoil(tSlaveAddress, tMainOffset, TheCommonUtils.CBool(prop.ToString())); break; default: MyModMaster.WriteSingleRegister(tSlaveAddress, tMainOffset, TheCommonUtils.CUShort(prop.ToString())); break; } } catch (Exception e) { MyBaseThing.LastMessage = $"{DateTime.Now} - Failure during write of modbus property: {e.Message}"; TheBaseAssets.MySYSLOG.WriteToLog(10000, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(MyBaseThing.EngineName, MyBaseThing.LastMessage, eMsgLevel.l1_Error, e.ToString())); } if (!KeepOpen) // races with reader thread, but reader thread will retry/reopen so at most one data point is lost { CloseModBus(); } }