コード例 #1
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //public bool TReadDword(Socket tcpClient, int Address, out byte[] value, int ComNum)
        public byte [] GetTReadDword(PPIReadWritePara readPara, out PPIAddress ppiAddress)
        {
            int  i = 0;
            byte fcs;

            ppiAddress          = new PPIAddress();
            ppiAddress.DAddress = Convert.ToByte(readPara.PlcAddress);
            byte[] TReadByte = ppiAddress.TReadByte;


            readPara.ByteAddress = readPara.ByteAddress * 8;
            TReadByte[28]        = Convert.ToByte(readPara.ByteAddress / 0x10000);
            TReadByte[29]        = Convert.ToByte((readPara.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            TReadByte[30]        = Convert.ToByte(readPara.ByteAddress & 0xff);           //低位,如320H,结果为20;

            for (i = 4, fcs = 0; i < 31; i++)
            {
                fcs += TReadByte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            TReadByte[31] = Convert.ToByte(tt);
            return(TReadByte);
        }
コード例 #2
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public static bool PLCRun(PPIReadWritePara para)
        {
            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            serialPort1.DiscardInBuffer();
            serialPort1.DiscardOutBuffer();

            serialPort1.Write(ppiAddress.RunBytes, 0, ppiAddress.RunBytes.Length);
            string str = ByteHelper.ByteToString(ppiAddress.RunBytes);

            Thread.Sleep(100);
            int recei = serialPort1.ReadByte();

            if (recei == 0xE5)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #3
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //public bool Writebyte(Socket tcpClient, int ByteAddress, Enums.StorageType storageType, int WriteValue, int ComNum)

        public bool Writebyte(PPIReadWritePara para)
        {
            if (para.WriteValue > 255)
            {
                return(false);
            }//最大写入值255
            int  i = 0;
            byte fcs;

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] Wbyte = ppiAddress.Wbyte;

            para.ByteAddress = para.ByteAddress * 8;
            Wbyte[22]        = 0x02; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word
            Wbyte[24]        = 0x01; // Byte 24 为数据个数:这里是 01,一次读一个数据
            if (para.StorageType == Enums.StorageType.V)
            {
                Wbyte[26] = 0x01;
            }
            else
            {
                Wbyte[26] = 0x00;
            }
            Wbyte[27] = (byte)para.StorageType;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            Wbyte[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            Wbyte[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Wbyte[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            Wbyte[32] = 0x04;
            Wbyte[34] = 0x08;


            Wbyte[35] = Convert.ToByte(para.WriteValue);
            for (i = 4, fcs = 0; i < 36; i++)
            {
                fcs += Wbyte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            Wbyte[36] = Convert.ToByte(tt);


            byte[] SendData = ByteHelper.MergerArray(new byte[] { Convert.ToByte(para.ComNum) }, Wbyte);


            return(WriteData(para.TcpClient, SendData, ppiAddress, para.ComNum));
        }
コード例 #4
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public bool WriteBit(PPIReadWritePara writePara)
        {
            if (writePara.WriteValue > 255)
            {
                return(false);
            } //最大写入值255

            int  i = 0;
            byte fcs;

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(writePara.PlcAddress);

            byte[] Wbit = ppiAddress.Wbit;

            writePara.ByteAddress = writePara.ByteAddress * 8 + writePara.Bitnumber;
            Wbit[22] = 0x01; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word

            if (writePara.StorageType == Enums.StorageType.T)
            {
                Wbit[22] = 0x1F;
            }
            if (writePara.StorageType == Enums.StorageType.C)
            {
                Wbit[22] = 0x1E;
            }

            Wbit[24] = 0x01; // Byte 24 为数据个数:这里是 01,一次读一个数据

            if (writePara.StorageType == Enums.StorageType.V)
            {
                Wbit[26] = 0x01;
            }
            else
            {
                Wbit[26] = 0x00;
            }
            Wbit[27] = (byte)writePara.StorageType;
            Wbit[28] = Convert.ToByte(writePara.ByteAddress / 0x10000);
            Wbit[29] = Convert.ToByte((writePara.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Wbit[30] = Convert.ToByte(writePara.ByteAddress & 0xff);           //低位,如320H,结果为20;
            Wbit[35] = Convert.ToByte(writePara.WriteValue);
            for (i = 4, fcs = 0; i < 36; i++)
            {
                fcs += Wbit[i];
            }

            int tt = Convert.ToInt32(fcs) % 256; //添加的代码 mod 256

            Wbit[36] = Convert.ToByte(tt);

            byte[] SendData = ByteHelper.MergerArray(new byte[] { Convert.ToByte(writePara.ComNum) }, Wbit);

            return(WriteData(writePara.TcpClient, SendData, ppiAddress, writePara.ComNum));
        }
コード例 #5
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //读取一个字存储单元的数据
        //public static bool ReadWord(int Address, Enums.StorageType storageType, out byte[] WordValue, int plcAdd)
        public static PPIReadWritePara ReadWord(PPIReadWritePara para)

        {
            int  i = 0;
            byte fcs;

            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }
            para.ByteAddress = para.ByteAddress * 8;

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] Rbyte = ppiAddress.Rbyte;
            Rbyte[22] = 0x04; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word
            Rbyte[24] = 0x01; //一次读取的个数

            if (para.StorageType == Enums.StorageType.V)
            {
                Rbyte[26] = 0x01;
            }
            else
            {
                Rbyte[26] = 0x00;
            }
            Rbyte[27] = (byte)para.StorageType;

            Rbyte[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            Rbyte[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Rbyte[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            for (i = 4, fcs = 0; i < 31; i++)
            {
                fcs += Rbyte[i];
            }
            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            Rbyte[31] = Convert.ToByte(tt);

            byte[] Receives = ReceiveReadByte(serialPort1, Rbyte, ppiAddress);

            if (Receives != null)
            {
                receiveByte    = ByteHelper.ByteToString(Receives);
                para.ReadValue = new byte[2];
                serialPort1.Read(Receives, 0, 30);
                para.ReadValue[0] = Receives[25];
                para.ReadValue[1] = Receives[26];
                receiveByte       = ByteHelper.ByteToString(Receives);
                para.IsSuceess    = true;
            }

            return(para);
        }
コード例 #6
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //public bool ReadDoubleWord(Socket tcpClient, int Address, Enums.StorageType storageType, out byte[] WordValue, int ComNum)
        public PPIReadWritePara ReadDoubleWord(PPIReadWritePara readPara)
        {
            int  i = 0;
            byte fcs;

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(readPara.PlcAddress);
            byte[] Rbyte = ppiAddress.Rbyte;

            readPara.ByteAddress = readPara.ByteAddress * 8;
            Rbyte[22]            = 0x06; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1  Word 06: Double Word
            Rbyte[24]            = 0x01; //一次读取的个数

            if (readPara.StorageType == Enums.StorageType.V)
            {
                Rbyte[26] = 0x01;
            }
            else
            {
                Rbyte[26] = 0x00;
            }
            Rbyte[27] = (byte)readPara.StorageType;

            Rbyte[28] = Convert.ToByte(readPara.ByteAddress / 0x10000);
            Rbyte[29] = Convert.ToByte((readPara.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Rbyte[30] = Convert.ToByte(readPara.ByteAddress & 0xff);           //低位,如320H,结果为20;

            for (i = 4, fcs = 0; i < 31; i++)
            {
                fcs += Rbyte[i];
            }
            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            Rbyte[31] = Convert.ToByte(tt);

            Rbyte = ByteHelper.MergerArray(new byte[] { Convert.ToByte(readPara.ComNum) }, Rbyte);



            byte[] Receives = ReceiveReadByte(readPara.TcpClient, Rbyte, ppiAddress, readPara.ComNum);

            if (Receives != null)
            {
                readPara.IsSuceess = false;
                readPara.ReadValue = new byte[4] {
                    Receives[31], Receives[32], Receives[33], Receives[34]
                };

                receiveByte = ByteHelper.ByteToString(Receives);
                return(readPara);
            }

            readPara.ReadValue = new byte[] { 0 };
            return(readPara);
        }
コード例 #7
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public bool WriteWord(PPIReadWritePara writePara)
        {
            int  i = 0;
            byte fcs;

            writePara.ByteAddress = writePara.ByteAddress * 8;

            PPIAddress ppiAddress = new PPIAddress();

            byte[] Wword = ppiAddress.Wword;

            Wword[22] = 0x04; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 =Word06: Double Word
            Wword[24] = 0x01; // Byte 24 为数据个数:这里是 01,一次读一个数据

            if (writePara.StorageType == Enums.StorageType.V)
            {
                Wword[26] = 0x01;
            }
            else
            {
                Wword[26] = 0x00;
            }
            Wword[27] = (byte)writePara.StorageType;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            Wword[28] = Convert.ToByte(writePara.ByteAddress / 0x10000);
            Wword[29] = Convert.ToByte((writePara.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Wword[30] = Convert.ToByte(writePara.ByteAddress & 0xff);           //低位,如320H,结果为20;

            Wword[32] = 0x04;

            Wword[34] = 0x10;

            Wword[35] = Convert.ToByte(writePara.WriteValue / 256);
            Wword[36] = Convert.ToByte(writePara.WriteValue % 256);
            for (i = 4, fcs = 0; i < Wword.Length - 2; i++)
            {
                fcs += Wword[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            Wword[37] = Convert.ToByte(tt);

            byte[] SendData = ByteHelper.MergerArray(new byte[] { Convert.ToByte(writePara.ComNum) }, Wword);

            return(WriteData(writePara.TcpClient, SendData, ppiAddress, writePara.ComNum));
        }
コード例 #8
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //public static bool TReadDword(int Address, out byte[] value, int plcAdd)

        public static PPIReadWritePara TReadDword(PPIReadWritePara para)
        {
            int  i = 0;
            byte fcs;

            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }
            para.ByteAddress = para.ByteAddress * 8;

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] TReadByte = ppiAddress.TReadByte;
            TReadByte[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            TReadByte[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            TReadByte[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            for (i = 4, fcs = 0; i < 31; i++)
            {
                fcs += TReadByte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            TReadByte[31] = Convert.ToByte(tt);
            //  Rbyte[31] = fcs;

            byte[] Receives = ReceiveReadByte(serialPort1, TReadByte, ppiAddress);

            if (Receives != null)
            {
                para.ReadValue = new byte[4];
                for (int j = 0; j < 4; j++)
                {
                    para.ReadValue[j] = Receives[26 + j];
                }

                receiveByte    = ByteHelper.ByteToString(Receives);
                para.IsSuceess = true;
            }

            return(para);
        }
コード例 #9
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public static bool CWriteWord(PPIReadWritePara para)
        {
            if (para.WriteValue > 65536)
            {
                return(false);
            }//最大写入值0xfffff,4,294,967,295
            int  i = 0;
            byte fcs;

            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }

            para.ByteAddress = para.ByteAddress * 8;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] CwriteWordByte = ppiAddress.CwriteWordByte;
            CwriteWordByte[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            CwriteWordByte[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            CwriteWordByte[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            CwriteWordByte[35] = Convert.ToByte((para.WriteValue / 0x10000) & 0xff);
            CwriteWordByte[36] = Convert.ToByte((para.WriteValue / 0x100) & 0xff);
            CwriteWordByte[37] = Convert.ToByte(para.WriteValue % 256);

            for (i = 4, fcs = 0; i < CwriteWordByte.Length - 2; i++)
            {
                fcs += CwriteWordByte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            CwriteWordByte[CwriteWordByte.Length - 2] = Convert.ToByte(tt);

            return(IsWriteSuccess(CwriteWordByte, serialPort1, ppiAddress));
        }
コード例 #10
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public bool TwriteDWord(PPIReadWritePara writePara)
        {
            //if (writeValue > uint.MaxValue)
            //{
            //    return false;
            //}//最大写入值0xffffffff,4,294,967,295
            int  i = 0;
            byte fcs;

            PPIAddress ppiAddress = new PPIAddress();

            byte[] TWritebyte = ppiAddress.TWritebyte;


            writePara.ByteAddress = writePara.ByteAddress * 8;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            TWritebyte[28] = Convert.ToByte(writePara.ByteAddress / 0x10000);
            TWritebyte[29] = Convert.ToByte((writePara.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            TWritebyte[30] = Convert.ToByte(writePara.ByteAddress & 0xff);           //低位,如320H,结果为20;

            TWritebyte[36] = Convert.ToByte(writePara.WriteValue / 0x1000000);
            TWritebyte[37] = Convert.ToByte((writePara.WriteValue / 0x10000) & 0xff);
            TWritebyte[38] = Convert.ToByte((writePara.WriteValue / 0x100) & 0xff);
            TWritebyte[39] = Convert.ToByte(writePara.WriteValue % 256);

            for (i = 4, fcs = 0; i < TWritebyte.Length - 2; i++)
            {
                fcs += TWritebyte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            TWritebyte[TWritebyte.Length - 2] = Convert.ToByte(tt);


            byte[] SendData = ByteHelper.MergerArray(new byte[] { Convert.ToByte(writePara.ComNum) }, TWritebyte);

            return(WriteData(writePara.TcpClient, SendData, ppiAddress, writePara.ComNum));
        }
コード例 #11
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public static bool TwriteDWord(PPIReadWritePara para)
        {
            //if (writeValue > uint.MaxValue)
            //{
            //    return false;
            //}//最大写入值0xffffffff,4,294,967,295
            int  i = 0;
            byte fcs;


            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }

            para.ByteAddress = para.ByteAddress * 8;

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] TWritebyte = ppiAddress.TWritebyte;

            TWritebyte[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            TWritebyte[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            TWritebyte[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            TWritebyte[36] = Convert.ToByte(para.WriteValue / 0x1000000);
            TWritebyte[37] = Convert.ToByte((para.WriteValue / 0x10000) & 0xff);
            TWritebyte[38] = Convert.ToByte((para.WriteValue / 0x100) & 0xff);
            TWritebyte[39] = Convert.ToByte(para.WriteValue % 256);

            for (i = 4, fcs = 0; i < TWritebyte.Length - 2; i++)
            {
                fcs += TWritebyte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            TWritebyte[TWritebyte.Length - 2] = Convert.ToByte(tt);


            return(IsWriteSuccess(TWritebyte, serialPort1, ppiAddress));
        }
コード例 #12
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //public bool CWriteWord(Socket tcpClient, int byteAddress, int writeValue, int ComNum)
        public bool CWriteWord(PPIReadWritePara para)

        {
            if (para.WriteValue > 65536)
            {
                return(false);
            }//最大写入值0xfffff,4,294,967,295
            int  i = 0;
            byte fcs;

            PPIAddress ppiAddress = new PPIAddress();

            byte[] CwriteWordByte = ppiAddress.CwriteWordByte;
            para.ByteAddress = para.ByteAddress * 8;
            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            CwriteWordByte[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            CwriteWordByte[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            CwriteWordByte[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            CwriteWordByte[35] = Convert.ToByte((para.WriteValue / 0x10000) & 0xff);
            CwriteWordByte[36] = Convert.ToByte((para.WriteValue / 0x100) & 0xff);
            CwriteWordByte[37] = Convert.ToByte(para.WriteValue % 256);

            for (i = 4, fcs = 0; i < CwriteWordByte.Length - 2; i++)
            {
                fcs += CwriteWordByte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            CwriteWordByte[CwriteWordByte.Length - 2] = Convert.ToByte(tt);
            byte[] SendData = ByteHelper.MergerArray(new byte[] { Convert.ToByte(para.ComNum) }, CwriteWordByte);
            return(WriteData(para.TcpClient, SendData, ppiAddress, para.ComNum));
        }
コード例 #13
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public PPIReadWritePara TReadDword(PPIReadWritePara readPara)
        {
            PPIAddress ppiAddress = new PPIAddress();

            byte[] TReadByte = ByteHelper.MergerArray(new byte[] { Convert.ToByte(readPara.ComNum) }, GetTReadDword(readPara, out ppiAddress));


            byte[] Receives = ReceiveReadByte(readPara.TcpClient, TReadByte, ppiAddress, readPara.ComNum);

            if (Receives != null)
            {
                readPara.IsSuceess = true;
                readPara.ReadValue = new byte[4];
                for (int j = 0; j < 4; j++)
                {
                    readPara.ReadValue[j] = Receives[32 + j];
                }
                receiveByte = ByteHelper.ByteToString(Receives);
                return(readPara);
            }
            readPara.ReadValue = new byte[] { 0 };
            return(readPara);
        }
コード例 #14
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //public  bool Readbytes(Socket tcpClient, int Address, Enums.StorageType storageType, out byte[] readValue, int ComNum, int byteCount = 1)

        public PPIReadWritePara Readbytes(PPIReadWritePara readPara)
        {
            if (readPara.ReadCount > 200 || readPara.ReadCount == 0 || readPara.ReadCount < 0)
            {
                readPara.ReadValue = new byte[] { 0 };
                return(readPara);
            }
            int  i = 0;
            byte fcs;

            //byte[] Receives = new byte[34 + byteCount];
            readPara.ByteAddress = readPara.ByteAddress * 8;
            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(readPara.PlcAddress);
            byte[] Rbyte = ppiAddress.Rbyte;
            Rbyte[22] = 0x02;                               //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word
            Rbyte[24] = Convert.ToByte(readPara.ReadCount); //一次读取的个数
            if (readPara.StorageType == Enums.StorageType.V)
            {
                Rbyte[26] = 0x01;
            }
            else
            {
                Rbyte[26] = 0x00;
            }
            Rbyte[27] = (byte)readPara.StorageType;

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            Rbyte[28] = Convert.ToByte(readPara.ByteAddress / 0x10000);
            Rbyte[29] = Convert.ToByte((readPara.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Rbyte[30] = Convert.ToByte(readPara.ByteAddress & 0xff);           //低位,如320H,结果为20;

            for (i = 4, fcs = 0; i < 31; i++)
            {
                fcs += Rbyte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            Rbyte[31] = Convert.ToByte(tt);
            //  Rbyte[31] = fcs;

            Rbyte = ByteHelper.MergerArray(new byte[] { Convert.ToByte(readPara.ComNum) }, Rbyte);

            byte[] Receives = ReceiveReadByte(readPara.TcpClient, Rbyte, ppiAddress, readPara.ComNum);
            if (Receives != null)
            {
                readPara.IsSuceess = true;
                receiveByte        = ByteHelper.ByteToString(Receives);

                readPara.ReadValue = new byte[readPara.ReadCount];
                if (readPara.StorageType == Enums.StorageType.T)
                {
                    for (int j = 0; j < readPara.ReadCount; j++)
                    {
                        readPara.ReadValue[j] = Receives[31 + j];
                    }
                }
                else
                {
                    for (int j = 0; j < readPara.ReadCount; j++)
                    {
                        readPara.ReadValue[j] = Receives[31 + j];
                    }
                }
                return(readPara);
            }

            readPara.ReadValue = new byte[] { 0 };
            return(readPara);
        }
コード例 #15
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        public static bool WriteDWord(PPIReadWritePara para)
        {
            //if (writeValue > uint.MaxValue)
            //{
            //    return false;
            //}//最大写入值0xffffffff,4,294,967,295
            int  i = 0;
            byte fcs;

            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }
            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] WDword = ppiAddress.WDword;

            para.ByteAddress = para.ByteAddress * 8;
            WDword[22]       = 0x06; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word
            WDword[24]       = 0x01; // Byte 24 为数据个数:这里是 01,一次读一个数据


            if (para.StorageType == Enums.StorageType.V)
            {
                WDword[26] = 0x01;
            }
            else
            {
                WDword[26] = 0x00;
            }


            WDword[27] = (byte)para.StorageType;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            WDword[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            WDword[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            WDword[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            WDword[32] = 0x04;
            WDword[34] = 0x20;

            WDword[35] = Convert.ToByte(para.WriteValue / 0x1000000);
            // long hh = (writeValue/0x10000) & 0xff;


            WDword[36] = Convert.ToByte((para.WriteValue / 0x10000) & 0xff);
            WDword[37] = Convert.ToByte((para.WriteValue / 0x100) & 0xff);
            WDword[38] = Convert.ToByte(para.WriteValue % 256);

            for (i = 4, fcs = 0; i < WDword.Length - 2; i++)
            {
                fcs += WDword[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            WDword[WDword.Length - 2] = Convert.ToByte(tt);


            return(IsWriteSuccess(WDword, serialPort1, ppiAddress));
        }
コード例 #16
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //一次写一个 Double Word 类型的数据。写命令是 40 个字节,其余为 38 个字节
        //写一个 Double Word 类型的数据,前面的 0~21 字节为 :68 23 23 68 02 00 6C 32 01 00 00 00 00 00 0E 00 00 04 01 12 0A 10
        //写一个其他类型的数据,前面的 0~21 字节为 :68 21 21 68 02 00 6C 32 01 00 00 00 00 00 0E 00 00 04 01 12 0A 10
        //  68 21 21 68 02 00 6C 32 01 00 00 00 00 00 0E 00 06 05 01 12 0A 10
        //从 22 字节开始根据写入数据的值和位置不同而变化

        //public static bool WriteBit(int ByteAddress, byte bitnumber, Enums.StorageType storageType, int WriteValue, int plcAdd)
        public static bool WriteBit(PPIReadWritePara para)
        {
            if (para.WriteValue > 255)
            {
                return(false);
            }//最大写入值255

            int  i = 0;
            byte fcs;


            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }

            para.ByteAddress = para.ByteAddress * 8 + para.Bitnumber;

            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] Wbit = ppiAddress.Wbit;
            Wbit[22] = 0x01;//Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word

            if (para.StorageType == Enums.StorageType.T)
            {
                Wbit[22] = 0x1F;
            }
            if (para.StorageType == Enums.StorageType.C)
            {
                Wbit[22] = 0x1E;
            }

            Wbit[24] = 0x01;// Byte 24 为数据个数:这里是 01,一次读一个数据

            if (para.StorageType == Enums.StorageType.V)
            {
                Wbit[26] = 0x01;
            }
            else
            {
                Wbit[26] = 0x00;
            }
            Wbit[27] = (byte)para.StorageType;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            Wbit[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            Wbit[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Wbit[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;

            Wbit[35] = Convert.ToByte(para.WriteValue);
            for (i = 4, fcs = 0; i < 36; i++)
            {
                fcs += Wbit[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            Wbit[36] = Convert.ToByte(tt);

            return(IsWriteSuccess(Wbit, serialPort1, ppiAddress));
        }
コード例 #17
0
ファイル: PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //读取某个位的状态
        //public static bool Readbit(int ByteAddress, int bitnumber, Enums.StorageType storageType, out byte[] bitValue, int plcAdd)

        public static PPIReadWritePara Readbit(PPIReadWritePara para)
        {
            para.ReadValue = new byte[1];

            int  i = 0;
            byte fcs;


            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }


            PPIAddress ppiAddress = new PPIAddress();

            ppiAddress.DAddress = Convert.ToByte(para.PlcAddress);
            byte[] Rbyte = ppiAddress.Rbyte;
            para.ByteAddress = para.ByteAddress * 8 + para.Bitnumber;
            Rbyte[22]        = 0x01;//Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word
            if (para.StorageType == Enums.StorageType.T)
            {
                Rbyte[22] = 0x1F;
            }
            if (para.StorageType == Enums.StorageType.C)
            {
                Rbyte[22] = 0x1E;
            }

            Rbyte[24] = 0x01;// Byte 24 为数据个数:这里是 01,一次读一个数据

            if (para.StorageType == Enums.StorageType.V)
            {
                Rbyte[26] = 0x01;
            }
            else
            {
                Rbyte[26] = 0x00;
            }
            Rbyte[27] = (byte)para.StorageType;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            // buffer[11] = Convert.ToByte(address & 0xff);//地位,如320H,结果为20
            //buffer[10] = Convert.ToByte((address / 0x100) & 0xff);//0x100 ->256
            //buffer[9] = Convert.ToByte(address / 0x10000);
            Rbyte[28] = Convert.ToByte(para.ByteAddress / 0x10000);
            Rbyte[29] = Convert.ToByte((para.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Rbyte[30] = Convert.ToByte(para.ByteAddress & 0xff);           //低位,如320H,结果为20;
            for (i = 4, fcs = 0; i < 31; i++)
            {
                fcs += Rbyte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256;//添加的代码 mod 256

            Rbyte[31] = Convert.ToByte(tt);

            byte[] Receives = ReceiveReadByte(serialPort1, Rbyte, ppiAddress);

            if (Receives != null)
            {
                receiveByte = ByteHelper.ByteToString(Receives);

                if (Receives.Length >= 3)
                {
                    para.ReadValue[0] = Receives[Receives.Length - 3];
                    para.IsSuceess    = true;
                }
            }

            return(para);
        }
コード例 #18
0
ファイル: ZLB_PPIHelper.cs プロジェクト: lvwenrui12/PPI
        //读取某个位的状态
        //public  bool Readbit(Socket tcpClient, int ComNum, int ByteAddress, int bitnumber,
        //    Enums.StorageType storageType,
        //    out byte[] bitValue, int plcAddress = 2)

        public PPIReadWritePara Readbit(PPIReadWritePara readPara)
        {
            readPara.ReadValue = new byte[1];
            #region 字符串拼接

            PPIAddress ppiAddress = new PPIAddress();
            ppiAddress.DAddress = Convert.ToByte(readPara.PlcAddress);

            byte[] Rbyte = ppiAddress.Rbyte;

            int  i = 0;
            byte fcs;

            readPara.ByteAddress = readPara.ByteAddress * 8 + readPara.Bitnumber;
            Rbyte[22]            = 0x01; //Byte 22 为读取数据的长度,01: 1 Bit 02: 1 Byte 04: 1 Word 06: Double Word
            if (readPara.StorageType == Enums.StorageType.T)
            {
                Rbyte[22] = 0x1F;
            }
            if (readPara.StorageType == Enums.StorageType.C)
            {
                Rbyte[22] = 0x1E;
            }

            Rbyte[24] = 0x01; // Byte 24 为数据个数:这里是 01,一次读一个数据

            if (readPara.StorageType == Enums.StorageType.V)
            {
                Rbyte[26] = 0x01;
            }
            else
            {
                Rbyte[26] = 0x00;
            }
            Rbyte[27] = (byte)readPara.StorageType;

            //偏移量,byte 28,29,30 存储器偏移量指针 (存储器地址 *8 ):
            //  如 VB100,存储器地址为 100,偏移量指针为 800,转换成 16
            //进制就是 320H,则 Byte 28~29 这三个字节就是: 00 03 20

            Rbyte[28] = Convert.ToByte(readPara.ByteAddress / 0x10000);
            Rbyte[29] = Convert.ToByte((readPara.ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Rbyte[30] = Convert.ToByte(readPara.ByteAddress & 0xff);           //低位,如320H,结果为20;
            for (i = 4, fcs = 0; i < 31; i++)
            {
                fcs += Rbyte[i];
            }

            int tt = Convert.ToInt32(fcs) % 256; //添加的代码 mod 256

            Rbyte[31] = Convert.ToByte(tt);

            byte[] SendData = ByteHelper.MergerArray(new byte[] { Convert.ToByte(readPara.ComNum) }, Rbyte);

            //Wbit = new byte[] { 0 };

            #endregion

            //接收到的数据:AA 00 02 44 03 05 E5 A5
            string str = ByteHelper.ByteToString(SendData);

            byte[] Receives = ReceiveReadByte(readPara.TcpClient, SendData, ppiAddress, readPara.ComNum);

            //if (Receives != null)
            //{
            //    readPara.IsSuceess = true;

            //    readPara.ReadValue[0] = Receives[Receives.Length - 4];

            //    receiveByte = ByteHelper.ByteToString(Receives);
            //}

            return(readPara);
        }