Пример #1
0
        //Reads message data from user input and writes a message to the channel
        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            int id = Convert.ToInt32(idBox.Text);

            TextBox[] textBoxes = { dataBox0, dataBox1, dataBox2, dataBox3, dataBox4,
                                    dataBox5, dataBox6, dataBox7 };
            byte[]    data = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                data[i] = textBoxes[i].Text == "" ? (byte)0 : Convert.ToByte(textBoxes[i].Text);
            }

            CheckBox[] boxes = { RTRBox,   STDBox,  EXTBox,   WakeUpBox, NERRBox, errorBox,
                                 TXACKBox, TXRQBox, delayBox, BRSBox,    ESIBox };
            int        flags = 0;

            foreach (CheckBox box in boxes)
            {
                if (box.IsChecked.Value)
                {
                    flags += Convert.ToInt32(box.Tag);
                }
            }

            int dlc = Convert.ToInt32(DLCBox.Text);

            string msg = String.Format("{0}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}   to handle {10}",
                                       id, dlc, data[0], data[1], data[2], data[3], data[4],
                                       data[5], data[6], data[7], handle);

            Canlib.canStatus status = Canlib.canWrite(handle, id, data, dlc, flags);
            CheckStatus("Writing message " + msg, status);
        }
Пример #2
0
        //close the can bus after transmission
        public static void Close()
        {
            for (int i = 0; i < CAN_Channel.numOfChan; i++)
            {
                switch (CAN_Channel._INTERFACEs[i])
                {
                case CAN_Channel.CAN_INTERFACE.KVASER:

                    Canlib.canBusOff(canHandle[i]);
                    status       = Canlib.canClose(canHandle[i]);
                    canHandle[i] = 0;
                    KvaserInit   = false;

                    ErrorControl(status: status, location: "canClose: Close()");
                    break;

                case CAN_Channel.CAN_INTERFACE.PEAK:

                    PCANBasic.Uninitialize((ushort)canHandle[i]);

                    break;
                }
            }

            CanInit = false;
        }
 void closeChannelButton_Click(object sender, RoutedEventArgs e)
 {
     if (sendHandle >= 0 && readHandle >= 0)
     {
         if (isSenderBusOn || isReaderBusOn)
         {
             busOffButton_Click(sender, e);
         }
         Canlib.canStatus senderStatus = Canlib.canClose(sendHandle);
         Canlib.canStatus readerStatus = Canlib.canClose(readHandle);
         CheckStatus("Closing channel " + channelName, senderStatus);
         sendHandle = -1;
         readHandle = -1;
         closeChannelButton.IsEnabled = false;
         busOnButton.IsEnabled        = false;
         busOffButton.IsEnabled       = false;
         bitrateComboBox.IsEnabled    = false;
         sendMessageButton.IsEnabled  = false;
         clearMessageButton.IsEnabled = false;
         buildMessageButton.IsEnabled = false;
         openChannelButton.IsEnabled  = true;
         //channelBox.IsEnabled = true;
         channelComboBox.IsEnabled = true;
     }
     else
     {
         MessageBox.Show("Closing Channel " + channelName + " failed. Please try again.");
     }
 }
Пример #4
0
 //This method prints an error if something goes wrong
 private void CheckStatus(Canlib.canStatus status, string method)
 {
     if (status < 0)
     {
         send2Terminal(method + " failed: " + status.ToString());
     }
 }
        /* This method is invoked when application need to Bus off without user's click (on the 'Bus Off' button). */
        void forceAllBusOff()
        {
            Canlib.canStatus senderStatus = Canlib.canBusOff(sendHandle);
            Canlib.canStatus readerStatus = Canlib.canBusOff(readHandle);
            //CheckStatus("Bus off", status);
            if (senderStatus == Canlib.canStatus.canOK && readerStatus == Canlib.canStatus.canOK)
            {
                isSenderBusOn = false;
                isReaderBusOn = false;

                busOffButton.IsEnabled       = false;
                sendMessageButton.IsEnabled  = false;
                clearMessageButton.IsEnabled = false;
                buildMessageButton.IsEnabled = false;
                busOnButton.IsEnabled        = true;
                bitrateComboBox.IsEnabled    = true;

                MessageBox.Show("Forced Bus off succeeded. Go Bus on again.");
            }
            else
            {
                MessageBox.Show("Forced Bus off failed. Restart application again.");
                this.Close();
            }
        }
