public async static Task ChangeSpeed(ushort speed) { using (Modbus modbus = new Modbus()) { modbus.Open("COM1", 9600, 8, Parity.Even, StopBits.One); ushort[] va = new ushort[1] { (ushort)(speed * 100) }; ushort retryTime = 10; while (retryTime > 0 && await modbus.WriteRegisters(Inverter.Address, (ushort)InverterFuncRegister.FrequencyCommand, 1, va) != true) { await Task.Delay(100); retryTime--; } if (retryTime == 0) { throw new Exception("更改转速失败,请重试或检查变频器工作是否正常"); } else { Speed = speed; } } }
public async static Task GetRunstate() { using (Modbus modbus = new Modbus()) { //等待主轴启动 await Task.Delay(1500); modbus.Open("COM1", 9600, 8, Parity.Even, StopBits.One); ushort retryTime = 10; ushort[] value = new ushort[1] { 1 }; while (retryTime > 0 && await modbus.ReadRegisters(Inverter.Address, (ushort)InverterFuncRegister.RunState, 1, value) != true) { await Task.Delay(100); } if (retryTime == 0) { throw new Exception(modbus.modbusStatus); } else { if ((value[0] & (0x01 << 12)) != 0) IsEnabled = true; else IsEnabled = false; } } }