Пример #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);
            }
        }
Пример #2
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)
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg.id    = (ushort)a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            ulong msgdata = a_message.getData();

            // store in data (ulong)

            /*byte databyte = a_message.getCanData(7);
             * msg.data_1 = GetDataMSBADCII(databyte);
             * msg.data_2 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(6);
             * msg.data_3 = GetDataMSBADCII(databyte);
             * msg.data_4 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(5);
             * msg.data_5 = GetDataMSBADCII(databyte);
             * msg.data_6 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(4);
             * msg.data_7 = GetDataMSBADCII(databyte);
             * msg.data_8 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(3);
             * msg.data_9 = GetDataMSBADCII(databyte);
             * msg.data_10 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(2);
             * msg.data_11 = GetDataMSBADCII(databyte);
             * msg.data_12 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(1);
             * msg.data_13 = GetDataMSBADCII(databyte);
             * msg.data_14 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(0);
             * msg.data_15 = GetDataMSBADCII(databyte);
             * msg.data_16 = GetDataLSBADCII(databyte);
             */

            msg.data = a_message.getData(); // this data should be in ascii: unsigned char data[16];			// Databytes 0...7
            // example:

            /*
             * msg.data[0]='A';
             *          msg.data[1]='1';
             *
             *          msg.data[2]='B';
             *          msg.data[3]='2';
             *
             *          msg.data[4]='C';
             *          msg.data[5]='3';
             *
             *          msg.data[6]='D';
             *          msg.data[7]='4';
             *
             *          msg.data[8]='E';
             *          msg.data[9]='5';
             *
             *          msg.data[10]='F';
             *          msg.data[11]='6';
             *
             *          msg.data[12]='1';
             *          msg.data[13]='2';
             *
             *          msg.data[14]='3';
             *          msg.data[15]='4'; * */
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }

            int writeResult;

            Console.WriteLine("Writing to handle: " + m_deviceHandle.ToString("X8"));
            writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg);

            if (writeResult == EASYSYNC.ERROR_CANUSB_OK)
            {
                return(true);
            }
            else
            {
                //EASYSYNC.canusb_Flush(m_deviceHandle);
                Console.WriteLine("Failed to send message: " + writeResult.ToString("X8"));
                return(false);
            }
        }