示例#1
0
        /// <summary>
        /// 对二进制串解报
        /// </summary>
        public void UnpackReceive(byte[] bytetemp, int startAddr, int len)
        {
            try
            {
                //if (tvar is RTU)
                if (m_config_Modbus.m_ModbusType == "RTU")
                {
                    if (bytetemp[2] + 5 == bytetemp.Length)
                    {
                        RTU vtemp = new RTU(bytetemp, len, startAddr,m_type);

                        if ((vtemp.Responseread != null) && (vtemp.Responseread.ByteNum != 0))
                        {

                            Console.WriteLine("RX: FC:{0} ", vtemp.Responseread.FC);
                            try
                            {
                                sw = File.AppendText(logfile);
                                sw.WriteLine(DateTime.Now.ToString() + " RX: FC:{0} ", vtemp.Responseread.FC);
                                sw.Close();
                            }
                            catch { }

                            var datas = vtemp.GetData();
                            foreach (var data in datas)
                            {
                                //if (data.Addr == 0) continue;
                                Console.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " +
                                                    "data:" + data.Data.ToString());
                                try
                                {
                                    sw = File.AppendText(logfile);
                                    sw.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " +
                                                        "data:" + data.Data.ToString());
                                    sw.Close();
                                }
                                catch { }

                                numInf numtemp = new numInf();
                                if (find.TryGetValue(data.Addr, out numtemp))
                                {
                                    numtemp.val = Convert.ToSingle(data.Data);
                                    numtemp.dtm = DateTime.Now;

                                    find.Remove(data.Addr);
                                    find.Add(data.Addr, numtemp);
                                }
                                else
                                {
                                    numtemp.val = Convert.ToSingle(data.Data);
                                    numtemp.dtm = DateTime.Now;
                                    find.Add(data.Addr, numtemp);
                                }
                            }
                            Console.WriteLine("\n");
                            try
                            {
                                sw = File.AppendText(logfile);
                                sw.WriteLine("\r\n");
                                sw.Close();
                            }
                            catch { }
                        }//end if ((vtemp.Responseread != null) && (vtemp.Responseread.ByteNum != 0))
                    }//end if (bytetemp[2] + 5 == bytetemp.Length)
                }//end if (tvar is RTU)

                //if (tvar is ASCII)
                if (m_config_Modbus.m_ModbusType == "ASCII")
                {
                    ASCII vtemp = new ASCII(bytetemp, len, startAddr, m_type);
                    if ((vtemp.AscRtu.Responseread != null) && (vtemp.AscRtu.Responseread.ByteNum != 0))
                    {

                        Console.WriteLine("RX: FC:{0} ", vtemp.AscRtu.Responseread.FC);
                        try
                        {
                            sw = File.AppendText(logfile);
                            sw.WriteLine(DateTime.Now.ToString() + " RX: FC:{0} ", vtemp.AscRtu.Responseread.FC);
                            sw.Close();
                        }
                        catch { }

                        var datas = vtemp.AscRtu.GetData();
                        foreach (var data in datas)
                        {
                            //if (data.Addr == 0) continue;
                            Console.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " +
                                                "data:" + data.Data.ToString());
                            try
                            {
                                sw = File.AppendText(logfile);
                                sw.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " +
                                                    "data:" + data.Data.ToString());
                                sw.Close();
                            }
                            catch { }

                            numInf numtemp = new numInf();
                            if (find.TryGetValue(data.Addr, out numtemp))
                            {
                                numtemp.val = Convert.ToSingle(data.Data);
                                numtemp.dtm = DateTime.Now;

                                find.Remove(data.Addr);
                                find.Add(data.Addr, numtemp);
                            }
                            else
                            {
                                numtemp.val = Convert.ToSingle(data.Data);
                                numtemp.dtm = DateTime.Now;
                                find.Add(data.Addr, numtemp);
                            }
                        }
                        Console.WriteLine("\n");
                        try
                        {
                            sw = File.AppendText(logfile);
                            sw.WriteLine("\r\n");
                            sw.Close();
                        }
                        catch { }
                    }

                }//if (tvar is ASCII)
            }// end try
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                //异常处理
                throw ex;
            }
        }
示例#2
0
        /// <summary>
        /// 对字符串解报
        /// </summary>
        /// ycType= 1; int16;
        /// ycType= 2; int32;
        /// ycType= 3; float32;
        /// ycType= 4; double64;
        /// ycType= 5; Uint16;
        /// ycType= 6; int32_Inver;
        /// ycType= 7; float32_Inver;
        /// ycType= 8; double64_Inver;
        public static void UnPackString(string pm, string str,int type)
        {
            string stemp = str.ToLower().Replace(" ", "");
            byte[] bytetemp = new byte[stemp.Length / 2];
            for (int i = 0; i < stemp.Length / 2; i++)
            {
                string ss = stemp.Substring(2 * i, 2);
                bytetemp[i] = Convert.ToByte(ss, 16);
            }
            try
            {
                if ((pm == "TX"))
                {
                    //T temp = new T(bytetemp);
                    //if (temp.GetControl().PRM)
                    //{
                    //    Console.WriteLine("TX: FCB:{0} FCV:{1} FUNCTION:{2}",
                    //                        temp.GetControl().FCB, temp.GetControl().FCV, temp.GetControl().function_1);

                    //}
                    //else
                    //{
                    //    Console.WriteLine("RX: ACD:{0} DFC:{1} FUNCTION:{2}",
                    //                        temp.GetControl().ACD, temp.GetControl().DFC, temp.GetControl().function_0);

                    //}
                }
                if ((pm == "RX"))
                {
                    //if (tvar is RTU)
                    //if (m_config_Modbus.m_ModbusType == "RTU" )
                    {
                        RTU vtemp = new RTU(bytetemp, 0,type);
                        if ((vtemp.Responseread != null) && (vtemp.Responseread.ByteNum != 0))
                        {

                            Console.WriteLine("RX: FC:{0} ", vtemp.Responseread.FC);
                            Console.WriteLine("RX: {0}", vtemp.ToString());

                            var datas = vtemp.GetData();
                            foreach (var data in datas)
                            {
                                //if (data.Addr == 0) continue;
                                Console.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " +
                                                    "data:" + data.Data.ToString());
                            }
                            Console.WriteLine("\r\n");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }