示例#1
0
 public override void Flush()
 {
     EASYSYNC.canusb_Flush(m_deviceHandle);
 }
示例#2
0
 override public void clearTransmitBuffer()
 {
     EASYSYNC.canusb_Flush(m_deviceHandle);
 }
示例#3
0
        /// <summary>
        /// The open method tries to connect to both busses to see if one of them is connected and
        /// active. The first strategy is to listen for any CAN message. If this fails there is a
        /// check to see if the application is started after an interrupted flash session. This is
        /// done by sending a message to set address and length (only for P-bus).
        /// </summary>
        /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is
        /// returned.</returns>
        override public OpenResult open()
        {
            if (m_deviceHandle != 0)
            {
                close();
            }
            // get first adapter
            //char[] adapter = new char[10];
            StringBuilder adapter = new StringBuilder(10);

            int result = EASYSYNC.canusb_getFirstAdapter(adapter, 10);

            if (result > 0)
            {
                Console.WriteLine("Number of adapters found: " + result.ToString("X8"));
                Console.WriteLine("Adapter serialnumber: " + adapter);
                m_deviceHandle = EASYSYNC.canusb_Open(null,
                                                            //"0x00:0xB9:0x07",
                                                            //"00:B9:07",
                                                      "00B907",
                                                            //"600",
                                                            //null,
                                                      null, //EASYSYNC.CANUSB_ACCEPTANCE_CODE_ALL,
                                                      null, //EASYSYNC.CANUSB_ACCEPTANCE_MASK_ALL,
                                                      EASYSYNC.CANUSB_FLAG_TIMESTAMP);
                Console.WriteLine("Handle: " + m_deviceHandle.ToString("X8"));
                if (m_deviceHandle > 0 && m_deviceHandle != 0xFFFFFFFD)
                {
                    EASYSYNC.canusb_Flush(m_deviceHandle);
                    Console.WriteLine("Creating new reader thread");

                    StringBuilder sb = new StringBuilder(100);
                    EASYSYNC.canusb_VersionInfo(m_deviceHandle, sb);
                    Console.WriteLine("Versionifo: " + sb.ToString());

                    Console.WriteLine("Status: " + EASYSYNC.canusb_Status(m_deviceHandle).ToString("X8"));

                    m_readThread = new Thread(readMessages);
                    try
                    {
                        m_readThread.Priority = ThreadPriority.Normal; // realtime enough
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.Message);
                    }
                    if (m_readThread.ThreadState == ThreadState.Unstarted)
                    {
                        m_readThread.Start();
                    }
                    return(OpenResult.OK);
                }
                else
                {
                    // second try after unload of dlls?

                    //close();
                    return(OpenResult.OpenError);
                }
            }
            return(OpenResult.OpenError);
        }
示例#4
0
 override public void clearReceiveBuffer()
 {
     EASYSYNC.canusb_Flush(m_deviceHandle);
 }
示例#5
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();
//            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("X2"));
                return(false);
            }
        }