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);
            }
Exemple #2
0
        private void sendBtn_Click(object sender, EventArgs e)
        {
            string[] values = data.Text.Split(' ');
            UInt16   addr   = Convert.ToUInt16(values[0]);
            bool     ret;

            if (values.Length > 1)
            {
                short[] val = { Convert.ToInt16(values[1]) };
                ret = link.SendFc16(1, addr, 1, val);
                if (ret)
                {
                    output.Text += "Successfully wrote ";
                }
                else
                {
                    output.Text += "Failed to write ";
                }
                output.Text += values[1] + " to address " + values[0] + "\n";
            }
            else
            {
                short[] val = new short[1];
                ret = link.SendFc3(1, addr, 1, ref val);
                if (ret)
                {
                    output.Text += "Read from " + values[0] + ": " + val[0].ToString() + "\n";
                }
                else
                {
                    output.Text += "Read from " + values[0] + " failed\n";
                }
            }
            if (!ret)
            {
                output.Text += "Status: " + link.modbusStatus + "\n";
            }
        }