Exemplo n.º 1
0
        bool WriteModbusData(modbusrtu ModbusRtu, byte SlaveAddr, ushort ModbusAddr, string datatype, string sValue)
        {
            ushort ntype = (ushort)(ModbusAddr / 10000);
            ushort naddr = (ushort)(ModbusAddr % 10000);
            bool   bok   = false;

            naddr = (ushort)(naddr - 1);
            switch (ntype)
            {
            case 0:    //DO
                short[] coils = new short[1];
                if (sValue.Equals("1"))
                {
                    coils[0] = 1;
                }
                else
                {
                    coils[0] = 0;
                }
                bok = ModbusRtu.SendFc5(SlaveAddr, naddr, coils[0]);
                break;

            case 4:    //AO
                if (datatype.Equals("uint16"))
                {
                    ushort[] registerhold = new ushort[1];
                    registerhold[0] = ushort.Parse(sValue);
                    bok             = ModbusRtu.SendFc16(SlaveAddr, naddr, 1, registerhold);
                }
                else if (datatype.Equals("int16"))
                {
                    ushort[] registerhold = new ushort[1];
                    registerhold[0] = (ushort)short.Parse(sValue);
                    bok             = ModbusRtu.SendFc16(SlaveAddr, naddr, 1, registerhold);
                }
                else if (datatype.Equals("float"))
                {
                    ushort[] registerhold = new ushort[2];
                    int      intValue     = int.Parse(sValue);
                    registerhold[1] = (ushort)(intValue >> 16);
                    registerhold[0] = (ushort)intValue;
                    bok             = ModbusRtu.SendFc16(SlaveAddr, naddr, 2, registerhold);
                }
                else if (datatype.Equals("uint32"))
                {
                    ushort[] registerhold = new ushort[2];
                    int      intValue     = int.Parse(sValue);
                    registerhold[1] = (ushort)(intValue >> 16);
                    registerhold[0] = (ushort)intValue;
                    bok             = ModbusRtu.SendFc16(SlaveAddr, naddr, 2, registerhold);
                }
                else if (datatype.Equals("int32"))
                {
                    ushort[] registerhold = new ushort[2];
                    int      intValue     = int.Parse(sValue);
                    registerhold[1] = (ushort)(intValue >> 16);
                    registerhold[0] = (ushort)intValue;
                    bok             = ModbusRtu.SendFc16(SlaveAddr, naddr, 2, registerhold);
                }
                break;
            }
            return(bok);
        }
Exemplo n.º 2
0
        void ReadModbusData(modbusrtu ModbusRtu, byte SlaveAddr, ushort ModbusAddr, DataType datatype, ushort nNumber, ref string[] sValue)
        {
            ushort ntype = (ushort)((ModbusAddr / 10000));
            ushort naddr = (ushort)((ModbusAddr % 10000));

            naddr = (ushort)(naddr - 1);
            switch (ntype)
            {
            case 0:    //DO
                ushort[] coils  = new ushort[nNumber];
                bool     bcoils = ModbusRtu.SendFc1(SlaveAddr, naddr, nNumber, ref coils);
                if (bcoils)
                {
                    for (int i = 0; i < nNumber; i++)
                    {
                        sValue[i] = coils[i].ToString();
                    }
                }
                else
                {
                    for (int i = 0; i < nNumber; i++)
                    {
                        sValue[i] = "0";
                    }
                }
                break;

            case 1:    //DI
                ushort[] dis  = new ushort[nNumber];
                bool     bdis = ModbusRtu.SendFc2(SlaveAddr, naddr, nNumber, ref dis);
                if (bdis)
                {
                    for (int i = 0; i < nNumber; i++)
                    {
                        sValue[i] = dis[i].ToString();
                    }
                }
                else
                {
                    for (int i = 0; i < nNumber; i++)
                    {
                        sValue[i] = "0";
                    }
                }
                break;

            case 4:    //AO
                if (datatype.Equals("uint16"))
                {
                    ushort[] registerhold = new ushort[nNumber];
                    bool     bhold        = ModbusRtu.SendFc3(SlaveAddr, naddr, nNumber, ref registerhold);
                    if (bhold)
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            sValue[i] = registerhold[i].ToString();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            sValue[i] = "0";
                        }
                    }
                }
                else if (datatype.Equals("int16"))
                {
                    ushort[] registerhold = new ushort[nNumber];
                    bool     bhold        = ModbusRtu.SendFc3(SlaveAddr, naddr, nNumber, ref registerhold);
                    if (bhold)
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            sValue[i] = ((short)registerhold[i]).ToString();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            sValue[i] = "0";
                        }
                    }
                }
                else if (datatype.Equals("float"))
                {
                    ushort[] registerhold = new ushort[2 * nNumber];
                    bool     bhold        = ModbusRtu.SendFc3(SlaveAddr, naddr, (ushort)(2 * nNumber), ref registerhold);
                    if (bhold)
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            int intValue = (int)registerhold[i * 2 + 1];
                            intValue <<= 16;
Fc4(SlaveAddr, naddr, (ushort)(2 * nNumber), ref registerinput);
                    if (binput)
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            int intValue = (int)registerinput[2 * i + 1];
                            intValue <<= 16;
                            intValue  += (int)registerinput[2 * i + 0];
                            sValue[i]  = BitConverter.ToSingle(BitConverter.GetBytes(intValue), 0).ToString();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            sValue[i] = "0";
                        }
                    }
                }
                else if (datatype.Equals("int32"))
                {
                    ushort[] registerinput = new ushort[2 * nNumber];
                    bool     binput        = ModbusRtu.SendFc4(SlaveAddr, naddr, (ushort)(2 * nNumber), ref registerinput);
                    if (binput)
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            int intValue = (int)registerinput[2 * i + 1];
                            intValue <<= 16;
                            intValue  += (int)registerinput[2 * i + 0];
                            sValue[i]  = intValue.ToString();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            sValue[i] = "0";
                        }
                    }
                }
                else if (datatype.Equals("uint32"))
                {
                    ushort[] registerinput = new ushort[2 * nNumber];
                    bool     binput        = ModbusRtu.SendFc4(SlaveAddr, naddr, (ushort)(2 * nNumber), ref registerinput);
                    if (binput)
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            UInt32 intValue = (UInt32)registerinput[2 * i + 1];
                            intValue <<= 16;
                            intValue  += (UInt32)registerinput[2 * i + 0];
                            sValue[i]  = intValue.ToString();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < nNumber; i++)
                        {
                            sValue[i] = "0";
                        }
                    }
                }
                break;
            }
            Invoke(showInfo, new object[] { ModbusRtu.modbusStatus, 1 });
        }

        bool WriteModbusData(modbusrtu ModbusRtu, byte SlaveAddr, ushort ModbusAddr, string datatype, string sValue)
        {
            ushort ntype = (ushort)(ModbusAddr / 10000);
            ushort naddr = (ushort)(ModbusAddr % 10000);
            bool   bok   = false;

            naddr = (ushort)(naddr - 1);
            switch (ntype)
            {
            case 0:    //DO
                short[] coils = new short[1];
                if (sValue.Equals("1"))
                {
                    coils[0] = 1;
                }
                else
                {
                    coils[0] = 0;
                }
                bok = ModbusRtu.SendFc5(SlaveAddr, naddr, coils[0]);
                break;

            case 4:    //AO
                if (datatype.Equals("uint16"))
                {
                    ushort[] registerhold = new ushort[1];
                    registerhold[0] = ushort.Parse(sValue);
                    bok             = ModbusRtu.SendFc16(SlaveAddr, naddr, 1, registerhold);
                }
                else if (datatype.Equals("int16"))
                {
                    ushort[] registerhold = new ushort[1];
                    registerhold[0] = (ushort)short.Parse(sValue);
                    bok             =Graph vs = new VisualGraph.VisualGraph();
                        vs.Name              = pagename;
                        vs.Width             = 800;
                        vs.Height            = 480;
                        vs.MouseClick       += new MouseEventHandler(vs_MouseClick);
                        vs.MouseDoubleClick += new MouseEventHandler(vs_MouseDoubleClick);
                        TabPage page = new TabPage();
                        page.Name = pagename;
                        page.Text = pagename;
                        page.Tag  = pagename;
                        page.Controls.Add(vs);
                        tabMain.TabPages.Add(page);
                        tabMain.SelectTab(pagename);
                        BinaryFormatter BinaryRead = new BinaryFormatter();
                        vs.ObjList = (ObjList)BinaryRead.Deserialize(fs);
                        //设置每个图元的lock和runmode属性
                        int count = vs.ObjList.Count();
                        for (int i = 0; i < count; i++)
                        {
                            DrawObject o = (DrawObject)vs.ObjList[i];
                            o.RunMode = true;
                            o.Lock    = true;
                        }
                        vs.BackGroundColor = (Color)BinaryRead.Deserialize(fs);;
                        fs.Close();
                        vs.Invalidate();
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString(), " Save Page error.");
            }
        }