Пример #6
0
 //Goes on bus
 public void busOn()
 {
     Canlib.canStatus status = Canlib.canBusOn(handle);
     if (status == Canlib.canStatus.canOK)
     {
         onBus = true;
     }
 }
Пример #7
0
 // The check method takes a canStatus (which is an enumerable) and the method
 // name as a string argument. If the status is an error code, it will print it.
 // Most Canlib method return a status, and checking it with a method like this
 // is a useful practice to avoid code duplication.
 private static void CheckStatus(Canlib.canStatus status, string method)
 {
     if (status < 0)
     {
         string errorText;
         Canlib.canGetErrorText(status, out errorText);
         Debug.WriteLine(method + " failed: " + errorText);
     }
 }
Пример #8
0
 private void OpenChannel(out int hnd, int bitrate)
 {
     logger.Debug("hnd = canlibCLSNET.Canlib.canOpenChannel()");
     hnd = Canlib.canOpenChannel(ChannelNumber, 0);
     logger.Debug("canlibCLSNET.Canlib.canSetBusParams(hnd)");
     Canlib.canStatus statusSetParam = Canlib.canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0);
     logger.Debug("canlibCLSNET.Canlib.canBusOn(hnd)");
     Canlib.canStatus statusOn = Canlib.canBusOn(hnd);
     Canlib.canIoCtl(hnd, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
 }
Пример #9
0
 private void OpenChannelWithParamsC200(out int hnd, byte btr0, byte btr1)
 {
     logger.Debug("hnd = canlibCLSNET.Canlib.canOpenChannel()");
     hnd = Canlib.canOpenChannel(ChannelNumber, 0);
     logger.Debug("canlibCLSNET.Canlib.canSetBusParams(hnd)");
     Canlib.canStatus statusSetParam = Canlib.canSetBusParamsC200(hnd, btr0, btr1);
     logger.Debug("canlibCLSNET.Canlib.canBusOn(hnd)");
     Canlib.canStatus statusOn = Canlib.canBusOn(hnd);
     Canlib.canIoCtl(hnd, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
 }
Пример #10
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()
        {
            Canlib.canInitializeLibrary();

            //Check if bus is connected
            if (isOpen())
            {
                close();
            }
            Thread.Sleep(200);
            m_readThread = new Thread(readMessages)
            {
                Name = "KvaserCANDevice.m_readThread"
            };

            m_endThread = false;

            // Take first adapter...
            string[] adapters = GetAdapterNames();
            if (adapters.Length == 0)
            {
                return(OpenResult.OpenError);
            }
            SetSelectedAdapter(adapters[0]);

            //Check if P bus is connected
            logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()");
            handleWrite = Canlib.canOpenChannel(ChannelNumber, 0);
            logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)");
            Canlib.canStatus statusSetParamWrite = Canlib.canSetBusParamsC200(handleWrite, 0x40, 0x37);
            logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)");
            Canlib.canStatus statusOnWrite = Canlib.canBusOn(handleWrite);
            Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);
            //
            logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()");
            handleRead = Canlib.canOpenChannel(ChannelNumber, 0);
            logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)");
            Canlib.canStatus statusSetParamRead = Canlib.canSetBusParamsC200(handleRead, 0x40, 0x37);
            logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)");
            Canlib.canStatus statusOnRead = Canlib.canBusOn(handleRead);
            Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0);

            if (handleWrite < 0 || handleRead < 0)
            {
                return(OpenResult.OpenError);
            }

            logger.Debug("P bus connected");
            if (m_readThread.ThreadState == ThreadState.Unstarted)
            {
                m_readThread.Start();
            }
            return(OpenResult.OK);
        }
 //Goes on bus
 public void busOn()
 {
     Canlib.canStatus status = Canlib.canBusOn(handle);
     if (status == Canlib.canStatus.canOK)
     {
         onBus = true;
         if (!dumper.IsBusy)
         {
             dumper.RunWorkerAsync();
         }
     }
 }
