Esempio n. 1
0
        /*private void AddToCanTrace(string line)
         * {
         *  if (m_EnableCanLog)
         *  {
         *      DateTime dtnow = DateTime.Now;
         *      using (StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\CanTraceCANUSBDevice.txt", true))
         *      {
         *          sw.WriteLine(dtnow.ToString("dd/MM/yyyy HH:mm:ss") + " - " + line);
         *      }
         *  }
         * }*/
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override public bool sendMessage(CANMessage a_message)
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg.id    = a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.data  = a_message.getData();
            //Console.WriteLine("Data to send: " + msg.data.ToString("X16"));
            //Console.WriteLine("Data (original) to send: " + a_message.getData().ToString("X16"));

            int writeResult;

            AddToCanTrace("Sending message");
            writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == EASYSYNC.ERROR_CANUSB_OK)
            {
                AddToCanTrace("Message sent successfully");
                return(true);
            }
            else
            {
                switch (writeResult)
                {
                case EASYSYNC.ERROR_CANUSB_COMMAND_SUBSYSTEM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM");
                    break;

                case EASYSYNC.ERROR_CANUSB_INVALID_PARAM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM");
                    break;

                case EASYSYNC.ERROR_CANUSB_NO_MESSAGE:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE");
                    break;

                case EASYSYNC.ERROR_CANUSB_NOT_OPEN:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN");
                    break;

                case EASYSYNC.ERROR_CANUSB_OPEN_SUBSYSTEM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM");
                    break;

                case EASYSYNC.ERROR_CANUSB_TX_FIFO_FULL:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_TX_FIFO_FULL");
                    break;

                default:
                    AddToCanTrace("Message failed to send: " + writeResult.ToString());
                    break;
                }
                return(false);
            }
        }
Esempio n. 2
0
 public override void handleMessage(CANMessage a_message)
 {
     lock (m_canMessage)
     {
         if (a_message.getID() == m_waitMsgID)
         {
             m_canMessage.setData(a_message.getData());
             m_canMessage.setFlags(a_message.getFlags());
             m_canMessage.setID(a_message.getID());
             m_canMessage.setLength(a_message.getLength());
             m_canMessage.setTimeStamp(a_message.getTimeStamp());
             messageReceived = true;
             m_resetEvent.Set();
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override public bool sendMessage(CANMessage a_message)
        {
            lock (lockObj)
            {
                while (interfaceBusy)
                {
                    if (lastSentTimestamp < Environment.TickCount - timeoutWithoutReadyChar)
                    {
                        //Console.WriteLine("released");
                        break;
                    }
                }

                lastSentTimestamp = Environment.TickCount;
                interfaceBusy     = true;
                //Console.WriteLine("set");

                Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg();
                msg.id    = a_message.getID();
                msg.len   = a_message.getLength();
                msg.flags = a_message.getFlags();
                msg.data  = a_message.getData();

                if (m_serialPort.IsOpen)
                {
                    //m_serialPort.Write("\r");
                    string txstring = "t";
                    txstring += msg.id.ToString("X3");
                    txstring += "8"; // always 8 bytes to transmit
                    for (int t = 0; t < 8; t++)
                    {
                        byte b = (byte)(((msg.data >> t * 8) & 0x0000000000000000FF));
                        txstring += b.ToString("X2");
                    }
                    txstring += "\r";
                    AddToCanTrace(string.Format("TX: {0} {1} {2}", a_message.getID().ToString("X3"), a_message.getData().ToString("X16"), txstring));
                    m_serialPort.Write(txstring);
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override protected bool sendMessageDevice(CANMessage a_message)
        {
            Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg();
            msg.id    = a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.data  = a_message.getData();
            int writeResult;

            writeResult = Lawicel.CANUSB.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == Lawicel.CANUSB.ERROR_CANUSB_OK)
            {
                return(true);
            }
            else
            {
                logger.Debug("tx failed writeResult: " + writeResult);
                return(false);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// sendMessage send a CANMessage.
 /// </summary>
 /// <param name="a_message">A CANMessage.</param>
 /// <returns>true on success, othewise false.</returns>
 public override bool sendMessage(CANMessage a_message)
 {
     Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg();
     msg.id = a_message.getID();
     msg.len = a_message.getLength();
     msg.flags = a_message.getFlags();
     msg.data = a_message.getData();
     int writeResult;
     AddToCanTrace("TX: " + msg.id.ToString("X4") + " " + msg.data.ToString("X16"));
     writeResult = Lawicel.CANUSB.canusb_Write(m_deviceHandle, ref msg);
     if (writeResult == Lawicel.CANUSB.ERROR_CANUSB_OK)
     {
         AddToCanTrace("Message sent successfully");
         return true;
     }
     else
     {
         switch (writeResult)
         {
             case Lawicel.CANUSB.ERROR_CANUSB_COMMAND_SUBSYSTEM:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM");
                 break;
             case Lawicel.CANUSB.ERROR_CANUSB_INVALID_PARAM:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM");
                 break;
             case Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE");
                 break;
             case Lawicel.CANUSB.ERROR_CANUSB_NOT_OPEN:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN");
                 break;
             case Lawicel.CANUSB.ERROR_CANUSB_OPEN_SUBSYSTEM:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM");
                 break;
             case Lawicel.CANUSB.ERROR_CANUSB_TX_FIFO_FULL:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_TX_FIFO_FULL");
                 break;
             default:
                 AddToCanTrace("Message failed to send: " + writeResult.ToString());
                 break;
         }
         return false;
     }
 }
Esempio n. 6
0
 /// <summary>
 /// sendMessage send a CANMessage.
 /// </summary>
 /// <param name="a_message">A CANMessage.</param>
 /// <returns>true on success, othewise false.</returns>
 protected override bool sendMessageDevice(CANMessage a_message)
 {
     Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg();
     msg.id = a_message.getID();
     msg.len = a_message.getLength();
     msg.flags = a_message.getFlags();
     msg.data = a_message.getData();
     int writeResult;
     writeResult = Lawicel.CANUSB.canusb_Write(m_deviceHandle, ref msg);
     if (writeResult == Lawicel.CANUSB.ERROR_CANUSB_OK)
     {
         return true;
     }
     else
     {
         logger.Debug("tx failed writeResult: " + writeResult);
         return false;
     }
 }
Esempio n. 7
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        public override bool sendMessage(CANMessage a_message)
        {
            lock (lockObj)
            {
                while (interfaceBusy)
                {
                    if (lastSentTimestamp < Environment.TickCount - timeoutWithoutReadyChar)
                    {
                        //Console.WriteLine("released");
                        break;
                    }
                }

                lastSentTimestamp = Environment.TickCount;
                interfaceBusy = true;
                //Console.WriteLine("set");

                Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg();
                msg.id = a_message.getID();
                msg.len = a_message.getLength();
                msg.flags = a_message.getFlags();
                msg.data = a_message.getData();

                if (m_serialPort.IsOpen)
                {
                    //m_serialPort.Write("\r");
                    string txstring = "t";
                    txstring += msg.id.ToString("X3");
                    txstring += "8"; // always 8 bytes to transmit
                    for (int t = 0; t < 8; t++)
                    {
                        byte b = (byte)(((msg.data >> t * 8) & 0x0000000000000000FF));
                        txstring += b.ToString("X2");
                    }
                    txstring += "\r";
                    AddToCanTrace(string.Format("TX: {0} {1} {2}", a_message.getID().ToString("X3"), a_message.getData().ToString("X16"), txstring));
                    m_serialPort.Write(txstring);
                    return true;
                }
            }
            return false;
        }