コード例 #1
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void ReadWriteMultipleRegisters()
        {
            ushort[] readData;
            ushort[] writeData = new ushort[2];
            writeData[0] = Convert.ToUInt16(txtWriteValue.Text);
            writeData[1] = 2222;
            wmnRet _wmnReceiveRet = mbm.ReadWriteMultipleRegisters(Convert.ToByte(txtUnitId.Text),
                                                                   Convert.ToUInt16(txtReadAddress.Text),
                                                                   Convert.ToUInt16(txtReadCount.Text),
                                                                   Convert.ToUInt16(txtWriteAddress.Text),
                                                                   writeData,
                                                                   out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                for (int i = 0; i < readData.Length; i++)
                {
                    txtData.Text += "Address[" + (Convert.ToUInt16(txtReadAddress.Text) + i).ToString() + "] Value: " + readData[i].ToString() + "; \r\n";
                }
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #2
0
        private void btnReadString_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            ushort[] readData;
            // To read a STRING(80) value from PLC, you have to read 40 register(16bit)
            wmnRet _wmnReceiveRet = mbmUdp.ReadInputRegisters(0, 12388, 50, out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                txtData.Text += "Address[" + 12388.ToString() + "] Value: " + wmnConvert.ToString(readData) + "; \r\n";
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
            _wmnReceiveRet = mbmUdp.ReadInputRegisters(0, 12438, 50, out readData);
            if (_wmnReceiveRet.Value == 0)
            {
                txtData.Text += "Address[" + 12438.ToString() + "] Value: " + wmnConvert.ToString(readData) + ";\r\n";
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #3
0
        private void btnWriteString_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            ushort[] writeData      = wmnConvert.ToUInt16(txtStringValue.Text);
            wmnRet   _wmnReceiveRet = mbmUdp.WriteMultipleRegisters(0, 12388, writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #4
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void WriteSingleCoil()
        {
            bool   writeData      = Convert.ToBoolean(txtWriteValue.Text);
            wmnRet _wmnReceiveRet = mbm.WriteSingleCoil(Convert.ToByte(txtUnitId.Text),
                                                        Convert.ToUInt16(txtWriteAddress.Text),
                                                        writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #5
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void WriteSingleRegister()
        {
            ushort writeData      = Convert.ToUInt16(txtWriteValue.Text);
            wmnRet _wmnReceiveRet = mbm.WriteSingleRegister(Convert.ToByte(txtUnitId.Text),
                                                            Convert.ToUInt16(txtWriteAddress.Text),
                                                            writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #6
0
        private void btnWriteBool_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            bool   writeData      = Convert.ToBoolean(cbxBoolValue.Text);
            wmnRet _wmnReceiveRet = mbmUdp.WriteSingleCoil(0, 12288, writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #7
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void btnWriteRegister_Click(object sender, EventArgs e)
        {
            ushort[] writeData = new ushort[1];
            writeData[0] = Convert.ToUInt16(txtWriteValue.Text);
            wmnRet _wmnReceiveRet = mbm.WriteMultipleRegisters(Convert.ToByte(txtSlaveId.Text),
                                                               Convert.ToUInt16(txtWriteAddress.Text),
                                                               writeData);

            txtData.Text = _wmnReceiveRet.Text;;
        }
コード例 #8
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void WriteMultipleRegisters()
        {
            ushort[] writeData = new ushort[1];
            writeData[0] = Convert.ToUInt16(txtWriteValue.Text);
            wmnRet _wmnReceiveRet = mbm.WriteMultipleRegisters(Convert.ToByte(txtUnitId.Text),
                                                               Convert.ToUInt16(txtWriteAddress.Text),
                                                               writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #9
0
        private void btnWriteUdint_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            //ushort[] writeData = wmnConvert.ToUInt16(Convert.ToUInt32(txtUdintValue.Text));
            uint[]   values         = { 14202, 98150, 31470 };
            ushort[] writeData      = wmnConvert.ToUInt16(values);
            wmnRet   _wmnReceiveRet = mbmUdp.WriteMultipleRegisters(0, 12328, writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #10
0
        private void btnWriteReal_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            ushort[] writeData = wmnConvert.ToUInt16(Convert.ToSingle(txtRealValue.Text));
            //float[] values = { 1.42f, 9.81f, 3.14f };
            //ushort[] writeData = wmnConvert.ToUInt16(values);
            wmnRet _wmnReceiveRet = mbmUdp.WriteMultipleRegisters(0, 12348, writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #11
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void MaskWriteRegister()
        {
            ushort andMask = Convert.ToUInt16(txtWriteValue.Text);
            //ushort andMask = 0x0000;
            ushort orMask         = 0x0000;
            wmnRet _wmnReceiveRet = mbm.MaskWriteRegister(Convert.ToByte(txtUnitId.Text),
                                                          Convert.ToUInt16(txtWriteAddress.Text),
                                                          andMask,
                                                          orMask);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #12
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void btnConnect_Click(object sender, EventArgs e)
        {
            wmnRet _wmnReceiveRet = mbm.Connect(txtHost.Text, Convert.ToUInt16(txtPort.Text));

            if (_wmnReceiveRet.Value != 0)
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
            else
            {
                txtData.Text = "Connected";
            }
        }
コード例 #13
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void WriteMultipleCoils()
        {
            bool[] writeData = new bool[36];
            writeData[0] = true;
            writeData[1] = false;
            writeData[2] = false;
            writeData[3] = false;
            writeData[4] = false;
            writeData[5] = false;
            writeData[6] = false;
            writeData[7] = false;

            writeData[8]  = true;
            writeData[9]  = true;
            writeData[10] = true;
            writeData[11] = true;
            writeData[12] = true;
            writeData[13] = true;
            writeData[14] = true;
            writeData[15] = false;

            writeData[16] = true;
            writeData[17] = true;
            writeData[18] = true;
            writeData[19] = true;
            writeData[20] = false;
            writeData[21] = false;
            writeData[22] = false;
            writeData[23] = false;

            writeData[24] = true;
            writeData[25] = true;
            writeData[26] = false;
            writeData[27] = false;
            writeData[28] = true;
            writeData[29] = true;
            writeData[30] = false;
            writeData[31] = false;

            writeData[32] = false;
            writeData[33] = true;
            writeData[34] = false;
            writeData[35] = true;
            wmnRet _wmnReceiveRet = mbm.WriteMultipleCoils(Convert.ToByte(txtUnitId.Text),
                                                           Convert.ToUInt16(txtWriteAddress.Text),
                                                           writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #14
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void GetCommEventCounter()
        {
            ushort status;
            ushort eventCount;
            wmnRet _wmnReceiveRet = mbm.GetCommEventCounter(Convert.ToByte(txtUnitId.Text),
                                                            out status,
                                                            out eventCount);

            if (_wmnReceiveRet.Value == 0)
            {
                txtData.Text += "Status := " + status.ToString() + "; EventCount := " + eventCount.ToString() + "; \r\n";
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #15
0
        private void btnReadWord_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            ushort[] readData;
            wmnRet   _wmnReceiveRet = mbmUdp.ReadInputRegisters(0, 12298, 2, out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                for (int i = 0; i < readData.Length; i++)
                {
                    txtData.Text += "Address[" + (12298 + i).ToString() + "] Value: " + readData[i].ToString() + ";  \r\n";
                }
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #16
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void ReadDiscreteInputs()
        {
            bool[] readData;
            wmnRet _wmnReceiveRet = mbm.ReadDiscreteInputs(Convert.ToByte(txtUnitId.Text),
                                                           Convert.ToUInt16(txtReadAddress.Text),
                                                           Convert.ToUInt16(txtReadCount.Text),
                                                           out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                for (int i = 0; i < readData.Length; i++)
                {
                    txtData.Text += "Address[" + (Convert.ToUInt16(txtReadAddress.Text) + i).ToString() + "] Value: " + readData[i].ToString() + "; \r\n";
                }
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #17
0
        private void btnReadCoils_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            bool[] readData;
            ushort readAddr       = Convert.ToUInt16(txtReadCoilAddress.Text);
            wmnRet _wmnReceiveRet = mbmUdp.ReadCoils(0, readAddr, 32, out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                for (int i = 0; i < readData.Length; i++)
                {
                    txtData.Text += "Address[" + (i).ToString() + "] Value: " + readData[i].ToString() + "; \r\n";
                }
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #18
0
        private void btnWriteCoils_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            bool[] writeData = new bool[32];
            writeData[0]  = true;
            writeData[1]  = false;
            writeData[2]  = true;
            writeData[3]  = false;
            writeData[4]  = true;
            writeData[5]  = false;
            writeData[6]  = true;
            writeData[7]  = false;
            writeData[8]  = true;
            writeData[9]  = true;
            writeData[10] = true;
            writeData[11] = true;
            writeData[12] = true;
            writeData[13] = true;
            writeData[14] = true;
            writeData[15] = true;
            writeData[16] = false;
            writeData[17] = false;
            writeData[18] = false;
            writeData[19] = false;
            writeData[20] = false;
            writeData[21] = false;
            writeData[22] = false;
            writeData[23] = false;
            writeData[24] = true;
            writeData[25] = true;
            writeData[26] = true;
            writeData[27] = true;
            writeData[28] = false;
            writeData[29] = false;
            writeData[30] = false;
            writeData[31] = false;
            wmnRet _wmnReceiveRet = mbmUdp.WriteMultipleCoils(0, 0, writeData);

            txtData.Text = _wmnReceiveRet.Text;
        }
コード例 #19
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void ReadBlock()
        {
            ushort[] readData;
            wmnRet   _wmnReceiveRet = mbm.ReadBlock(Convert.ToByte(txtUnitId.Text),
                                                    Convert.ToUInt16(txtReadAddress.Text),
                                                    Convert.ToUInt16(txtReadCount.Text),
                                                    out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                /*for (int i = 0; i < readData.Length; i++)
                 * {
                 *  txtData.Text += "Address[" + (Convert.ToUInt16(txtReadAddress.Text) + i).ToString() + "] Value: " + readData[i].ToString() + "; \r\n";
                 * }*/
                txtData.Text = "Successful executed";
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #20
0
ファイル: frmMain.cs プロジェクト: Hiroshi-jp/WagoModbusNet
        private void btnRead_Click(object sender, EventArgs e)
        {
            ushort[] readData;
            txtData.Text = "";
            wmnRet _wmnReceiveRet = mbm.ReadInputRegisters(Convert.ToByte(txtSlaveId.Text),
                                                           Convert.ToUInt16(txtReadAddress.Text),
                                                           Convert.ToUInt16(txtReadCount.Text),
                                                           out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                for (int i = 0; i < readData.Length; i++)
                {
                    txtData.Text = txtData.Text + "Address[" + (Convert.ToUInt16(txtReadAddress.Text) + i).ToString() + "] Value: " + readData[i].ToString() + "; \r\n";
                }
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #21
0
        private void btnReadDINT_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            ushort[] readData;
            // To read one DINT(32bit) value from PLC, you have to read two register(16bit)
            wmnRet _wmnReceiveRet = mbmUdp.ReadInputRegisters(0, 12308, 4, out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                // Convert ushort[] into int[]
                int[] data = wmnConvert.ToInt32(readData);
                for (int i = 0; i < data.Length; i++)
                {
                    txtData.Text += "Address[" + (12308 + (2 * i)).ToString() + "] Value: " + data[i].ToString() + "; \r\n";
                }
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
        }
コード例 #22
0
        // The ThreadProc method is called when the thread starts.
        // It loops ten times, writing to the console and yielding
        // the rest of its time slice each time, and then ends.
        public void ThreadProc(object obj)
        {
            ModbusMasterUdp mbmUdpt = new ModbusMasterUdp();

            mbmUdpt.AutoConnect = true;
            mbmUdpt.Port        = 502;
            mbmUdpt.Hostname    = (string)obj;

            bool[] readData;
            wmnRet _wmnReceiveRet = mbmUdpt.ReadCoils(0, 0x3000, 2, out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                for (int i = 0; i < readData.Length; i++)
                {
                    Console.WriteLine(mbmUdpt.Hostname + ":" + mbmUdpt.Port.ToString() + " Address[" + (0x3000 + i).ToString() + "] Value: " + readData[i].ToString() + ";");
                }
            }
            else
            {
                Console.WriteLine(_wmnReceiveRet.Text);
            }
        }
コード例 #23
0
        private void btnReadBool_Click(object sender, EventArgs e)
        {
            txtData.Text    = "";
            mbmUdp.Hostname = txtHost.Text;
            bool[] readData;
            wmnRet _wmnReceiveRet = mbmUdp.ReadCoils(0, 0x3000, 2, out readData);

            if (_wmnReceiveRet.Value == 0)
            {
                for (int i = 0; i < readData.Length; i++)
                {
                    txtData.Text += "Address[" + (0x3000 + i).ToString() + "] Value: " + readData[i].ToString() + "; \r\n";
                }
            }
            else
            {
                txtData.Text = _wmnReceiveRet.Text;
            }
            Thread t1 = new Thread(new ParameterizedThreadStart(ThreadProc));
            Thread t2 = new Thread(new ParameterizedThreadStart(ThreadProc));

            t1.Start(txtHost.Text);
            t2.Start("192.168.1.1");
        }