Beispiel #1
0
        /// <summary>
        /// Refresh Modbus Address type and value
        /// </summary>
        private void RefreshModbusAddressSetting()
        {
            gvAddress.Items.Clear();

            try
            {
                if (m_adamModbusSocket.Connect(m_adamModbusSocket.GetIP(), protoType, portNum))
                {
                    bool bFixed;
                    m_adamModbusSocket.Configuration().GetModbusAddressMode(out bFixed);
                    string[] szAddressType  = new string[m_szSlotInfo.Length];
                    string[] szLength       = new string[m_szSlotInfo.Length];
                    string[] szStartAddress = new string[m_szSlotInfo.Length];

                    //Update Modbus Address Setting tab
                    ListViewItem[] listItemModule = new ListViewItem[m_szSlotInfo.Length];
                    for (int i = 0; i < m_szSlotInfo.Length; i++)
                    {
                        listItemModule[i] = new ListViewItem(i.ToString());
                        if (m_szSlotInfo[i] != null)
                        {
                            if (IsModuleSupportModbusSetting(m_szSlotInfo[i]))
                            {
                                listItemModule[i].SubItems.Add("APAX-" + m_szSlotInfo[i]);
                            }
                            else
                            {
                                listItemModule[i].SubItems.Add("APAX-" + m_szSlotInfo[i] + " : INVALID");
                            }
                        }
                        else
                        {
                            listItemModule[i].SubItems.Add("Empty");
                        }

                        listItemModule[i].SubItems.Add("");
                        listItemModule[i].SubItems.Add("");
                        listItemModule[i].SubItems.Add("");
                        gvAddress.Items.Add(listItemModule[i]);
                    }

                    if (bFixed)
                    {
                        addressTypeValue.Text = "Fixed Mode";
                        for (int i = 0; i < m_szSlotInfo.Length; i++)
                        {
                            if ((m_szSlotInfo[i] != null) && (IsModuleSupportModbusSetting(m_szSlotInfo[i])))
                            {
                                Apax5000Config apaxConfig = null;
                                int            iTemp;
                                m_adamModbusSocket.Configuration().GetModuleConfig(i, out apaxConfig);
                                if (apaxConfig.wDevType == 0x0001 || apaxConfig.wDevType == 0x0002)
                                {
                                    szLength[i]      = Convert.ToString(64);
                                    iTemp            = 64 * i + 1;
                                    szAddressType[i] = "0X";
                                }
                                else
                                {
                                    szLength[i]      = Convert.ToString(32);
                                    iTemp            = 32 * i + 1;
                                    szAddressType[i] = "4X";
                                }
                                szStartAddress[i] = iTemp.ToString();
                            }
                            else
                            {
                                szAddressType[i]  = "0X";
                                szStartAddress[i] = (0).ToString();
                                szLength[i]       = (0).ToString();
                            }
                        }
                    }
                    else
                    {
                        int[] o_iData;
                        addressTypeValue.Text = "Flexible Mode";
                        m_adamModbusSocket.Modbus().ReadAdvantechRegs(60101, 64, out o_iData);
                        for (int i = 0; i < m_szSlotInfo.Length; i++)
                        {
                            if (IsModuleSupportModbusSetting(m_szSlotInfo[i]))
                            {
                                int iStartAddress = o_iData[i * 2] % 10000;
                                if (o_iData[i * 2] / 40000 == 1)
                                {
                                    szAddressType[i] = "4X";
                                }
                                else
                                {
                                    szAddressType[i] = "0X";
                                }
                                szStartAddress[i] = iStartAddress.ToString();
                                szLength[i]       = o_iData[i * 2 + 1].ToString();
                            }
                            else
                            {
                                szAddressType[i]  = "0X";
                                szStartAddress[i] = (0).ToString();
                                szLength[i]       = (0).ToString();
                            }
                        }
                    }
                    UpdateInfoString(IDX_STARTADDRESS, szStartAddress);
                    UpdateInfoString(IDX_ADDRESSTYPE, szAddressType);
                    UpdateInfoString(IDX_LENGTH, szLength);
                }
            }
            catch
            {
                MessageBox.Show("Initialize UI Modbus address setting failed.", "Error");
            }
            m_adamModbusSocket.Disconnect();
        }