private void connectButton_Click(object sender, EventArgs e) { short[] values = new short[16]; if (connectButton.Text == "Connect") { if (deviceBox.Text == "") { MessageBox.Show("Please select device com port"); } else { if (mb.Open(deviceBox.Text, 9600, 8, Parity.None, StopBits.One)) { Enable_Panel(); connectButton.Text = "Disconnect"; pleaseclickLabel.Text = ""; actionTextBox.Text += "TM-012P is connected\r\n"; pvLabel.ForeColor = Color.Turquoise; outputLabel.ForeColor = Color.LightBlue; cojLabel.ForeColor = Color.Turquoise; _connected = true; threadstart(); Verifyuser_config(); } else { MessageBox.Show("Failed to connect device or device com port not availble", "Communication failed", MessageBoxButtons.OK); } } } else { //device_serialPort.Close(); if (mb.Close()) { Disble_Panel(); _connected = false; _mbisread = false; //threadstart(); prodIDLabel.Text = ""; connectButton.Text = "Connect"; pleaseclickLabel.Text = "Please click connect...."; actionTextBox.Text += "TM-012P is disconnected\r\n"; swLabel.Text = ""; pvLabel.ForeColor = Color.DarkGray; outputLabel.ForeColor = Color.DarkGray; cojLabel.ForeColor = Color.DarkGray; } else { MessageBox.Show("Can not close communication, device might be unplugged", "Communication failed", MessageBoxButtons.OK); } } }
public bool WriteRegister(ushort reg, short value) { modbus link = new modbus(); bool ret = link.Open(port, 9600, 8, Parity.None, StopBits.Two); if (!ret) { throw EngineMessageException.offline( "Opening port " + port + " failed: " + link.modbusStatus); } short[] val = { value }; Logger.log( "Writing 0x" + value.ToString("x") + " to register 0x" + reg.ToString("x") + " to address 0x" + address.ToString("x")); ret = link.SendFc16(address, reg, 1, val); if (!ret) { throw new EngineMessageException( "Writing 0x" + value.ToString("x") + " to register 0x" + reg.ToString("x") + " failed: " + link.modbusStatus); } link.Close(); return(ret); }
private string FetchValueFromPlc(WebSocketSession session, string e) { HashMap obj = JsonConvert.DeserializeObject <HashMap>(e); int slaveMachineID = obj.GetValue <int>("SlaveID"); byte address = (byte)slaveMachineID; ushort start = obj.GetValue <ushort>("start"); ushort registers = obj.GetValue <ushort>("registers"); short[] values = new short[registers]; int waitTime = obj.GetValue <int>("waitTime"); String subscribe = obj.GetValue <String>("SubscribeWay"); modbus.SubscribeWay = subscribe; modbus.WaitTime = waitTime; modbus md = new modbus(); md.SendFc3NewVersion(address, start, registers, ref values); if (!String.IsNullOrEmpty(subscribe) && subscribe == "发送之后阻塞直到获取返回") { md.Close(); } return(values.ToString()); }
/* para de pesquisar */ private void StopPoll() { isPolling = false; timer.Stop(); mb.Close(); btnStart.Enabled = true; lblStatus.Text = mb.modbusStatus; Grava_Log(lblStatus.Text); }
public short ReadRegister(ushort reg) { modbus link = new modbus(); link.Open(port, 9600, 8, Parity.None, StopBits.Two); short[] val = new short[1]; bool ret = link.SendFc3(address, reg, 1, ref val); if (!ret) { throw new EngineMessageException( "Read from register 0x" + reg.ToString("x") + " failed: " + link.modbusStatus); } link.Close(); return(val[0]); }