Exemple #1
0
        public bool IsWriteSuccess(byte[] Receives, PPIAddress ppiAddress, int ComNum)
        {
            byte[] ReceivesCheck = ByteHelper.MergerArray(new byte[] { Convert.ToByte(ComNum) }, ppiAddress.WriteReceivesCheck);

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

            ReceivesCheck = GetSendData(ReceivesCheck);

            int n = 0;

            for (int j = 0; j < ReceivesCheck.Length; j++)
            {
                if (Receives[j] != ReceivesCheck[j])
                {
                    n++;
                    break;
                }
            }
            if (n == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #2
0
        }//读取失败

        //读取一个字节存储单元的数据,长度29
        public static bool Readbyte(int Address, Enums.StorageType storageType, out int readValue, int plcAdd)
        {
            int  i = 0;
            byte fcs;

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

            PPIAddress ppiAddress = new PPIAddress();

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

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

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


            Rbyte[28] = Convert.ToByte(Address / 0x10000);
            Rbyte[29] = Convert.ToByte((Address / 0x100) & 0xff); //0x100 ->256;
            Rbyte[30] = Convert.ToByte(Address & 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;

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

            if (Receives != null)
            {
                receiveByte = ByteHelper.ByteToString(Receives);
                readValue   = (int)Receives[25];

                return(true);
            }

            else
            {
                readValue = 0;
                return(false);
            }
        }
Exemple #3
0
        //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);
        }
Exemple #4
0
        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);
            }
        }
Exemple #5
0
        //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));
        }
Exemple #6
0
        //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);
        }
Exemple #7
0
        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));
        }
Exemple #8
0
        //读取一个字存储单元的数据
        //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);
        }
Exemple #9
0
        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));
        }
Exemple #10
0
        public byte[] ReceiveReadByte(TcpClient tcpClient, byte[] Rbyte, PPIAddress ppiAddress, int ComNum)
        {
            byte[] Receives = new byte[100];
            Rbyte = GetSendData(Rbyte);

            sendCmd = ByteHelper.ByteToString(Rbyte);

            byte[] receivesAffirm = new byte[8];
            try
            {
                tcpClient.Client.Send(Rbyte);

                int n = tcpClient.Client.Receive(receivesAffirm);
                if (n > 0)
                {
                    if (receivesAffirm[5] == Convert.ToByte(ComNum) && receivesAffirm[6] == ppiAddress.confirm)
                    {
                        Rbyte = ByteHelper.MergerArray(new byte[] { Convert.ToByte(ComNum) }, ppiAddress.Affirm);

                        Rbyte = GetSendData(Rbyte);

                        tcpClient.Client.Send(Rbyte);

                        int m = tcpClient.Client.Receive(Receives);

                        if (m > 0)
                        {
                            int ReceiveDataCount = 0;
                            for (int i = Receives.Length - 1; i >= 0; i--)
                            {
                                if (Receives[i] != 0)
                                {
                                    ReceiveDataCount = i;
                                    break;
                                }
                            }
                            byte[] ReceivesResult = new byte[ReceiveDataCount + 1];
                            Array.Copy(Receives, 0, ReceivesResult, 0, ReceiveDataCount + 1);

                            return(ReceivesResult);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return(null);
        }
Exemple #11
0
        public static bool IsWriteSuccess(byte[] SendData, SerialPort port, PPIAddress ppiAddress)
        {
            serialPort1.DiscardInBuffer();
            serialPort1.DiscardOutBuffer();

            serialPort1.Write(SendData, 0, SendData.Length);

            sendCmd = ByteHelper.ByteToString(SendData);
            Thread.Sleep(100);

            int Rece = serialPort1.ReadByte();

            if (Rece == 0xE5)
            {
                port.DiscardInBuffer();
                port.DiscardOutBuffer();

                port.Write(ppiAddress.Affirm, 0, ppiAddress.Affirm.Length);
                TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);

                PortTimeout(ppiAddress.WriteReceivesCheck.Length, ts1, port);

                byte[] Receives = new byte[ppiAddress.WriteReceivesCheck.Length];
                port.Read(Receives, 0, Receives.Length);
                receiveByte = ByteHelper.ByteToString(Receives);
                string strcheck = ByteHelper.ByteToString(ppiAddress.WriteReceivesCheck);

                int n = 0;
                for (int j = 0; j < 24; j++)
                {
                    if (Receives[j] != ppiAddress.WriteReceivesCheck[j])
                    {
                        n++;
                        break;
                    }
                }
                if (n == 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemple #12
0
        //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);
        }
Exemple #13
0
        public bool WriteData(TcpClient tcpClient, byte[] SendData, PPIAddress ppiAddress, int ComNum)
        {
            SendData = GetSendData(SendData);

            sendCmd = ByteHelper.ByteToString(SendData);

            bool flag = false;

            byte[] receivesAffirm = new byte[8];
            try
            {
                tcpClient.Client.Send(SendData);

                int n = tcpClient.Client.Receive(receivesAffirm);
                if (n > 0)
                {
                    if (receivesAffirm[5] == Convert.ToByte(ComNum) && receivesAffirm[6] == ppiAddress.confirm)
                    {
                        SendData = ByteHelper.MergerArray(new byte[] { Convert.ToByte(ComNum) }, ppiAddress.Affirm);

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

                        SendData = GetSendData(SendData);

                        tcpClient.Client.Send(SendData);

                        string str = ByteHelper.ByteToString(SendData);

                        byte[] Receives = new byte[ppiAddress.WriteReceivesCheck.Length + 7];
                        int    m        = tcpClient.Client.Receive(Receives);

                        if (m > 0)
                        {
                            flag        = IsWriteSuccess(Receives, ppiAddress, ComNum);
                            receiveByte = ByteHelper.ByteToString(Receives);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }



            return(flag);
        }
Exemple #14
0
        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));
        }
Exemple #15
0
        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));
        }
Exemple #16
0
        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));
        }
Exemple #17
0
        public static byte[] ReceiveReadByte(SerialPort sPort, byte[] sendData, PPIAddress ppiAddress)
        {
            byte[] Receives = new byte[100];
            sPort.DiscardInBuffer();
            sPort.DiscardOutBuffer();
            sPort.Write(sendData, 0, sendData.Length);

            sendCmd = ByteHelper.ByteToString(sendData);
            ReadTimeOut(sPort, 1, 1);


            if (sPort.BytesToRead > 0)
            {
                if (0xe5 == sPort.ReadByte())
                {
                    sPort.DiscardInBuffer();
                    sPort.DiscardOutBuffer();

                    sPort.Write(ppiAddress.Affirm, 0, ppiAddress.Affirm.Length);
                }
                ReadTimeOut(sPort, 1, 20);
                sPort.Read(Receives, 0, Receives.Length);


                int ReceiveDataCount = 0;
                for (int i = Receives.Length - 1; i >= 0; i--)
                {
                    if (Receives[i] != 0)
                    {
                        ReceiveDataCount = i;
                        break;
                    }
                }
                byte[] ReceivesResult = new byte[ReceiveDataCount + 1];
                Array.Copy(Receives, 0, ReceivesResult, 0, ReceiveDataCount + 1);

                receiveByte = ByteHelper.ByteToString(ReceivesResult);
                return(ReceivesResult);
            }

            return(null);
        }
Exemple #18
0
        //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));
        }
Exemple #19
0
        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);
        }
Exemple #20
0
        //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);
        }
Exemple #21
0
        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));
        }
Exemple #22
0
        //一次写一个 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));
        }
Exemple #23
0
        //读取某个位的状态
        //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);
        }
Exemple #24
0
        //读取某个位的状态
        //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);
        }
Exemple #25
0
        public static bool Readbits(int ByteAddress, int bitnumber, Enums.StorageType storageType, out byte[] bitValue, int plcAdd, int bitCount = 1)//测试失败
        {
            if (bitCount > 255 || bitCount == 0 || bitCount < 0)
            {
                bitValue = new byte[] { 0 };
                return(false);
            }

            int  i = 0;
            byte fcs;

            //byte[] Receives = new byte[27 + bitCount];

            //if (storageType == Enums.StorageType.T)
            //{
            //    Receives = new byte[31 + bitCount];
            //}

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

            PPIAddress ppiAddress = new PPIAddress();

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

            Rbyte[24] = Convert.ToByte(bitCount);// Byte 24 为数据个数:这里是 01,一次读一个数据

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

            Rbyte[28] = Convert.ToByte(ByteAddress / 0x10000);
            Rbyte[29] = Convert.ToByte((ByteAddress / 0x100) & 0xff); //0x100 ->256;
            Rbyte[30] = Convert.ToByte(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);
                bitValue    = new byte[bitCount];
                for (int j = 0; j < bitCount; j++)
                {
                    bitValue[j] = Receives[24 + j];
                }

                return(true);
            }

            else
            {
                bitValue = new byte[] { 0 };
                return(false);
            }
        }//读取失败