Ejemplo n.º 1
0
 private void testThread()
 {
     bytCount        = 5;
     bytStartChannel = 0;
     UInt32[] dwGetDIValue = new UInt32[1];
     UInt32[] dwOldValue   = new UInt32[1];
     dwOldValue[0] = 0;
     bool[] isOn = new bool[bytCount];
     while (true)
     {
         ret = MXIO_CS.E1K_DI_Reads(hConnection[0], bytStartChannel, bytCount, dwGetDIValue);
         if (ret == MXIO_CS.MXIO_OK)
         {
             if (dwOldValue[0] != dwGetDIValue[0])
             {
                 Console.WriteLine(ret);
                 dwOldValue[0] = dwGetDIValue[0];
                 for (i = 0, dwShiftValue = 0; i < bytCount; i++, dwShiftValue++)
                 {
                     isOn[i] = (dwGetDIValue[0] & (1 << dwShiftValue)) != 0;
                     //TODO handle the result (change DIs value to bool?)
                 }
                 Console.WriteLine("New register: " + _timer.Elapsed);
             }
         }
         Thread.Sleep(100);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the di bit.
        /// </summary>
        /// <param name="channel">The channel.</param>
        /// <param name="attempt">The max number of read attempts.</param>
        /// <returns></returns>
        /// <exception cref="System.IO.IOException"></exception>
        public int GetDIBit(int channel, int attempt)
        {
            uint[] value = new uint[1];

            lock (_lock)
            {
                MoxaStatus status = (MoxaStatus)MXIO_CS.E1K_DI_Reads(_hConn[0], (byte)channel, 1, value);

                if (status != MoxaStatus.Ok)
                {
                    CheckConnection(true);

                    if (attempt >= _retries)
                    {
                        throw new IOException(this, String.Format("Could not read MOXA DI channel {0} after {1} retries. ({2}).", channel, attempt, status));
                    }

                    return(GetDIBit(channel, attempt + 1));
                }
            }

            return((int)value[0]);
        }
Ejemplo n.º 3
0
        public int[] GetDIBits(int[] channels)
        {
            List <int> allValues = new List <int>(channels.Length);

            List <List <int> > _contGroups = new List <List <int> >();

            _contGroups.Add(new List <int>());

            for (int i = 0; i < channels.Length; i++)
            {
                List <int> group = _contGroups.Last();

                if (group.Count == 0)
                {
                    group.Add(channels[i]);
                    continue;
                }

                if (group.Last() != channels[i] - 1)
                {
                    _contGroups.Add(new List <int>()
                    {
                        channels[i]
                    });
                }
                else
                {
                    group.Add(channels[i]);
                }
            }

            foreach (List <int> group in _contGroups)
            {
                uint[] values = new uint[1];

                lock (_lock)
                {
                    MoxaStatus status  = MoxaStatus.SIOResponseBad;
                    int        attempt = 0;

                    while (attempt < _retries)
                    {
                        status = (MoxaStatus)MXIO_CS.E1K_DI_Reads(_hConn[0], (byte)group.First(), (byte)group.Count, values);

                        if (status == MoxaStatus.Ok)
                        {
                            break;
                        }

                        CheckConnection(true);

                        attempt++;
                    }

                    if (status != MoxaStatus.Ok)
                    {
                        throw new IOException(this, String.Format("Could not read MOXA DI channels {0} after {1} retries ({2}).", String.Join(",", channels), _retries, status));
                    }
                }

                for (int i = 0; i < group.Count; i++)
                {
                    uint bitMask  = (uint)1 << i;
                    int  bitValue = (values[0] & bitMask) != 0 ? 1 : 0;

                    allValues.Add(bitValue);
                }
            }

            return(allValues.ToArray());
        }
Ejemplo n.º 4
0
        //------------- Format variable ------------


        private void RunTimer_Tick(object sender, EventArgs e)
        {
            Int32 dwShiftValue = 0x0000;
            int   i            = 0;

            UInt32[] dwGetDIValue = new UInt32[1];
            if (cbService.Checked)
            {
                CheckService(NavServiceServer, NavServiceUserName, NavServicePassword, NavServiceServiceName, false);
            }
            BalanceWS.BalanceWS CBPAutomation = new BalanceWS.BalanceWS();
            CBPAutomation.UseDefaultCredentials = true;
            try
            {
                bool OK = false;
                OK = CBPAutomation.WSNewPalletExist(NewPalletPosIs);
                if (OK)
                {
                    ret = CheckConnection(false);
                    if (ret == MXIO_CS.EIO_SOCKET_DISCONNECT)
                    {
                        CreateIoHandle();
                    }
                    ret = MOXA_CSharp_MXIO.MXIO_CS.E1K_DO_Writes(hConnection[0], DO_DATA_SEND_TO_FISKER, 1, TRIGGER_TYPE_LO_2_HI);
                    MXEIO_Error.CheckErr(ret, "E1K_DO_Writes");
                    if (ret == MXIO_CS.MXIO_OK)
                    {
                        tbCommunication.AppendText(string.Format("New Pallet E1K_DO_Writes Set Ch{0} DO Direction DO Mode value = ON success.{1}", DO_DATA_SEND_TO_FISKER, "\r\n"));
                    }
                    else
                    {
                        tbCommunication.AppendText(string.Format("New Pallet E1K_DO_Write Ch{0} ON returned ERROR: {1} Return value: {2}{3}", DO_DATA_SEND_TO_FISKER, MXEIO_Error.CheckErr(ret, "E1K_DO_Writes"), ret.ToString(), "\r\n"));
                    }
                    GlobalChannel = DO_DATA_SEND_TO_FISKER;
                    Thread.Sleep(2000);
                    ResetChanel_Tick(sender, e);
                }

                if (OK)
                {
                    try
                    {
                        CBPAutomation.WSRenameNewPallet(NewPalletPosIs);
                    }
                    catch (Exception ex)
                    {
                        tbCommunication.AppendText(string.Format("Rename New pallet failed: {0}{1}", MessageBox.Show(ex.Message), "\r\n"));
                    }
                }
            }
            catch (Exception ex)
            {
                tbCommunication.AppendText(string.Format("New pallet check failed: {0}{1}", MessageBox.Show(ex.Message), "\r\n"));
            }

            ret = CheckConnection(false);
            if (ret == MXIO_CS.EIO_SOCKET_DISCONNECT)
            {
                CreateIoHandle();
            }

            ret = MXIO_CS.E1K_DI_Reads(hConnection[0], bytStartChannel, bytCount, dwGetDIValue);
            MXEIO_Error.CheckErr(ret, "E1K_DI_Reads");

            if (ret == MXIO_CS.MXIO_OK)
            {
                for (i = 0, dwShiftValue = 0; i < bytCount; i++, dwShiftValue++)
                {
                    MyString = (((dwGetDIValue[0] & (1 << dwShiftValue)) == 0) ? "OFF" : "ON");
                    if (MyString.Equals("ON"))
                    {
                        // se hvilken Ch det drejer sig om og udfør handling.
                        //tbCommunication.AppendText(string.Format("DI value: ch[{0}] = {1}{2}", i + bytStartChannel, ((dwGetDIValue[0] & (1 << dwShiftValue)) == 0) ? "OFF" : "ON", "\r\n"));
                        if ((dwShiftValue) == DI_DUMMY_PALLET)
                        {
                            try
                            {
                                CBPAutomation.WSAddPalletToQue(string.Format("W {0}", DateTime.Now.ToShortDateString() + ' ' + DateTime.Now.ToShortTimeString()));
                                tbCommunication.AppendText(string.Format("Add Wrap-only pallet OK: {0}", "\r\n"));
                            }
                            catch (Exception ex)
                            {
                                tbCommunication.AppendText(string.Format("Add Wrap-only pallet failed: {0}{1}", MessageBox.Show(ex.Message), "\r\n"));
                            }
                        }


                        if ((dwShiftValue) == DI_READY_FOR_LABEL)
                        {
                            try
                            {
                                if (CBPAutomation.WSIsDummyPallet())
                                {
                                    ret = CheckConnection(false);
                                    if (ret == MXIO_CS.EIO_SOCKET_DISCONNECT)
                                    {
                                        CreateIoHandle();
                                    }

                                    ret = MOXA_CSharp_MXIO.MXIO_CS.E1K_DO_Writes(hConnection[0], DO_DUMMY_PALLET, 1, TRIGGER_TYPE_LO_2_HI);
                                    MXEIO_Error.CheckErr(ret, "E1K_DO_Writes");
                                    if (ret == MXIO_CS.MXIO_OK)
                                    {
                                        tbCommunication.AppendText(string.Format("Dummy Pallet DataE1K_DO_Writes Set Ch{0} DO Direction DO Mode value = ON success.{1}", DO_LABEL_SEND_TO_PRINTER, "\r\n"));
                                    }
                                    else
                                    {
                                        tbCommunication.AppendText(string.Format("Dummy Pallet E1K_DO_Write returned ERROR: {0} Return value: {1}{2}", MXEIO_Error.CheckErr(ret, "E1K_DO_Writes"), ret.ToString(), "\r\n"));
                                    }
                                    Thread.Sleep(2000);
                                    GlobalChannel = DO_DUMMY_PALLET;
                                    ResetChanel_Tick(sender, e);
                                }

                                else if (CBPAutomation.WSCreateSSCCDataFile())
                                {
                                    if (System.IO.File.Exists(s_SSCCPath + s_SSCCLabelName))
                                    {
                                        System.IO.File.Copy(s_SSCCPath + s_SSCCLabelName, EidosPrinterIp + EidosPrinterPort, true);
                                    }

                                    if (b_DeleteFile)
                                    {
                                        System.IO.File.Delete(s_SSCCPath + s_SSCCLabelName);
                                    }

                                    ret = CheckConnection(false);
                                    if (ret == MXIO_CS.EIO_SOCKET_DISCONNECT)
                                    {
                                        CreateIoHandle();
                                    }

                                    ret = MOXA_CSharp_MXIO.MXIO_CS.E1K_DO_Writes(hConnection[0], DO_LABEL_SEND_TO_PRINTER, 1, TRIGGER_TYPE_LO_2_HI);
                                    MXEIO_Error.CheckErr(ret, "E1K_DO_Writes");
                                    if (ret == MXIO_CS.MXIO_OK)
                                    {
                                        tbCommunication.AppendText(string.Format("Create SSCC DataE1K_DO_Writes Set Ch{0} DO Direction DO Mode value = ON success.{1}", DO_LABEL_SEND_TO_PRINTER, "\r\n"));
                                    }
                                    else
                                    {
                                        tbCommunication.AppendText(string.Format("Create SSCC E1K_DO_Write returned ERROR: {0} Return value: {1}{2}", MXEIO_Error.CheckErr(ret, "E1K_DO_Writes"), ret.ToString(), "\r\n"));
                                    }
                                    Thread.Sleep(2000);
                                    GlobalChannel = DO_LABEL_SEND_TO_PRINTER;
                                    //RunTimer.Stop();
                                    //ResetChanel.Start();
                                    ResetChanel_Tick(sender, e);
                                }
                            }
                            catch (Exception ex)
                            {
                                tbCommunication.AppendText(string.Format("Send SSCC Data to printer failed: {0}{1}", MessageBox.Show(ex.Message), "\r\n"));
                            }
                        }

                        if ((dwShiftValue) == DI_PALLET_FINISHED)
                        {
                            try
                            {
                                CBPAutomation.WSRemovePallet();
                                tbCommunication.AppendText(string.Format("Remove pallet success: {0}", "\r\n"));
                                Thread.Sleep(2000);
                            }
                            catch (Exception ex)
                            {
                                tbCommunication.AppendText(string.Format("Remove pallet failed: {0}{1}", MessageBox.Show(ex.Message), "\r\n"));
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public override bool GetDigitalInput(int input)
 {
     uint[] value = new uint[1];
     InvokeWithRetry("GetDigitalInput", () => MXIO_CS.E1K_DI_Reads(connection, 1, (byte)input, value));
     return(value[0] == 1);
 }