Create own GUI with USBtinLib for testing or simulating CAN bus devices

For testing or simulating CAN bus devices an own GUI is often useful. This page shows step-by-step how to create such an application with USBtinLib, the open source library for USBtin.

Contents

Setup NetBeans IDE project

  1. Download, install and start NetBeans IDE (tested with IDE 8.0.2 "All").
  2. "File" -> "New Project..."
  3. Choose Categories "Java" and Projects "Java Application" and click "Next".
    NetBeans New Project Dialog
  4. Set project name and location. Disable checkbox "Create Main Class". Click "Finish".
    NetBeans New Project Dialog Name and Location
  5. Right-click on project, "New" -> "Other..."
    Add jFrame to project
  6. Select category "Swing GUI Forms" and file type "JFrame Form". Click "Next".
    Add jFrame to project
  7. Input class name. Click "Finish".
    Set name and location of JFrame
  8. Now, the basic project is setup up and you can test the application with "Run" -> "Run Project" (shows blank frame).

Add libraries (USBtinLib and jSSC) to project

  1. Create new folder "lib" in file browser tab.
    Add library folder
  2. Download jar file of USBtinLib (direct download link: USBtinLib-1.1.0.jar) and save it to the "lib" folder.
  3. Download jar file of jSSC (Java Simple Serial Connector) (direct download link: jssc-2.8.0.jar) and save it to the "lib" folder". library folder
  4. Add libraries to project: right-click on "Libraries" -> "Add JAR/Folder..." and choose the two jar files.
    Add libraries
    Project libraries
  5. Add imports to main java class:
    import de.fischl.usbtin.*;
    import jssc.SerialPortList;
    and add member
    protected USBtin usbtin = new USBtin();

    Add imports

Add "Connect/Disconnect" button

  1. Open design view of main java class, add a combobox and change its name to "serialPort"
    Set combobox name
  2. Right-click on combobox -> "Properties" -> Click on "..." behind "model" text input field. Choose "Custom code" and input
    new javax.swing.DefaultComboBoxModel(SerialPortList.getPortNames())
    Set model of combobox
  3. Add button to design, change variable name to "connectionButton" and set text to "Connect".
    Add connection button
  4. Double click on button and implement connect/disconnect method (here, the CAN channel opens in ACTIVE mode with 100kBaud):
        private void connectionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
           
            if (connectionButton.getText().equals("Disconnect")) {
                try {
                    usbtin.closeCANChannel();
                    usbtin.disconnect();
                    System.out.println("Disconnected");
                } catch (USBtinException e) {
                    System.err.println(e.getMessage());
                }
                connectionButton.setText("Connect");
                serialPort.setEnabled(true);
            } else {
                try {
                    usbtin.connect((String) serialPort.getSelectedItem());
                    usbtin.openCANChannel(100000, USBtin.OpenMode.ACTIVE);
                    connectionButton.setText("Disconnect");
                    serialPort.setEnabled(false);
                    System.out.println("Connected to USBtin (FW" + usbtin.getFirmwareVersion() +
                                       "/HW" + usbtin.getHardwareVersion() + ")");
                } catch (USBtinException e) {
                    System.err.println(e.getMessage());
                }
            }
        }
    Action performed handler for connection button
  5. Time to test the application: plug in USBtin (tested with USBtin EB) and "Run" -> "Run Project". If you click the button, the USBtin led goes on and you can see the USBtin version in the NetBeans output window.
    Test connection

Send a CAN message

  1. Add new button, set text and set variable name to "sendButton".
    Add send button
  2. Double-click on button and implement handler:
        private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            
            try {            
                usbtin.send(new CANMessage(0x100, new byte[]{0x11, 0x22, 0x33}));            
            } catch (USBtinException e) {
                System.err.println(e.getMessage());
            }
        }
  3. Run the project an test it. Here a second USBtin EB is connected and opened in USBtinViewer (right window) to see the output of the first USBtin interface:
    Send test

Receive CAN messages

  1. Add text area and change variable name to "logTextArea".
    Add textarea for logging
  2. Switch to code view and add "CANMessageListener" to the class:
    public class USBtinLibGUIDemo extends javax.swing.JFrame implements CANMessageListener {
    Implement the listener method:
       @Override
        public void receiveCANMessage(CANMessage canmessage) {
            logTextArea.append(canmessage.toString() + "\n");
        }
    And finally register the message listener in the constructor:
    usbtin.addMessageListener(this);

    Add receive functions
  3. Run the project an test it. Here a second USBtin EB is connected and opened in USBtinViewer (right window) to send CAN messages to the first USBtin interface:
    Receive CAN message test

Add slider which sends CAN messages on change

  • Add slider and change variable name to "testSlider". Set properies for minimum and maximum.
    Add slider
  • Add "stateChanged" event to slider and implement event method:
        private void testSliderStateChanged(javax.swing.event.ChangeEvent evt) {                                        
            JSlider source = (JSlider)evt.getSource();
            if (!source.getValueIsAdjusting()) {
                try {            
                    usbtin.send(new CANMessage(0x101, new byte[]{(byte)source.getValue()}));     
                } catch (USBtinException e) {
                    System.err.println(e.getMessage());
                }            
            }
        }
    Add event to slider
  • Run the project an test it. Here a second USBtin EB is connected and opened in USBtinViewer (right window) to see the output of the first USBtin interface:
    Slider test
  • Simulate behavior of a CAN device

    Example: we simulate a CAN device which waits for messages with ID 0x001, sums the data bytes and send a CAN message with the sum as payload within ID 0x002.

  • Extend the listener method (see Receive CAN messages) to simulate our sum-device
        @Override
        public void receiveCANMessage(CANMessage canmessage) {
            logTextArea.append(canmessage.toString() + "\n");
            
            if (canmessage.getId() == 0x001) {
                byte sum = 0;
                for (byte b : canmessage.getData()) sum += b;
                
                try {            
                   usbtin.send(new CANMessage(0x002, new byte[]{sum}));     
                } catch (USBtinException e) {
                    System.err.println(e.getMessage());
                }
            }
        }
  • Run the project an test it. Here a second USBtin EB is connected and opened in USBtinViewer (right window) to send messages and see the output of the first USBtin interface:
    Simulation test