Пример #12
0
        //Sets the bitrate
        private void setBitrateButton_Click(object sender, RoutedEventArgs e)
        {
            int[] bitrates = new int[4] {
                Canlib.canBITRATE_125K, Canlib.canBITRATE_250K,
                Canlib.canBITRATE_500K, Canlib.BAUD_1M
            };
            int bitrate = bitrates[bitrateBox.SelectedIndex];

            Canlib.canStatus status = Canlib.canSetBusParams(handle, bitrate, 0, 0, 0, 0, 0);

            CheckStatus("Setting bitrate to " + ((ComboBoxItem)bitrateBox.SelectedValue).Content, status);
        }
Пример #13
0
        //Sets the bitrate
        private void setBitrate_Button_Click(object sender, EventArgs e)
        {
            int[] bitrates = new int[4] {
                Canlib.canBITRATE_125K, Canlib.canBITRATE_250K,
                Canlib.canBITRATE_500K, Canlib.BAUD_1M
            };
            int bitrate = bitrates[bitrateText.SelectedIndex];

            Canlib.canStatus status = Canlib.canSetBusParams(handle, bitrate, 0, 0, 0, 0, 0);

            CheckStatus("Setting bitrate to " + bitrateText.SelectedValue, status);
        }
Пример #14
0
 private void CheckStatus(String action, Canlib.canStatus status)
 {
     if (status != Canlib.canStatus.canOK)
     {
         String errorText = "";
         Canlib.canGetErrorText(status, out errorText);
         ECULog.Info(action + " failed: " + errorText);
     }
     else
     {
         ECULog.Info(action + " succeeded");
     }
 }
Пример #15
0
 /*
  * Updates the status bar, prints error message if something goes wrong
  */
 private void CheckStatus(String action, Canlib.canStatus status)
 {
     if (status != Canlib.canStatus.canOK)
     {
         String errorText = "";
         Canlib.canGetErrorText(status, out errorText);
         statusText.Text = action + " failed: " + errorText;
     }
     else
     {
         statusText.Text = action + " succeeded";
     }
 }
Пример #16
0
        public FuctionResult Close()
        {
            if (handle != -1)
            {
                Canlib.canStatus cs = Canlib.canBusOff(handle);
                CheckStatus("Bus off", cs);
            }
            onBus = false;
            Canlib.canStatus cs1 = Canlib.canClose(handle);
            CheckStatus("Closing channel", cs1);
            handle = -1;

            return(FuctionResult.OK);
        }
Пример #17
0
        /// <summary>
        /// CAN发送一帧数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dat"></param>
        /// <param name="dlc"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public bool WriteData(int id, byte[] dat, int dlc, out long time)
        {
            time = 0;
            Canlib.canStatus canStatus = Canlib.canWrite(canHandler, id, dat, dlc, 0);
            if (canStatus != Canlib.canStatus.canOK)
            {
                return(false);
            }
            int ttime = 0;

            Canlib.kvReadTimer(canHandler, out ttime);
            time = (long)ttime;
            return(true);
        }
Пример #18
0
        // ***********************
        // Return a string that includes the formatting for an ErrorMsg that can be displayed; for CAN bus
        // ***********************
        public static string ErrorMsg(string Location, Canlib.canStatus status)
        {
            string ErrorMsg = "";

            if (status == 0) //in can lib 0 is the OK status
            {
                ErrorMsg = Location + ": Success";
            }
            else // all other status values are errors
            {
                ErrorMsg = Location + ": Fail : Error Code:" + status;
            }

            return(ErrorMsg);
        }
Пример #19
0
        //Goes on bus
        private void busOnButton_Click(object sender, RoutedEventArgs e)
        {
            Canlib.canStatus status = Canlib.canBusOn(handle);
            CheckStatus("Bus on", status);
            if (status == Canlib.canStatus.canOK)
            {
                onBus = true;

                //This starts the DumpMessageLoop method
                if (!dumper.IsBusy)
                {
                    dumper.RunWorkerAsync();
                }
            }
        }
Пример #20
0
        public FuctionResult SendShortMsg(int id, byte[] data, int dlc)
        {
            if (data.Length != 8)
            {
                return(FuctionResult.ERROR_UNKNOWN);
            }
            string msg = String.Format("{0}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}   to handle {10}",
                                       id, dlc, data[0], data[1], data[2], data[3], data[4],
                                       data[5], data[6], data[7], handle);

            Canlib.canStatus status = Canlib.canWrite(handle, id, data, dlc, flags);
            CheckStatus("Writing message " + msg, status);

            return(FuctionResult.OK);
        }
Пример #21
0
        public Message ReadMessage(int readHandle, out Canlib.canStatus status)
        {
            status = Canlib.canStatus.canOK;
            Message m = null;
            int     id;

            byte[] data = new byte[8];
            int    dlc;
            int    flags;
            long   time;

            status = Canlib.canReadWait(readHandle, out id, data, out dlc, out flags, out time, 50);
            m      = new Message(id, data, dlc, flags, time);

            return(m);
        }
Пример #22
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)
        {
            byte[] msg = a_message.getDataAsByteArray();

            writeStatus = Canlib.canWrite(handleWrite, (int)a_message.getID(), msg, a_message.getLength(), 0);

            if (writeStatus == Canlib.canStatus.canOK)
            {
                return(true);
            }
            else
            {
                logger.Debug(String.Format("tx failed with status {0}", writeStatus));
                return(false);
            }
        }
        //Reads message data from user input and writes a message to the channel, sending to the CAN bus.
        void sendMessageButton_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(idBox.Text))
            {
                MessageBox.Show("Tx Failed: CAN message ID has not been specified.");
                return;
            }
            if (String.IsNullOrEmpty(DLCBox.Text))
            {
                MessageBox.Show("Tx Failed: DLC value has not been specified.");
                return;
            }

            string idStr_decimal = Utils.ConvertHexStrToDecStr(idBox.Text);
            int    id_decimal    = Convert.ToInt32(idStr_decimal);

            int dlc = Convert.ToInt32(DLCBox.Text);

            byte[] data = new byte[Defined.MAX_DLC];
            Array.Clear(data, 0, sizeof(byte) * Defined.MAX_DLC);
            for (int i = 0; i < dlc; i++)
            {
                data[i] = canDataBoxes[i].Text == "" ? (byte)0 : Convert.ToByte(canDataBoxes[i].Text);
            }

            int messageOptionFlags = 0;

            if (messageOptionCheckboxPanel.IsVisible)
            {
                foreach (CheckBox box in messageOptionCheckboxPanel.Children)
                {
                    if (box.IsChecked.Value)
                    {
                        messageOptionFlags += Convert.ToInt32(box.Tag);
                    }
                }
            }

            string msgLog = String.Format("0x{0:x3}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}   to handle {10}",
                                          idBox.Text, DLCBox.Text, data[0], data[1], data[2], data[3], data[4],
                                          data[5], data[6], data[7], sendHandle);

            Canlib.canStatus status = Canlib.canWrite(sendHandle, id_decimal, data, dlc, messageOptionFlags);
            LogOutput("[Tx] " + msgLog);
            CheckStatus("[Tx] " + msgLog, status);
        }
