private void Button_MouseClick(object sender, MouseEventArgs e) { this.PeriodicRefreshTimer.Enabled = false; ModbusRTU.SuspendRead((byte)_ReadFunctionCode, _ReadAddress, _ReadDataLengthInBit);//鼠标单击本控件后,进入写寄存器状态,暂停寄存器的读操作 switch (_ButtonClickAction) { case ModbusButtonClickActionEnum.Set: ModbusRTU.MasterDataRepos.Coils[_WriteAddress] = true; break; case ModbusButtonClickActionEnum.Reset: ModbusRTU.MasterDataRepos.Coils[_WriteAddress] = false; break; case ModbusButtonClickActionEnum.Toggle: ModbusRTU.MasterDataRepos.Coils[_WriteAddress] = !ModbusRTU.MasterDataRepos.Coils[_WriteAddress]; break; default: break; } ModbusRTU.VoteToConfirmTransmitRegs('+', (byte)_WriteFunctionCode, _WriteAddress, _WriteDataLengthInBit); ModbusRTU.AssembleRequestADU(1, ModbusRTU.LoadUnmannedBuses((byte)_WriteFunctionCode, 0)); ModbusRTU.ResumeRead((byte)_ReadFunctionCode, _ReadAddress, _ReadDataLengthInBit);//鼠标单击本控件后,进入写寄存器状态,暂停寄存器的读操作 PeriodicRefreshTimer.Enabled = true; }
public MainForm() { InitializeComponent(); WorkSpcaeSystemInfoForm.MdiParent = this; WorkSpcaeFaultInfoForm.MdiParent = this; WorkSpcaeWaveformForm.MdiParent = this; WorkSpaceWorkModeForm.MdiParent = this; treeView1.ExpandAll(); ModbusRTU.IsMaster = true; ModbusRTU.AssembleRequestADU(1, ModbusRTU.LoadUnmannedBuses((byte)ModbusRTU.ModbusFuncCode.ReadStorageRegs, 8)); ModbusRTU.AssembleRequestADU(1, ModbusRTU.LoadUnmannedBuses((byte)ModbusRTU.ModbusFuncCode.ReadInputRegs, 8)); ModbusRTU.AssembleRequestADU(1, ModbusRTU.LoadUnmannedBuses((byte)ModbusRTU.ModbusFuncCode.ReadCoils, 8)); }
private void ModbusView_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) //如果按下了Enter键,则进入发数据状态 { if (UInt16.TryParse(this.Text, out ModbusRTU.MasterDataRepos.StorageRegs[_WriteAddress]) == false) //TryParse 函数如果判断出所转换的字符串与所对应的数据格式不对应,会以0值out出来 { this.Text = "Wrong Input"; return; } ; // 需要改进,增加容错能力 考虑使用uint16.tryparse() ModbusRTU.VoteToConfirmTransmitRegs('+', (byte)_WriteFunctionCode, _WriteAddress, _WriteDataLengthInWord); ModbusRTU.AssembleRequestADU(1, ModbusRTU.LoadUnmannedBuses((byte)_WriteFunctionCode, 0)); HideCaret(this.Handle); PeriodicRefreshTimer.Enabled = true; } if (e.KeyChar == (char)Keys.Escape)//通过Esc键取消写操作,恢复周期性读数据状态 { ModbusRTU.ResumeRead((byte)_ReadFunctionCode, _ReadAddress, _ReadDataLengthInWord); HideCaret(this.Handle); PeriodicRefreshTimer.Enabled = true; } }