void print(IntPtr sdkContext, BS2Rs485SlaveDeviceEX slaveExDevice)
 {
     Console.WriteLine(">>>> SlaveDevice id[{0, 10}] channel[{1}] type[{2, 3}] model[{3, 16}] enable[{4}], connected[{5}]",
                       slaveExDevice.deviceID,
                       slaveExDevice.channelInfo,
                       slaveExDevice.deviceType,
                       API.productNameDictionary[(BS2DeviceTypeEnum)slaveExDevice.deviceType],
                       Convert.ToBoolean(slaveExDevice.enableOSDP),
                       Convert.ToBoolean(slaveExDevice.connected));
 }
        public void getSlaveExDevice(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            IntPtr slaveDeviceObj   = IntPtr.Zero;
            UInt32 slaveDeviceCount = 0;
            UInt32 outchannelport   = 0;

            Console.WriteLine("Trying to get the slave devices.");
            BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetSlaveExDevice(sdkContext, deviceID, 0xFF, out slaveDeviceObj, out outchannelport, out slaveDeviceCount);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
            }
            else if (slaveDeviceCount > 0)
            {
                List <BS2Rs485SlaveDeviceEX> slaveDeviceList = new List <BS2Rs485SlaveDeviceEX>();
                IntPtr curSlaveDeviceObj = slaveDeviceObj;
                int    structSize        = Marshal.SizeOf(typeof(BS2Rs485SlaveDeviceEX));

                for (int idx = 0; idx < slaveDeviceCount; ++idx)
                {
                    BS2Rs485SlaveDeviceEX item = (BS2Rs485SlaveDeviceEX)Marshal.PtrToStructure(curSlaveDeviceObj, typeof(BS2Rs485SlaveDeviceEX));
                    slaveDeviceList.Add(item);
                    curSlaveDeviceObj = (IntPtr)((long)curSlaveDeviceObj + structSize);
                }

                API.BS2_ReleaseObject(slaveDeviceObj);

                foreach (BS2Rs485SlaveDeviceEX slaveExDevice in slaveDeviceList)
                {
                    print(sdkContext, slaveExDevice);
                }

                //slaveExControl(sdkContext, slaveDeviceList);
            }
            else
            {
                Console.WriteLine(">>> There is no slave device in the device.");
            }
        }
        public void setSlaveExDevice(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            IntPtr slaveDeviceObj   = IntPtr.Zero;
            UInt32 slaveDeviceCount = 0;
            UInt32 outchannelport   = 0;

            Console.WriteLine("Choose the RS485 port where the device is connected. [0(default), 1, 2, 3, 4]");
            Console.Write(">>>> ");
            int selchannel = Util.GetInput(0);

            Console.WriteLine("Trying to get the slave devices.");
            BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetSlaveExDevice(sdkContext, deviceID, (uint)selchannel, out slaveDeviceObj, out outchannelport, out slaveDeviceCount);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
            }
            else if (slaveDeviceCount > 0)
            {
                List <BS2Rs485SlaveDeviceEX> slaveDeviceList = new List <BS2Rs485SlaveDeviceEX>();
                IntPtr curSlaveDeviceObj = slaveDeviceObj;
                int    structSize        = Marshal.SizeOf(typeof(BS2Rs485SlaveDeviceEX));

                for (int idx = 0; idx < slaveDeviceCount; ++idx)
                {
                    BS2Rs485SlaveDeviceEX item = (BS2Rs485SlaveDeviceEX)Marshal.PtrToStructure(curSlaveDeviceObj, typeof(BS2Rs485SlaveDeviceEX));
                    slaveDeviceList.Add(item);
                    curSlaveDeviceObj = (IntPtr)((long)curSlaveDeviceObj + structSize);
                }

                Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");
                for (UInt32 idx = 0; idx < slaveDeviceCount; ++idx)
                {
                    BS2Rs485SlaveDeviceEX slaveDevice = slaveDeviceList[(int)idx];
                    Console.WriteLine("[{0:000}] ==> SlaveDevice id[{1, 10}] channel[{2}] type[{3, 3}] model[{4, 16}] enable[{5}], connected[{6}]",
                                      idx,
                                      slaveDevice.deviceID,
                                      slaveDevice.channelInfo,
                                      slaveDevice.deviceType,
                                      API.productNameDictionary[(BS2DeviceTypeEnum)slaveDevice.deviceType],
                                      Convert.ToBoolean(slaveDevice.enableOSDP),
                                      Convert.ToBoolean(slaveDevice.connected));
                }
                Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");
                Console.WriteLine("Enter the index of the slave device which you want to connect: [INDEX_1,INDEX_2 ...]");
                Console.Write(">>>> ");
                char[]           delimiterChars     = { ' ', ',', '.', ':', '\t' };
                string[]         slaveDeviceIndexs  = Console.ReadLine().Split(delimiterChars);
                HashSet <UInt32> connectSlaveDevice = new HashSet <UInt32>();

                if (slaveDeviceIndexs.Length == 0)
                {
                    Console.WriteLine("All of the slave device will be disabled.");
                }
                else
                {
                    foreach (string slaveDeviceIndex in slaveDeviceIndexs)
                    {
                        if (slaveDeviceIndex.Length > 0)
                        {
                            UInt32 item;
                            if (UInt32.TryParse(slaveDeviceIndex, out item))
                            {
                                if (item < slaveDeviceCount)
                                {
                                    connectSlaveDevice.Add(slaveDeviceList[(int)item].deviceID);
                                }
                            }
                        }
                    }
                }

                curSlaveDeviceObj = slaveDeviceObj;
                for (int idx = 0; idx < slaveDeviceCount; ++idx)
                {
                    BS2Rs485SlaveDeviceEX item = (BS2Rs485SlaveDeviceEX)Marshal.PtrToStructure(curSlaveDeviceObj, typeof(BS2Rs485SlaveDeviceEX));

                    if (connectSlaveDevice.Contains(item.deviceID))
                    {
                        if (item.enableOSDP != 1)
                        {
                            item.enableOSDP = 1;
                            Marshal.StructureToPtr(item, curSlaveDeviceObj, false);
                        }
                    }
                    else
                    {
                        if (item.enableOSDP != 0)
                        {
                            item.enableOSDP = 0;
                            Marshal.StructureToPtr(item, curSlaveDeviceObj, false);
                        }
                    }

                    curSlaveDeviceObj = (IntPtr)((long)curSlaveDeviceObj + structSize);
                }

                Console.WriteLine("Trying to set the slave devices.");
                result = (BS2ErrorCode)API.BS2_SetSlaveExDevice(sdkContext, deviceID, (uint)selchannel, slaveDeviceObj, slaveDeviceCount);

                API.BS2_ReleaseObject(slaveDeviceObj);

                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                }
                else
                {
                    //slaveExControl(sdkContext, slaveDeviceList);
                    Console.WriteLine("Do you want update config of CST slaves? [y/n]");
                    Console.Write(">>>> ");
                    if (Util.IsYes())
                    {
                        slaveExConfig(sdkContext, deviceID, slaveDeviceList);
                    }
                }
            }
            else
            {
                Console.WriteLine(">>> There is no slave device in the device.");
            }
        }