private void readConfigFile()
        {
            if (MAppConfig.InitFromFile())
            {
                string defaultCfgFile    = MAppConfig.getValueByName("defaultCfgFile");
                string defaultPLCCfgFile = MAppConfig.getValueByName("defaultPLCCfgFile");
                string configDirStr      = MAppConfig.getValueByName("defaultCfgDir");
                string fileName          = configDirStr + "/" + defaultCfgFile;
                //PLC服务器配置
                string plcFileName = configDirStr + "/" + defaultPLCCfgFile;

                if (!String.IsNullOrEmpty(fileName))
                {
                    if (!NodeMgr.init(fileName))//
                    {
                        MessageBox.Show(fileName + "模型解析失败!检查配置文件", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                if (!String.IsNullOrEmpty(plcFileName))
                {
                    if (!Modbus.PLCNodeMgr.init(plcFileName))//
                    {
                        MessageBox.Show(plcFileName + " PLC服务器配置解析失败!检查配置文件", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Example #2
0
        public void toStart()
        {
            string configDirStr = MAppConfig.getValueByName("defaultCfgDir");

            _dataFileName = configDirStr + "/" + NodeMgr._nodeList[_deviceIndex]._typeStr + "_data.csv";

            if (!readData(_dataFileName))
            {
                FormLog(_dataFileName + "读取失败");
                return;
            }

            Thread thread = new Thread(run)
            {
                Name = "DataPlay" + NodeMgr._nodeList[_deviceIndex]._name, IsBackground = true
            };

            thread.Start();
        }
Example #3
0
        bool setValueUniverse(int groupindex, int offset, string dataTypeStr, int coe_reverse, string valueStr)
        {
            float  value_f;
            string offsetAddOne = MAppConfig.getValueByName("offsetAddOne");

            if (offsetAddOne != "0")
            {
                offset += 1;//此处的内存对应的是modbus协议中的地址,比offset要多1。
            }
            else
            {
                offset += 0;//只在配置为0时才不加1
            }
            try
            {
                if (valueStr.Contains('.'))//有些点虽为INT型,但最终的值是float。风速为INT,modbus值为988这样。
                {
                    value_f = float.Parse(valueStr);
                }
                else
                {
                    value_f = int.Parse(valueStr);
                }



                switch (dataTypeStr.ToUpper())
                {
                case "INT16":
                case "WORD":
                case "INT":    //目前主控把INT当作16位
                    setValue16(groupindex, offset, (ushort)(value_f * coe_reverse));
                    break;

                case "INT32":
                case "DINT":
                case "DWORD":
                    setValue32(groupindex, offset, (int)value_f * coe_reverse);
                    break;

                case "REAL":
                case "FLOAT":
                    value_f = float.Parse(valueStr);
                    setValue32(groupindex, offset, value_f * coe_reverse);
                    break;

                case "BIT":    //先不管
                    return(true);

                default:
                    setValue16(groupindex, offset, (ushort)(value_f * coe_reverse));
                    break;
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Example #4
0
        /// <summary>
        /// initData
        /// </summary>
        /// <param name="errorStr"></param>
        /// <returns></returns>
        public bool initNodeData(ref string errorStr)
        {
            _dt = null;

            try
            {
                if (_typeStr.Length == 0)
                {
                    return(false);
                }

                string configDirStr = MAppConfig.getValueByName("defaultCfgDir");
                string csvFileName  = configDirStr + "/" + _typeStr + ".csv";// _typeStr是MY1500, 采用MY1500.csv作为模型名
                bool   ret          = CSVReader.readCSV(csvFileName, out _dt);
                if (!ret)
                {
                    return(false);
                }

                for (int i = 0; i < _dt.Rows.Count; i++) //写入各行数据
                {
                    {
                        string ioName = _dt.Rows[i]["path"].ToString();
                        if (ioName.Length == 0)
                        {
                            errorStr = csvFileName + "[path] 列出现空值";
                            return(false);
                        }

                        _ioName2index[ioName] = i;
                    }

                    string groupindexStr = _dt.Rows[i]["groupIndex"].ToString();
                    if (groupindexStr.Length == 0)
                    {
                        errorStr = csvFileName + "[groupIndex] 列出现空值";
                        return(false);
                    }
                    int groupindex = int.Parse(groupindexStr);     //功能码

                    string offsetStr = _dt.Rows[i]["offs"].ToString();
                    if (offsetStr.Length == 0)
                    {
                        errorStr = csvFileName + "[offs] 列出现空值";
                        return(false);
                    }
                    if (offsetStr.Contains(':'))
                    {
                        offsetStr = offsetStr.Substring(0, offsetStr.IndexOf(":"));
                    }
                    int offset = int.Parse(offsetStr);

                    string dataTypeStr = _dt.Rows[i]["dataType"].ToString();
                    if (dataTypeStr.Length == 0)
                    {
                        errorStr = csvFileName + "[dataType] 列出现空值";
                        return(false);
                    }

                    float  coe         = float.Parse(_dt.Rows[i]["coe"].ToString());
                    int    coe_reverse = floatToInt(1.00000000f / coe);//1.0除以0.1得到0.9
                    string valueStr    = "0";
                    if (_dt.Columns.Contains("value"))
                    {
                        valueStr = _dt.Rows[i]["value"].ToString();//如果有value这一列就赋值,否则默认是0
                    }

                    bool ret1 = setValueUniverse(groupindex, offset, dataTypeStr, coe_reverse, valueStr);
                    if (ret1 != true)
                    {
                        return(false);
                    }
                }//for

                return(true);
            }
            catch (Exception e)
            {
                errorStr = e.Message;
                return(false);
            }
        }