Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
 public override void Bind(AssemblyNameReference anref)
 {
     base.Bind(anref);
     if (anref != null)
     {
         AssemblyCulture.Text   = anref.Culture;
         Major.Value            = anref.Version.Major;
         Minor.Value            = anref.Version.Minor;
         Build.Value            = anref.Version.Build;
         Revision.Value         = anref.Version.Revision;
         PublicKey.Text         = ByteHelper.ByteToString(anref.PublicKey);
         PublicKeyToken.Text    = ByteHelper.ByteToString(anref.PublicKeyToken);
         Hash.Text              = ByteHelper.ByteToString(anref.Hash);
         Algorithm.SelectedItem = anref.HashAlgorithm;
     }
     else
     {
         AssemblyCulture.Text    = string.Empty;
         Major.Value             = 0;
         Minor.Value             = 0;
         Build.Value             = 0;
         Revision.Value          = 0;
         PublicKey.Text          = string.Empty;
         PublicKeyToken.Text     = string.Empty;
         Hash.Text               = string.Empty;
         Algorithm.SelectedIndex = -1;
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="data">接收到的数据</param>
        /// <param name="key">自定义key UDP发送TCP指令判断设备使用</param>
        /// <returns>需要发送的数据</returns>
        private byte[] MessageHandling(byte[] data, out string key)
        {
            key = "";
            byte[] Senddata = null;
            try
            {
                #region 解析命令
                if (data.Length > 0)
                {
                    string getD = ByteHelper.ByteToString(data);
                    switch (data[2])
                    {
                    case 0x93:
                        //对时命令
                        TxtLogHelper.WriteLog($"请求内容:{getD}", "TCP");
                        ConsoleWriteLine("接收到的请求内容:" + getD);
                        Senddata = TimeCheck(data);
                        break;

                    case 0x91:
                        //设备数据域上传
                        TxtLogHelper.WriteLog($"请求内容:{getD}", "TCP", 1);
                        ConsoleWriteLine("接收到的请求内容:" + getD);
                        Senddata = ModbusUpload(data);
                        key      = data[10].ToString("X");
                        TxtLogHelper.WriteLog($"获取到的key:{key}", "TCP", 1);
                        break;

                    case 0x84:
                        //设备注册命令
                        TxtLogHelper.WriteLog($"请求内容:{getD}", "TCP");
                        ConsoleWriteLine("接收到的请求内容:" + getD);
                        Senddata = Register(data);
                        break;

                    case 0x89:
                        TxtLogHelper.WriteLog($"请求内容:{getD}", "TCP", 1);
                        ConsoleWriteLine("接收到的请求内容:" + getD);
                        //上传报警设定值
                        Senddata = Register(data);
                        break;

                    default:
                        ConsoleWriteLine("接收到的请求内容:" + getD, 2);
                        TxtLogHelper.WriteLog($"请求内容:{getD}", "TCP");
                        //设备回复服务器消息
                        ReplyMessage(data);
                        break;
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                key      = "";
                Senddata = null;
                TxtLogHelper.WriteLog("解析客户端上报命令异常" + ex.Message, "TCP");
            }
            return(Senddata);
        }
Exemplo n.º 5
0
 /// <summary>
 /// PublicKey update
 /// </summary>
 /// <param name="sender">Updater object</param>
 /// <param name="e">parameters</param>
 private void PublicKey_Validated(object sender, EventArgs e)
 {
     if (ByteHelper.ByteToString(Item.PublicKey) != PublicKey.Text)
     {
         Item.PublicKey = ByteHelper.StringToByte(PublicKey.Text);
         Bind(Item);
     }
 }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        private void PublicKey_Validated(object sender, EventArgs e)
        {
            if (Item == null || ByteHelper.ByteToString(Item.PublicKey) == PublicKey.Text)
            {
                return;
            }

            Item.PublicKey = ByteHelper.StringToByte(PublicKey.Text);
            Bind(Item);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 对时命令
        /// </summary>
        private byte[] TimeCheck(byte[] data)
        {
            DateTime daydate  = DateTime.Now;
            string   bates01  = "93," + int.Parse(daydate.ToString("yy")).ToString("X") + "," + daydate.Month.ToString("X") + "," + daydate.Day.ToString("X") + "," + WeekHelper.ConvertDateToWeek(daydate).ToString("00") + "," + daydate.Hour.ToString("X") + "," + daydate.Minute.ToString("X") + "," + daydate.Second.ToString("X");
            string   batesstr = "7b,7b," + ByteHelper.ByteToString(ByteHelper.TenToHex(bates01)).Trim(' ').Replace(' ', ',') + "," + ByteHelper.CRC16_String(ByteHelper.TenToHex(bates01)) + ",7d,7d";

            byte[] SendData = ByteHelper.TenToHex(batesstr);//固定值
            TxtLogHelper.WriteLog("对时回复命令内容:" + batesstr, "TCP");
            ConsoleWriteLine("对时回复命令内容:" + batesstr, 1);
            return(SendData);
        }
Exemplo n.º 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);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 设备注册命令
        /// </summary>
        private byte[] Register(byte[] data)
        {
            //string xlh = ByteHelper.HexToTen(ByteHelper.ByteToString(data, 3, 20, "D5"));
            //string kh = ByteHelper.HexToTen(ByteHelper.ByteToString(data, 23, 30));
            string xhqd  = ByteHelper.HexToTen(ByteHelper.ByteToString(data, 53, 1));
            string gjbb1 = ByteHelper.HexToTen(ByteHelper.ByteToString(data, 54, 2));
            string gjbb2 = ByteHelper.HexToTen(ByteHelper.ByteToString(data, 56, 2));
            string gjbb3 = ByteHelper.HexToTen(ByteHelper.ByteToString(data, 58, 2));
            string sjjg  = ByteHelper.HexToTen(ByteHelper.ByteToString(data, 60, 1));

            //AShebeiRepository sbrep = new AShebeiRepository();
            //a_shebei sbmodel = sbrep.SearchModelByXlh(Hex2Ten(xlh));
            //if (sbmodel == null)
            //{
            //    sbmodel = new a_shebei();
            //    sbmodel.xlh = Hex2Ten(xlh);
            //    sbmodel.kh = Hex2Ten(kh);
            //    sbmodel.xhqd = int.Parse(Hex2Ten(xhqd));
            //    sbmodel.gjbb1 = Hex2Ten(gjbb1);
            //    sbmodel.gjbb2 = Hex2Ten(gjbb2);
            //    sbmodel.gjbb3 = Hex2Ten(gjbb3);
            //    sbmodel.scjg = int.Parse(Hex2Ten(sjjg));
            //    sbmodel.cjsj = DateTime.Now.ToString("yyyy-MM-dd");
            //    sbmodel.ipdz = clientipe.Address.ToString() + ":" + clientipe.Port.ToString();
            //    int num = sbrep.Insert(sbmodel);
            //    //TxtLogHelper.WriteLog("设备注册添加数据返回值:" + num, 0);
            //}
            //else
            //{
            //    sbmodel.xlh = Hex2Ten(xlh);
            //    sbmodel.kh = Hex2Ten(kh);
            //    sbmodel.xhqd = int.Parse(Hex2Ten(xhqd));
            //    sbmodel.gjbb1 = Hex2Ten(gjbb1);
            //    sbmodel.gjbb2 = Hex2Ten(gjbb2);
            //    sbmodel.gjbb3 = Hex2Ten(gjbb3);
            //    sbmodel.scjg = int.Parse(Hex2Ten(sjjg));
            //    sbmodel.xgsj = DateTime.Now.ToString("yyyy-MM-dd");
            //    sbmodel.ipdz = clientipe.Address.ToString() + ":" + clientipe.Port.ToString();
            //    if (sbrep.Edit(sbmodel))
            //    {
            //        //TxtLogHelper.WriteLog("设备注册修改数据成功", 0);
            //    }
            //    else
            //    {
            //        //TxtLogHelper.WriteLog("设备注册修改数据失败", 0);
            //    }
            //}
            TxtLogHelper.WriteLog("设备注册回复命令内容:" + "7b 7b 84 bf 23 7d 7d", "TCP");
            ConsoleWriteLine("设备注册回复命令内容:7b 7b 84 bf 23 7d 7d", 1);
            return(ByteHelper.StringToByte("7b 7b 84 bf 23 7d 7d"));//固定值
        }
Exemplo n.º 12
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);
            }
        }
Exemplo n.º 13
0
 public override void Bind(LinkedResource res)
 {
     base.Bind(res);
     if (res != null)
     {
         Filename.Text = res.File;
         Hash.Text     = ByteHelper.ByteToString(res.Hash);
     }
     else
     {
         Filename.Text = string.Empty;
         Hash.Text     = string.Empty;
     }
 }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        private void ButFromFile_Click(object sender, EventArgs e)
        {
            if (OpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Filename.Text = Path.GetFileName(OpenFileDialog.FileName);
                    Filename_Validated(this, EventArgs.Empty);

                    Hash.Text = ByteHelper.ByteToString(CryptoService.ComputeHash(OpenFileDialog.FileName));
                    Hash_Validated(this, EventArgs.Empty);
                } catch (Exception)
                {
                }
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 18
0
        private void ButFromFile_Click(object sender, EventArgs e)
        {
            if (OpenFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            try
            {
                Filename.Text = Path.GetFileName(OpenFileDialog.FileName);
                Filename_Validated(this, EventArgs.Empty);

                Hash.Text = ByteHelper.ByteToString(CryptoService.ComputeHash(OpenFileDialog.FileName));
                Hash_Validated(this, EventArgs.Empty);
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch
            {
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="data">接收到的数据</param>
        /// <param name="TcpSendData">需要发送的TCP数据 未null则不发送</param>
        /// <param name="key">自定义key 发送TCP指令判断设备使用</param>
        /// <returns>需要回复的UDP数据 未null则不发送</returns>
        private byte[] MessageHandling(byte[] data, out byte[] TcpSendData, out string key)
        {
            key         = "";
            TcpSendData = null;
            byte[] UdpSenddata = null;
            try
            {
                #region 解析数据
                if (data != null && data.Length > 0)
                {
                    string getD = ByteHelper.ByteToString(data);
                    Console.WriteLine("UDP客户端请求内容:" + getD);
                    TxtLogHelper.WriteLog("客户端请求内容:" + getD, "UDP");

                    switch (data[2])
                    {
                    case 0X90:
                        #region Modbus数据下发
                        key = data[3].ToString("X");
                        TxtLogHelper.WriteLog("获取到的key:" + key, "UDP");
                        #endregion
                        break;
                    }

                    TxtLogHelper.WriteLog("服务器下发控制命令:" + getD, "UDP");
                    tcp.ConsoleWriteLine("服务器下发控制命令:" + getD, 3);
                    TcpSendData = ByteHelper.StringToByte(getD);
                }
                #endregion
            }
            catch (Exception ex)
            {
                key         = "";
                TcpSendData = null;
                UdpSenddata = null;
                TxtLogHelper.WriteLog("解析客户端上报命令异常" + ex.Message, "UDP");
            }
            return(UdpSenddata);
        }
Exemplo n.º 20
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);
        }
Exemplo n.º 21
0
        private void ComponentGroup(Control ctr)
        {
            #region Button

            if (ctr is Button)
            {
                Btn btn = new Btn((Button)ctr);

                byte[] btnPara = btn.ParaToByte();
                btnByteList.Add(btnPara);
                strSendByte += ByteHelper.ByteToString(btnPara);
            }
            #endregion

            #region textBox
            if (ctr is TextBox)
            {
                TxtBox txtBox = new TxtBox((TextBox)ctr);

                textByteList.Add(txtBox.ParaToByte());
                strSendByte += ByteHelper.ByteToString(textPara);
            }
            #endregion
        }
Exemplo n.º 22
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);
        }
Exemplo n.º 23
0
        /// <summary>
        /// 设备数据域上传
        /// </summary>
        private byte[] ModbusUpload(byte[] data)
        {
            if (data[5] == data[7] && data[5] == 0x31 && data[9] == data[8] && data[8] == 0x28 && data[11] == 0x03)
            {
                ADianliangRepository dlrep = new ADianliangRepository();
                a_dianliang          model = new a_dianliang();
                int    index = 10;
                string dz    = data[index].ToString();
                index++;
                index++;
                string zjs = data[index].ToString();
                index++;
                string dybh = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));
                index += 2;
                string dlbh = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));
                index += 2;
                string nxwd = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));
                index += 2;
                string sjrq = ("20" + data[index + 5].ToString("X")) + "-" + data[index + 4].ToString("X") + "-" + data[index + 3].ToString("X") + " " + data[index + 2].ToString("X") + ":" + data[index + 1].ToString("X") + ":" + data[index].ToString("");

                index += 6;
                string ady = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));
                index += 2;
                string bdy = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));
                index += 2;
                string cdy = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));

                index += 8;
                string adl = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));
                index += 2;
                string bdl = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));
                index += 2;
                string cdl = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 2));

                try
                {
                    model.date = DateTime.Parse(sjrq).ToString("yyyy-MM-dd HH:mm:ss");
                }
                catch
                {
                    model.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
                model.xdy1 = Convert.ToDecimal(int.Parse(ady) / 10.0);
                model.xdy2 = Convert.ToDecimal(int.Parse(bdy) / 10.0);
                model.xdy3 = Convert.ToDecimal(int.Parse(cdy) / 10.0);

                model.dl1 = Convert.ToDecimal(int.Parse(adl) / 100.00);
                model.dl2 = Convert.ToDecimal(int.Parse(bdl) / 100.00);
                model.dl3 = Convert.ToDecimal(int.Parse(cdl) / 100.00);
                TxtLogHelper.WriteLog("解析命令:A电压" + ady + " B电压" + bdy + " C电压 " + cdy + " A电流" + adl + " B电流" + bdl + " C电流" + cdl, "TCP", 1);
                index = 12 + int.Parse(zjs) + 14;
                if (data.Length > (index + 68))
                {
                    zjs    = data[index + 2].ToString();
                    index += 3;
                    string zdn = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 4));
                    index += 20;
                    string adn = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 4));
                    index += 4;
                    index += 16;
                    string bdn = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 4));
                    index += 4;
                    index += 16;
                    string cdn = ByteHelper.HexToTen(ByteHelper.ByteToString(data, index, 4));


                    model.zydl = Convert.ToDecimal(int.Parse(zdn) / 100.00);
                    model.aydl = Convert.ToDecimal(int.Parse(adn) / 100.00);
                    model.bydl = Convert.ToDecimal(int.Parse(bdn) / 100.00);
                    model.cydl = Convert.ToDecimal(int.Parse(cdn) / 100.00);
                    TxtLogHelper.WriteLog("解析命令: A电能" + adn + " B电能" + bdn + " C电能" + cdn + " 总电能" + zdn, "TCP", 1);
                }

                //dlrep.Insert(model);
            }
            TxtLogHelper.WriteLog("设备数据域上传回复命令内容:" + "7b 7b 91 7e ec 7d 7d", "TCP", 1);
            ConsoleWriteLine("设备数据域上传回复命令内容:7b 7b 91 7e ec 7d 7d", 1);
            return(ByteHelper.StringToByte("7b 7b 91 7e ec 7d 7d"));//固定值
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }//读取失败
Exemplo n.º 26
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);
        }
Exemplo n.º 27
0
        private void btnRead_Click(object sender, EventArgs e)
        {
            PPIReadWritePara readResult = new PPIReadWritePara();

            //ZLB_PPIHelper zlbPPI = new ZLB_PPIHelper();

            if (tcpClient.Connected)
            {
                PPIReadWritePara para = new PPIReadWritePara();
                para.StorageType = (Enums.StorageType)Enum.Parse(typeof(Enums.StorageType), comStore.Text);
                para.TcpClient   = tcpClient;
                para.ByteAddress = Int32.Parse(txtAddress.Text);
                para.Bitnumber   = int.Parse(txtBit.Text);
                para.PlcAddress  = int.Parse(txtPLC.Text);
                para.ComNum      = int.Parse(txtComNum.Text);

                para.ReadCount  = int.Parse(txtReadCount.Text) > 0 ? int.Parse(txtReadCount.Text) : 1;
                para.WriteValue = int.Parse(txtWriteValue.Text);
                if (para.StorageType == Enums.StorageType.T)
                {
                    MeiCloud.ZLB.ZlbDrive.PPI.PPIAddress ppiAddress = new MeiCloud.ZLB.ZlbDrive.PPI.PPIAddress();

                    drive.WriteCom(para.ComNum, ppiHelper.GetTReadDword(para, out ppiAddress));
                    //线程 对队列操作,这里就不能ReadCom

                    byte[] receivesAffirm = drive?.receiceData[para.ComNum];
                    if (receivesAffirm != null && receivesAffirm.Length > 0)
                    {
                        if (receivesAffirm[5] == Convert.ToByte(para.ComNum) && receivesAffirm[6] == ppiAddress.confirm)
                        {
                            drive.WriteCom(para.ComNum, ppiAddress.Affirm);
                        }
                        drive.receiceData[para.ComNum] = null;
                    }
                    else
                    {
                        Thread.Sleep(50);
                        if (receivesAffirm != null && receivesAffirm.Length > 0)
                        {
                            if (receivesAffirm[5] == Convert.ToByte(para.ComNum) && receivesAffirm[6] == ppiAddress.confirm)
                            {
                                drive.WriteCom(para.ComNum, ppiAddress.Affirm);
                            }
                            drive.receiceData[para.ComNum] = null;
                        }
                        else
                        {
                        }
                    }
                }
                else
                {
                    #region switch
                    switch (comRead.Text)
                    {
                    case "Bit":

                        readResult = ppiHelper.Readbit(para);

                        txtSendCmd.Text = ZLB_PPIHelper.sendCmd;
                        break;

                    case "Byte":
                        readResult      = ppiHelper.Readbytes(para);
                        txtSendCmd.Text = (ZLB_PPIHelper.sendCmd);
                        break;

                    case "Word":
                        readResult      = ppiHelper.ReadWords(para);
                        txtSendCmd.Text = (ZLB_PPIHelper.sendCmd);

                        txtSendCmd.Text = ZLB_PPIHelper.sendCmd;
                        break;

                    case "DWord":

                        readResult      = ppiHelper.ReadDoubleWord(para);
                        txtSendCmd.Text = (ZLB_PPIHelper.sendCmd);

                        txtSendCmd.Text = ZLB_PPIHelper.sendCmd;
                        break;

                    default:

                        break;
                    }
                    #endregion
                }
            }
            else
            {
                MessageBox.Show("连接断开");
            }

            if (readResult.IsSuceess)
            {
                txtReceive1.Text = ZLB_PPIHelper.receiveByte;
                txtValue.Text    = ByteHelper.ByteToString(readResult.ReadValue);
            }
            else
            {
                txtReceive1.Text = "读取失败";
            }
        }