Пример #24
0
        public bool CAN_SetBusParameter(int seg1, int seg2)
        {
            int sjw      = 0;
            int nosampl  = 0;
            int syncmode = 0;

            // 속도설정은 고정으로...
            Canlib.canStatus cs = Canlib.canSetBusParams(handle, Canlib.canBITRATE_500K, seg1, seg2, sjw, nosampl, syncmode);
            CheckStatus("CAN SetBusParameter", cs);
            if (cs == Canlib.canStatus.canOK)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        //Goes on bus
        void busOnButton_Click(object sender, RoutedEventArgs e)
        {
            //Set Bitrate
            int[] bitrates = new int[] { Canlib.canBITRATE_125K, Canlib.canBITRATE_250K,
                                         Canlib.canBITRATE_500K, Canlib.BAUD_1M };
            Canlib.canStatus senderStatus = Canlib.canSetBusParams(sendHandle, bitrates[bitrateComboBox.SelectedIndex], 0, 0, 0, 0, 0);
            Canlib.canStatus readerStatus = Canlib.canSetBusParams(readHandle, bitrates[bitrateComboBox.SelectedIndex], 0, 0, 0, 0, 0);

            CheckStatus("Setting bitrate to " + ((ComboBoxItem)bitrateComboBox.SelectedValue).Content, senderStatus);
            if (senderStatus != Canlib.canStatus.canOK || readerStatus != Canlib.canStatus.canOK)
            {
                return;
            }

            //Bus On
            senderStatus = Canlib.canBusOn(sendHandle);
            readerStatus = Canlib.canBusOn(readHandle);
            CheckStatus("Bus on with bitrate " + ((ComboBoxItem)bitrateComboBox.SelectedValue).Content, senderStatus);
            if (senderStatus == Canlib.canStatus.canOK && readerStatus == Canlib.canStatus.canOK)
            {
                isSenderBusOn = true;
                isReaderBusOn = true;

                //This starts the DumpMessageLoop method
                if (!messageReceiver.IsBusy)
                {
                    messageReceiver.RunWorkerAsync();
                }

                busOnButton.IsEnabled        = false;
                bitrateComboBox.IsEnabled    = false;
                busOffButton.IsEnabled       = true;
                sendMessageButton.IsEnabled  = true;
                clearMessageButton.IsEnabled = true;
                buildMessageButton.IsEnabled = true;
            }
            else
            {
                MessageBox.Show("Bus On failed. Please try again.");
            }
        }
        void busOffButton_Click(object sender, RoutedEventArgs e)
        {
            Canlib.canStatus senderStatus = Canlib.canBusOff(sendHandle);
            Canlib.canStatus readerStatus = Canlib.canBusOff(readHandle);
            CheckStatus("Bus off", senderStatus);
            if (senderStatus == Canlib.canStatus.canOK && readerStatus == Canlib.canStatus.canOK)
            {
                isSenderBusOn = false;
                isReaderBusOn = false;

                busOffButton.IsEnabled       = false;
                sendMessageButton.IsEnabled  = false;
                clearMessageButton.IsEnabled = false;
                buildMessageButton.IsEnabled = false;
                busOnButton.IsEnabled        = true;
                bitrateComboBox.IsEnabled    = true;
            }
            else
            {
                MessageBox.Show("Bus Off failed. Please try again.");
            }
        }
Пример #27
0
        static private void ErrorControl(int handle = 1, Canlib.canStatus status = Canlib.canStatus.canOK, string location = "\0")
        {
            if (handle < 0)
            {
                //get the error message
                Canlib.canGetErrorText((Canlib.canStatus)handle, out string msg);

                //write the error message and the location it occurred.
                Console.WriteLine("Handle error: " + msg + " \nThe location is: " + location);

                //close this interface
                Close();
            }

            if (status != Canlib.canStatus.canOK)
            {
                Console.WriteLine("A Can operation has failed: " + location);

                System.Windows.Forms.MessageBox.Show("A Can operation has failed: " + location, "Error",
                                                     System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
Пример #28
0
        //Reads message data from user input and writes a message to the channel
        private void sendMessage_Button_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(idBox.Text, 16);

            TextBox[] textBoxes = { dataBox0, dataBox1, dataBox2, dataBox3, dataBox4,
                                    dataBox5, dataBox6, dataBox7 };
            byte[]    data = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                data[i] = textBoxes[i].Text == "" ? (byte)0 : Convert.ToByte(textBoxes[i].Text);
            }

            int flags = Convert.ToInt32(flagsBox.Text);

            int dlc = Convert.ToInt32(DLCBox.Text);

            string msg = String.Format("{0}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}   to handle {10}",
                                       idBox.Text, dlc, data[0], data[1], data[2], data[3], data[4],
                                       data[5], data[6], data[7], handle);

            Canlib.canStatus status = Canlib.canWrite(handle, id, data, dlc, flags);
            CheckStatus("Writing message " + msg, status);
        }
Пример #29
0
        /// <summary>
        /// The close method closes the device.
        /// </summary>
        /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns>
        override public CloseResult close()
        {
            m_endThread = true;

            Canlib.canStatus statusBusOff1   = Canlib.canStatus.canOK;
            Canlib.canStatus statusBusOff2   = Canlib.canStatus.canOK;
            Canlib.canStatus statusCanClose1 = Canlib.canStatus.canOK;
            Canlib.canStatus statusCanClose2 = Canlib.canStatus.canOK;

            if (handleWrite >= 0)
            {
                statusBusOff1 = Canlib.canBusOff(handleWrite);
                logger.Debug("canlibCLSNET.Canlib.canBusOff(handleWrite)");
                statusCanClose1 = Canlib.canClose(handleWrite);
                logger.Debug("canlibCLSNET.Canlib.canClose(handleWrite)");
            }

            if (handleRead >= 0)
            {
                statusBusOff2 = Canlib.canBusOff(handleRead);
                logger.Debug("canlibCLSNET.Canlib.canBusOff(handleRead)");
                statusCanClose2 = Canlib.canClose(handleRead);
                logger.Debug("canlibCLSNET.Canlib.canClose(handleRead)");
            }

            handleWrite = -1;
            handleRead  = -1;
            if (Canlib.canStatus.canOK == statusBusOff1 && Canlib.canStatus.canOK == statusBusOff2 &&
                Canlib.canStatus.canOK == statusCanClose1 && Canlib.canStatus.canOK == statusCanClose2)
            {
                return(CloseResult.OK);
            }
            else
            {
                return(CloseResult.CloseError);
            }
        }
Пример #30
0
        static void listDevices()
        {
            Int16 i;
              Canlib.canStatus status = new Canlib.canStatus();
              int channel_count = 0;

              Canlib.canGetNumberOfChannels(out channel_count);

              Console.WriteLine("First argument must be a channel!\n");
              Console.WriteLine("Channel\t Name");
              Console.WriteLine("--------------------------------------------------------");
              for (i = 0; (status == Canlib.canStatus.canOK) && (i < channel_count); i++)
            {
              object chData;
              status = Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_CHANNEL_NAME, out chData);
              Console.WriteLine(chData.ToString());
            }
        }
Пример #31
0
 private void closeButton_Click(object sender, RoutedEventArgs e)
 {
     Canlib.canStatus status = Canlib.canClose(handle);
     CheckStatus("Closing channel", status);
     handle = -1;
 }
Пример #32
0
        static int Main(string[] args)
        {
            String password = "";
              string xml_config = "";
              Kvrlib.Status kvrstat;
              Kvrlib.ConfigHnd configHandle = new Kvrlib.ConfigHnd();
              Kvrlib.Address address = new Kvrlib.Address();
              Kvrlib.CipherInfoElement cipherInfoElement = new Kvrlib.CipherInfoElement();

              Int32 i;
              Int32 cur_profile;
              Int32 canlib_channel;
              Int32 no_profiles;

              object serial;
              object ean;

              Canlib.canStatus stat = new Canlib.canStatus();

              Canlib.canInitializeLibrary();
              Kvrlib.InitializeLibrary();

              switch (args.Length)
            {
            case 0:
              listDevices();
              return 0;
            case 1:
              canlib_channel = args[0][0] - '0';
              break;
            default:
              listDevices();
              return -1;
            }

              stat = Canlib.canGetChannelData(canlib_channel, Canlib.canCHANNELDATA_CARD_UPC_NO,out ean);
              stat = Canlib.canGetChannelData(canlib_channel, Canlib.canCHANNELDATA_CARD_SERIAL_NO, out serial);

              if (isAvailibleForConfig(canlib_channel, password))
            {
              Console.WriteLine("Channel " + canlib_channel + " is availible for configuration");
            }
              else
            {
              Console.WriteLine("Channel " + canlib_channel + " can not be opend for configuration");
              Canlib.canUnloadLibrary();
              return -1;
            }
              Canlib.canUnloadLibrary();

              //----------------------------------------------------------------------------
              // List number of profiles

              kvrstat = Kvrlib.ConfigNoProfilesGet(canlib_channel, out no_profiles);
              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Device does not support profiles " + kvrstat);
              return Convert.ToInt32(kvrstat);
            }
              Console.WriteLine("Device supports " + no_profiles + " profiles.");

              Kvrlib.ConfigActiveProfileGet(canlib_channel, out cur_profile);
              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Could not get active profile number " + kvrstat);
              return Convert.ToInt32(kvrstat);
            }
              Console.WriteLine("Active profile is: " + cur_profile);

              // Show profiles
              for (i = 0; i < no_profiles; i++)
            {
              kvrstat = Kvrlib.ConfigInfoGet(canlib_channel, i, out xml_config);
              if (kvrstat.Equals(Kvrlib.Status.BLANK))
            {
              Console.WriteLine("Profile " + i + " is blank.");
            }
              else if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Could not read profile " + i);
              return Convert.ToInt32(kvrstat);
            }
              else
            {
              Console.WriteLine("Profile " + i + ":\n" + xml_config);
            }
            }

              //----------------------------------------------------------------------------
              // Start configuration - read only
              kvrstat = Kvrlib.ConfigOpenEx(canlib_channel,
                                    Kvrlib.ConfigMode.R,
                                    password,
                                    out configHandle,
                                    cur_profile);

              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Could not start config " + kvrstat);
              return Convert.ToInt32(kvrstat);
            }

              // List available networks. This information could be
              // helpful when creating the XML configuration.

              kvrstat = doScanNetwork(configHandle);
              Kvrlib.ConfigClose(configHandle);

              //----------------------------------------------------------------------------
              // Start configuration - read/write

              kvrstat = Kvrlib.ConfigOpen(canlib_channel, Kvrlib.ConfigMode.RW, password, out configHandle);

              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Could not start config " + kvrstat);
              return Convert.ToInt32(kvrstat);
            }

              kvrstat = doConfigure(configHandle);

              // Done!
              Kvrlib.ConfigClose(configHandle);

              // Wait for reboot
              if (waitForDevice(ean, serial, 10000) != 0)
            { //10s timeout
              Console.WriteLine("waitForDevice() failed.");
              return -1;
            }

              //----------------------------------------------------------------------------
              // Start configuration - read only
              kvrstat = Kvrlib.ConfigOpen(canlib_channel, Kvrlib.ConfigMode.R, password, out configHandle);

              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Could not start config " + kvrstat);
              return Convert.ToInt32(kvrstat);
            }

              kvrstat = doTryConfigure(configHandle, 5, ean, serial);
              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("doTryConfiguration failed (" + kvrstat + ")");
              return Convert.ToInt32(kvrstat);
            }
              Kvrlib.ConfigClose(configHandle);
              Console.WriteLine("\nDone!");

              Console.WriteLine("Clear the last profile");
              kvrstat = Kvrlib.ConfigActiveProfileSet(canlib_channel, no_profiles - 1);
              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Set profile " + (no_profiles - 1) + " failed (" + kvrstat + ")");
              return Convert.ToInt32(kvrstat);
            }

              // Wait for reboot
              if (waitForDevice(ean, serial, 10000) != 0)
            { //10s timeout
              Console.WriteLine("waitForDevice() failed.");
              return -1;
            }

              eraseConfiguration(canlib_channel, password);

              // Wait for reboot
              if (waitForDevice(ean, serial, 10000) != 0)
            { //10s timeout
              Console.WriteLine("waitForDevice() failed.");
              return -1;
            }

              Console.WriteLine("Set profile 0 as active");
              kvrstat = Kvrlib.ConfigActiveProfileSet(canlib_channel, 0);
              if (!kvrstat.Equals(Kvrlib.Status.OK))
            {
              Console.WriteLine("Set profile " + 0 + " failed (" + kvrstat + ")");
              return Convert.ToInt32(kvrstat);
            }

              Kvrlib.UnloadLibrary();
              return 0;
        }