public CAN_TX_MSG(CAN_TX_MSG obj)
 {
     this.lArbId    = obj.lArbId;
     this.iNumBytes = obj.iNumBytes;
     for (int i = 0; i < 8; i++)
     {
         bData[i] = obj.bData[i];
     }
 }
 public static void Add_TX_Msg(CAN_TX_MSG objTxMsg)
 {
     mutex.WaitOne();
     if (qTxMsg.Count == 0)
     {
         qTxMsg.Enqueue(objTxMsg);
     }
     mutex.ReleaseMutex();
 }
        public static void AddBroadCastMsg(Int32 iArbId, byte iNumByte, byte[] bData)
        {
            CAN_TX_MSG txMsg = new CAN_TX_MSG(iArbId, iNumByte, bData);

            CAN_Connection.Add_TX_Msg(txMsg);
        }
        public async Task GetMessageFromCAN()
        {
            long          lResult      = 0;
            icsSpyMessage stMessagesTx = new icsSpyMessage();
            long          lNetworkID;
            int           uiTimeout = 0;

            // Has the uset open neoVI yet?;
            if (m_bPortOpen == false)
            {
                MessageBox.Show("ValueCAN not connected");
                return; // do not read messages if we haven't opened neoVI yet
            }

            // Read the Network we will transmit on (indicated by lstNetwork ListBox)
            lNetworkID = (int)eNETWORK_ID.NETID_HSCAN;
            stMessagesTx.StatusBitField = 0;
            tCANWait.Start();
            timeDelay.Start();

            while (StartDAQ)
            {
                mutex.WaitOne(250);
                var        txMsgCount = qTxMsg.Count;
                CAN_TX_MSG txMsg      = null;
                Console.WriteLine("TX Msg count = {0}", txMsgCount);
                if (txMsgCount > 0)
                {
                    txMsg = qTxMsg.Dequeue();
                }
                mutex.ReleaseMutex();


                if ((txMsgCount > 0) && (txMsg != null))
                {
                    stMessagesTx.ArbIDOrHeader   = txMsg.lArbId;          // The ArbID
                    stMessagesTx.NumberBytesData = txMsg.iNumBytes;       // The number of Data Bytes

                    stMessagesTx.Data1 = txMsg.bData[0];
                    stMessagesTx.Data2 = txMsg.bData[1];
                    stMessagesTx.Data3 = txMsg.bData[2];
                    stMessagesTx.Data4 = txMsg.bData[3];
                    stMessagesTx.Data5 = txMsg.bData[4];
                    stMessagesTx.Data6 = txMsg.bData[5];
                    stMessagesTx.Data7 = txMsg.bData[6];
                    stMessagesTx.Data8 = txMsg.bData[7];
                    Console.WriteLine("Sending TX MSG: ARB ID = " + icsNeoDll.ConvertToHex(Convert.ToString(txMsg.lArbId)) + " Data : " + BitConverter.ToString(txMsg.bData));
                    // Transmit the assembled message
                    lResult = icsNeoDll.icsneoTxMessages(m_hObject, ref stMessagesTx, (int)lNetworkID, 1);
                    if (lResult != 1)
                    {
                        Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!! Problem Transmitting Message");
                        await Task.Delay(2000);

                        continue;
                    }
                    timeDelay.Restart();
                }
                else
                {
                    Console.WriteLine("No TX MSG in Queue");
                }

                if (lResult == 1 && iNumberOfMessages > 0)
                {
                    // send the collected raw data bytes for processing
                    //ProcessReceivedMsg(Encoding.ASCII.GetBytes(sMsgData));
                    //Console.WriteLine("Num of Msg = {0}", iNumberOfMessages);
                    ProcessReceivedMsg(iNumberOfMessages, ToByteArray(stMessages));

                    Array.Clear(stMessages, 0, stMessages.Length);
                    iNumberOfMessages = -1;
                    lResult           = 0;
                }

                uiTimeout = 0;
                iElapsed  = Convert.ToInt32(timeDelay.Elapsed.TotalMilliseconds);
                if (iElapsed < Constants.RX_Timeout)
                {
                    uiTimeout = Constants.RX_Timeout - iElapsed;
                }
                else if (iElapsed < Constants.DAQ_Interval)
                {
                    uiTimeout = Constants.DAQ_Interval - iElapsed;
                }

                tCANWait.Restart();
                //Console.WriteLine("Ui Timeout before if {0}", uiTimeout);
                if (uiTimeout > 0)
                {
                    //Console.WriteLine("UI Timeout = {0}", uiTimeout);
                    if (icsNeoDll.icsneoWaitForRxMessagesWithTimeOut(m_hObject, Convert.ToUInt32(uiTimeout)) == 1)
                    {
                        // Message reveived success
                        if (tCANWait.Elapsed.TotalMilliseconds < uiTimeout)
                        {
                            //Console.WriteLine("########### Waiting for Timeout");
                            await Task.Delay((uiTimeout - Convert.ToInt32(tCANWait.Elapsed.TotalMilliseconds)));
                        }
                        Console.WriteLine("####### Message Received:: Time taken : {0} ########", timeDelay.Elapsed.TotalMilliseconds);
                    }
                    lResult = icsNeoDll.icsneoGetMessages(m_hObject, ref stMessages[0], ref iNumberOfMessages, ref iNumberOfErrors);
                }

                iElapsed = Convert.ToInt32(timeDelay.Elapsed.TotalMilliseconds);
                if (iElapsed < Constants.DAQ_Interval)
                {
                    //Console.WriteLine("Task Delay time = {0}", (Constants.DAQ_Interval - iElapsed));
                    await Task.Delay((Constants.DAQ_Interval - iElapsed));
                }
                //Console.WriteLine("End iElapsed = {0}", iElapsed);
            }
        }