Example #1
0
        static void Main(string[] args)
        {
            string deviceDescription = "USB-4704,BID#0";
            string profilePath       = @"C:\Advantech\Profile\p2.xml";

            tcClient   = new TcAdsClient();
            dataStream = new AdsStream(1);

            instantDoCtrl = new InstantDoCtrl();
            binReader     = new BinaryReader(dataStream, System.Text.Encoding.ASCII);
            tcClient.Connect(851);

            try
            {
                instantDoCtrl.SelectedDevice = new DeviceInformation(deviceDescription);
                errorCode = instantDoCtrl.LoadProfile(profilePath);//Loads a profile to initialize the device.
                if (BioFailed(errorCode))
                {
                    throw new Exception();
                }

                hDigOutP0                 = tcClient.AddDeviceNotification("A4704.byDigOutP0", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, huValHandle);
                hEndA4704Program          = tcClient.AddDeviceNotification("A4704.bEndA4704Program", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, huValHandle);
                tcClient.AdsNotification += new AdsNotificationEventHandler(OnNotification);

                HeartbeatThread heartbeat = new HeartbeatThread(tcClient);
                heartbeatThread = new Thread(new ThreadStart(heartbeat.beat));
                heartbeatThread.Start();

                manualResetEvent = new ManualResetEvent(false);
                manualResetEvent.WaitOne();

                //Console.ReadKey(false);
            }
            catch (Exception e)
            {
                // Something is wrong
                string errStr = BioFailed(errorCode) ? " Some error occurred. And the last error code is " + errorCode.ToString()
                                                           : e.Message;
                //Console.WriteLine(errStr);
            }
            finally
            {
                instantDoCtrl.Dispose();
                heartbeatThread.Abort();
                heartbeatThread.Join();
                tcClient.DeleteDeviceNotification(hDigOutP0);
                tcClient.Dispose();
            }
        }
        public void UpdateStaticDO_manual()
        {
            //-----------------------------------------------------------------------------------
            // Configure the following parameters before running the demo
            //-----------------------------------------------------------------------------------
            //The default device of project is demo device, users can choose other devices according to their needs.
            string    deviceDescription = "PCI-1750,BID#0";
            string    profilePath       = "../../profile/PCI-1750.xml";
            int       startPort         = 0;
            int       portCount         = 2;
            ErrorCode errorCode         = ErrorCode.Success;

            // Step 1: Create a 'InstantDoCtrl' for DO function.
            InstantDoCtrl instantDoCtrl = new InstantDoCtrl();

            try
            {
                // Step 2: Select a device by device number or device description and specify the access mode.
                // in this example we use ModeWrite mode so that we can fully control the device, including configuring, sampling, etc.
                instantDoCtrl.SelectedDevice = new DeviceInformation(deviceDescription);
                errorCode = instantDoCtrl.LoadProfile(profilePath);//Loads a profile to initialize the device.
                if (BioFailed(errorCode))
                {
                    throw new Exception();
                }

                // Step 3: Write DO ports
                byte[] bufferForWriting = new byte[64];
                //byte dataForWriteBit = 0;//data is used to the 'WriteBit'.
                //int bit = 1;//the bit is used to the 'WriteBit'.

                for (int i = 0; i < portCount; ++i)
                {
                    Console.WriteLine("Input a hexadecimal number for DO port {0} to output(for example, 0x11): ", startPort + i);
                    string data = Console.ReadLine();
                    bufferForWriting[i] = byte.Parse(data.Contains("0x") ? data.Remove(0, 2) : data, System.Globalization.NumberStyles.HexNumber);

                    /*
                     * //for WriteBit
                     * Console.WriteLine(" Input a hexadecimal number for DO port {0} to output(for example, 0x1 or 0x00): ", startPort + i);
                     * string data = Console.ReadLine();
                     * dataForWriteBit = byte.Parse(data.Contains("0x") ? data.Remove(0, 2) : data, System.Globalization.NumberStyles.HexNumber);
                     */
                }
                errorCode = instantDoCtrl.Write(startPort, portCount, bufferForWriting);
                /************************************************************************/
                //errorCode = instantDoCtrl.WriteBit(startPort, bit, dataForWriteBit);
                //NOTE:
                //Every channel has 8 bits, which be used to control 0--7 bit of anyone channel.
                //argument1:which port you want to contrl? For example, startPort is 0.
                //argument2:which bit you want to control? You can write 0--7, any number you want.
                //argument3:What status you want, open or close? 1 menas open, 0 means close.*/
                /************************************************************************/
                if (BioFailed(errorCode))
                {
                    throw new Exception();
                }
                Console.WriteLine("DO output completed !");
                // Read back the DO status.
                // Note:
                // For relay output, the read back must be deferred until the relay is stable.
                // The delay time is decided by the HW SPEC.
                // byte[] bufferForReading = new byte[64];
                // instantDoCtrl.DoRead(startPort, portCount, bufferForReading);
                // if (BioFailed(errorCode))
                // {
                //    throw new Exception();
                // }
                // Show DO ports' status
                // for (int i = startPort; i < portCount + startPort; ++i)
                // {
                //    Console.WriteLine("Now, DO port {0} status is:  0x{1:x}", i, bufferForReading[i - startPort]);
                // }
            }
            catch (Exception e)
            {
                // Something is wrong
                string errStr = BioFailed(errorCode) ? " Some error occurred. And the last error code is " + errorCode.ToString()
                                                           : e.Message;
                Console.WriteLine(errStr);
            }
            finally
            {
                // Step 4: Close device and release any allocated resource.
                instantDoCtrl.Dispose();
                //Console.ReadKey(false);
            }
        }