Browse Source

wip

master
Niklas 2 years ago
commit
1f5cc164ce
6 changed files with 937 additions and 0 deletions
  1. BIN
      lib/darcula.jar
  2. +80
    -0
      pom.xml
  3. +140
    -0
      src/main/java/de/gurkengewuerz/serialinterface/DashboardStatus.java
  4. +390
    -0
      src/main/java/de/gurkengewuerz/serialinterface/SerialGUI.form
  5. +290
    -0
      src/main/java/de/gurkengewuerz/serialinterface/SerialGUI.java
  6. +37
    -0
      src/main/java/de/gurkengewuerz/serialinterface/SerialInterface.java

BIN
lib/darcula.jar View File


+ 80
- 0
pom.xml View File

@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.gurkengewuerz</groupId>
<artifactId>serialinterface</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.bulenkov</groupId>
<artifactId>darcula</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/darcula.jar</systemPath>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}</finalName>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>${project.groupId}.${project.artifactId}.SerialInterface</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assamble</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

+ 140
- 0
src/main/java/de/gurkengewuerz/serialinterface/DashboardStatus.java View File

@@ -0,0 +1,140 @@
package de.gurkengewuerz.serialinterface;

import org.json.JSONObject;
import org.pmw.tinylog.Logger;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
* Created by gurkengewuerz.de on 08.01.2018.
*/
public class DashboardStatus {

private int fuel = 100;
private int rpm = 0;
private int kmh = 0;
private boolean backlight = true;
private boolean fogbeam = false;
private boolean highbeam = false;
private boolean handbrake = false;
private boolean abs = false;
private boolean oil_pressure = false;
private boolean tire_pressure = false;
private boolean kupplung = false;
private boolean trunk = false;
private boolean l_turn_signal = false;
private boolean r_turn_signal = false;
private boolean offroad = false;
private boolean water_temp = false;
private boolean seat_belt = false;
private boolean battery = false;
private boolean check_lamp = false;
private boolean doors = false;

public void setFuel(int fuel) {
this.fuel = fuel;
}

public void setRpm(int rpm) {
this.rpm = rpm;
}

public void setKmh(int kmh) {
this.kmh = kmh;
}

public void setBacklight(boolean backlight) {
this.backlight = backlight;
}

public void setFogbeam(boolean fogbeam) {
this.fogbeam = fogbeam;
}

public void setHighbeam(boolean highbeam) {
this.highbeam = highbeam;
}

public void setHandbrake(boolean handbrake) {
this.handbrake = handbrake;
}

public void setAbs(boolean abs) {
this.abs = abs;
}

public void setOil_pressure(boolean oil_pressure) {
this.oil_pressure = oil_pressure;
}

public void setTire_pressure(boolean tire_pressure) {
this.tire_pressure = tire_pressure;
}

public void setKupplung(boolean kupplung) {
this.kupplung = kupplung;
}

public void setTrunk(boolean trunk) {
this.trunk = trunk;
}

public void setL_turn_signal(boolean l_turn_signal) {
this.l_turn_signal = l_turn_signal;
}

public void setR_turn_signal(boolean r_turn_signal) {
this.r_turn_signal = r_turn_signal;
}

public void setOffroad(boolean offroad) {
this.offroad = offroad;
}

public void setWater_temp(boolean water_temp) {
this.water_temp = water_temp;
}

public void setSeat_belt(boolean seat_belt) {
this.seat_belt = seat_belt;
}

public void setBattery(boolean battery) {
this.battery = battery;
}

public void setCheck_lamp(boolean check_lamp) {
this.check_lamp = check_lamp;
}

public void setDoors(boolean doors) {
this.doors = doors;
}

public String generateJSON() {
JSONObject obj = new JSONObject();
obj.put("kmh", kmh);
obj.put("rpm", rpm);
obj.put("fuel", fuel);
obj.put("backlight", backlight);
obj.put("l_turn_signal", l_turn_signal);
obj.put("r_turn_signal", r_turn_signal);
obj.put("offroad", offroad);
obj.put("water_temp", water_temp);
obj.put("seat_belt", seat_belt);
obj.put("battery", battery);
obj.put("check_lamp", check_lamp);
obj.put("doors", doors);
obj.put("fogbeam", fogbeam);
obj.put("highbeam", highbeam);
obj.put("handbrake", handbrake);
obj.put("abs", abs);
obj.put("oil_pressure", oil_pressure);
obj.put("tire_pressure", tire_pressure);
obj.put("kupplung", kupplung);
obj.put("trunk", trunk);

return obj.toString();
}
}

+ 390
- 0
src/main/java/de/gurkengewuerz/serialinterface/SerialGUI.form View File

@@ -0,0 +1,390 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.gurkengewuerz.serialinterface.SerialGUI">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="1066" height="465"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<tabbedpane id="b373f" binding="tabbedPane1" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/>
</grid>
</constraints>
<properties>
<toolTipText value="Settings Panel"/>
</properties>
<border type="none"/>
<children>
<grid id="67de2" layout-manager="GridLayoutManager" row-count="2" column-count="8" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Settings"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b360f" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="4" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="3979f" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Listen on:"/>
</properties>
</component>
<component id="7841" class="javax.swing.JTextField" binding="a0000TextField" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="0.0.0.0"/>
</properties>
</component>
<component id="13719" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Port:"/>
</properties>
</component>
<component id="e23ad" class="javax.swing.JTextField" binding="a8652TextField" default-binding="true">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="8652"/>
</properties>
</component>
<grid id="7504f" binding="simulatorPanel" layout-manager="GridLayoutManager" row-count="9" column-count="7" same-size-horizontally="true" same-size-vertically="true" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e13cd" class="javax.swing.JSpinner" binding="fuelSpinner">
<constraints>
<grid row="0" column="1" row-span="1" col-span="4" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<toolTipText value="Set Fuel"/>
</properties>
</component>
<component id="7a85" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Fuellevel"/>
</properties>
</component>
<component id="d70a1" class="javax.swing.JCheckBox" binding="fogBeamCheckBox" default-binding="true">
<constraints>
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Fog Beam"/>
</properties>
</component>
<component id="e6f32" class="javax.swing.JCheckBox" binding="backlightCheckBox" default-binding="true">
<constraints>
<grid row="0" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Backlight"/>
</properties>
</component>
<component id="ef69f" class="javax.swing.JButton" binding="updateButton" default-binding="true">
<constraints>
<grid row="0" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Update"/>
</properties>
</component>
<component id="cc497" class="javax.swing.JCheckBox" binding="waterTempCheckBox" default-binding="true">
<constraints>
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Water Temp."/>
</properties>
</component>
<component id="cf2eb" class="javax.swing.JCheckBox" binding="highBeamCheckBox" default-binding="true">
<constraints>
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="High Beam"/>
</properties>
</component>
<component id="23b21" class="javax.swing.JCheckBox" binding="handbrakeCheckBox" default-binding="true">
<constraints>
<grid row="3" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Handbrake"/>
</properties>
</component>
<component id="5abb7" class="javax.swing.JCheckBox" binding="ABSCheckBox" default-binding="true">
<constraints>
<grid row="4" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="ABS"/>
</properties>
</component>
<component id="cf12a" class="javax.swing.JCheckBox" binding="oilPressureCheckBox" default-binding="true">
<constraints>
<grid row="5" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Oil Pressure"/>
</properties>
</component>
<component id="ad835" class="javax.swing.JCheckBox" binding="tirePressureCheckBox" default-binding="true">
<constraints>
<grid row="6" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Tire Pressure"/>
</properties>
</component>
<component id="16ce3" class="javax.swing.JCheckBox" binding="lTurnSignalCheckBox" default-binding="true">
<constraints>
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<selected value="false"/>
<text value="l. turn signal"/>
</properties>
</component>
<component id="410e9" class="javax.swing.JCheckBox" binding="rTurnSignalCheckBox" default-binding="true">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="r. turn signal"/>
</properties>
</component>
<component id="5857" class="javax.swing.JCheckBox" binding="offroadCheckBox" default-binding="true">
<constraints>
<grid row="3" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Offroad"/>
</properties>
</component>
<component id="d1169" class="javax.swing.JCheckBox" binding="seatBeltCheckBox" default-binding="true">
<constraints>
<grid row="5" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Seat Belt"/>
</properties>
</component>
<component id="4d16a" class="javax.swing.JCheckBox" binding="lowVoltageCheckBox" default-binding="true">
<constraints>
<grid row="6" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Low Voltage"/>
</properties>
</component>
<component id="6991a" class="javax.swing.JCheckBox" binding="checkLampCheckBox" default-binding="true">
<constraints>
<grid row="7" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&quot;Check Lamp&quot;"/>
</properties>
</component>
<component id="57cab" class="javax.swing.JCheckBox" binding="kupplungCheckBox" default-binding="true">
<constraints>
<grid row="7" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&quot;Kupplung&quot;"/>
</properties>
</component>
<component id="2486d" class="javax.swing.JCheckBox" binding="openDoorsCheckBox" default-binding="true">
<constraints>
<grid row="8" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Open Doors"/>
</properties>
</component>
<component id="c7825" class="javax.swing.JCheckBox" binding="openTrunkCheckBox" default-binding="true">
<constraints>
<grid row="8" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Open Trunk"/>
</properties>
</component>
<grid id="f8ce8" layout-manager="GridLayoutManager" row-count="3" column-count="8" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="8" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="99554" class="javax.swing.JSlider" binding="kmhSlider">
<constraints>
<grid row="0" column="4" row-span="3" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<inverted value="false"/>
<maximum value="240"/>
<orientation value="1"/>
<paintLabels value="true"/>
<paintTicks value="false"/>
<paintTrack value="true"/>
<snapToTicks value="true"/>
<value value="0"/>
</properties>
</component>
<component id="28e81" class="javax.swing.JSlider" binding="rpmSlider">
<constraints>
<grid row="0" column="0" row-span="3" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<inverted value="false"/>
<maximum value="8000"/>
<minorTickSpacing value="100"/>
<orientation value="1"/>
<paintLabels value="true"/>
<paintTicks value="false"/>
<paintTrack value="true"/>
<snapToTicks value="true"/>
<value value="0"/>
<valueIsAdjusting value="false"/>
</properties>
</component>
<component id="95218" class="javax.swing.JLabel" binding="rpmCounter">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="14" style="1"/>
<text value="RPM: 0"/>
</properties>
</component>
<component id="fb0c5" class="javax.swing.JLabel" binding="kmhCounter">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="14" style="1"/>
<text value="0 km/h"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</children>
</grid>
<component id="cb8a0" class="javax.swing.JButton" binding="connectButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Connect"/>
</properties>
</component>
<component id="c6619" class="javax.swing.JList" binding="comPorts">
<constraints>
<grid row="0" column="0" row-span="1" col-span="4" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="b0c47" class="javax.swing.JButton" binding="startServerButton" default-binding="true">
<constraints>
<grid row="1" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="false"/>
<text value="Start Server"/>
</properties>
</component>
<component id="7b84a" class="javax.swing.JButton" binding="refreshButton" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font/>
<text value="Refresh"/>
</properties>
</component>
</children>
</grid>
<grid id="df335" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Debug"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="80b21" class="javax.swing.JButton" binding="sendButton" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Send"/>
</properties>
</component>
<component id="2aae1" class="javax.swing.JTextField" binding="txText">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<scrollpane id="995bd">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="3d0a5" class="javax.swing.JTextArea" binding="debugArea">
<constraints/>
<properties>
<wrapStyleWord value="true"/>
</properties>
</component>
</children>
</scrollpane>
</children>
</grid>
</children>
</tabbedpane>
</children>
</grid>
</form>

+ 290
- 0
src/main/java/de/gurkengewuerz/serialinterface/SerialGUI.java View File

@@ -0,0 +1,290 @@
package de.gurkengewuerz.serialinterface;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import org.pmw.tinylog.Logger;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* Created by gurkengewuerz.de on 08.01.2018.
*/
public class SerialGUI {
private JTabbedPane tabbedPane1;
private JButton refreshButton;
private JTextField a0000TextField;
private JTextArea debugArea;
private JButton sendButton;
private JTextField txText;
private JTextField a8652TextField;
private JButton connectButton;
private JButton startServerButton;
public JPanel mainPanel;
private JList comPorts;
private JSlider kmhSlider;
private JSpinner fuelSpinner;
private JSlider rpmSlider;
private JCheckBox rTurnSignalCheckBox;
private JCheckBox lTurnSignalCheckBox;
private JCheckBox handbrakeCheckBox;
private JCheckBox fogBeamCheckBox;
private JCheckBox highBeamCheckBox;
private JCheckBox seatBeltCheckBox;
private JCheckBox lowVoltageCheckBox;
private JCheckBox waterTempCheckBox;
private JCheckBox ABSCheckBox;
private JCheckBox tirePressureCheckBox;
private JCheckBox backlightCheckBox;
private JButton updateButton;
private JCheckBox offroadCheckBox;
private JCheckBox oilPressureCheckBox;
private JCheckBox openDoorsCheckBox;
private JCheckBox kupplungCheckBox;
private JCheckBox checkLampCheckBox;
private JCheckBox openTrunkCheckBox;
private JLabel rpmCounter;
private JLabel kmhCounter;
private JPanel simulatorPanel;

private SerialPort serialPort;
private BufferedReader input;
private OutputStream output;
private static final int TIME_OUT = 2000;
private static final int BAUDRATE = 115200; // ~ 14 kByte pro Sekunde
private DashboardStatus status;
private ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();

/*
TODO: Klasse für Ansteuerung
TODO: WebServer
TODO: Update Simulation Panel if Class for contol gets updated! (Live View)


*/

public SerialGUI() {
status = new DashboardStatus();

Logger.info(String.join(",", listPorts()));
refreshPortList();

SpinnerModel sm = new SpinnerNumberModel(100, 0, 100, 5);
fuelSpinner.setModel(sm);

Hashtable labelsKMH = new Hashtable();
labelsKMH.put(0, new JLabel("0 km/h"));
labelsKMH.put(40, new JLabel("40 km/h"));
labelsKMH.put(80, new JLabel("80 km/h"));
labelsKMH.put(120, new JLabel("120 km/h"));
labelsKMH.put(160, new JLabel("160 km/h"));
labelsKMH.put(200, new JLabel("200 km/h"));
labelsKMH.put(240, new JLabel("240 km/h"));
kmhSlider.setLabelTable(labelsKMH);

kmhSlider.addChangeListener(evt -> {
JSlider slider = (JSlider) evt.getSource();
if (!slider.getValueIsAdjusting()) {
int value = slider.getValue();
kmhCounter.setText(value + " km/h");
}
});

Hashtable labelsRPM = new Hashtable();
labelsRPM.put(0, new JLabel("0"));
labelsRPM.put(1000, new JLabel("1k"));
labelsRPM.put(2000, new JLabel("2k"));
labelsRPM.put(3000, new JLabel("3k"));
labelsRPM.put(4000, new JLabel("4k"));
labelsRPM.put(5000, new JLabel("5k"));
labelsRPM.put(6000, new JLabel("6k"));
labelsRPM.put(7000, new JLabel("7k"));
labelsRPM.put(8000, new JLabel("8k"));
rpmSlider.setLabelTable(labelsRPM);

rpmSlider.addChangeListener(evt -> {
JSlider slider = (JSlider) evt.getSource();
if (!slider.getValueIsAdjusting()) {
int value = slider.getValue();
rpmCounter.setText("RPM: " + value);
}
});

refreshButton.addActionListener(e -> {
refreshPortList();
comPorts.grabFocus();
});

connectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (serialPort == null) {
try {
if (comPorts.getSelectedIndex() < 0) {
errorBox("Please select a COM Port!", "No port selected");
return;
}

CommPortIdentifier portId = getPortIDbyString((String) comPorts.getSelectedValue());
if (portId == null) {
errorBox("An invalid port is selected!\nIs the port still connected?", "Invalid port selected");
refreshPortList();
return;
}

// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);

// set port parameters
serialPort.setSerialPortParams(BAUDRATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();

ses.scheduleAtFixedRate(() -> write(status.generateJSON()), 5 * 1000, 10, TimeUnit.MILLISECONDS);


// add event listeners
serialPort.addEventListener(oEvent -> {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine = input.readLine();
debugArea.append(inputLine + "\n");
debugArea.setCaretPosition(debugArea.getDocument().getLength());


} catch (Exception ex) {
//Logger.error(ex);
}
}
});
serialPort.notifyOnDataAvailable(true);

refreshButton.setEnabled(false);
comPorts.setEnabled(false);
startServerButton.setEnabled(true);
connectButton.setText("Disconnect");
} catch (Exception ex) {
Logger.error(ex);
}
} else {
close();
serialPort = null;

refreshButton.setEnabled(true);
comPorts.setEnabled(true);
startServerButton.setEnabled(false);
connectButton.setText("Connect");
ses.shutdown();

// TODO: Stop Server
}
}
});

sendButton.addActionListener(e -> {
write(txText.getText());
txText.setText("");
});

updateButton.addActionListener(e -> {
status.setRpm(rpmSlider.getValue());
status.setKmh(kmhSlider.getValue());
status.setFuel((Integer) fuelSpinner.getValue());
status.setFogbeam(fogBeamCheckBox.isSelected());
status.setHighbeam(highBeamCheckBox.isSelected());
status.setHandbrake(handbrakeCheckBox.isSelected());
status.setAbs(ABSCheckBox.isSelected());
status.setOil_pressure(oilPressureCheckBox.isSelected());
status.setTire_pressure(tirePressureCheckBox.isSelected());
status.setKupplung(kupplungCheckBox.isSelected());
status.setTrunk(openTrunkCheckBox.isSelected());
status.setBacklight(backlightCheckBox.isSelected());
status.setL_turn_signal(lTurnSignalCheckBox.isSelected());
status.setR_turn_signal(rTurnSignalCheckBox.isSelected());
status.setOffroad(offroadCheckBox.isSelected());
status.setWater_temp(waterTempCheckBox.isSelected());
status.setSeat_belt(seatBeltCheckBox.isSelected());
status.setBattery(lowVoltageCheckBox.isSelected());
status.setCheck_lamp(checkLampCheckBox.isSelected());
status.setDoors(openDoorsCheckBox.isSelected());
Logger.info(status.generateJSON());
Logger.info(status.generateJSON().getBytes().length);
});
}

public void write(String payload) {
if (serialPort != null) {
try {
output.write(payload.getBytes());
output.flush();
} catch (IOException e) {
Logger.error(e);
}
}
}

public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}

private void refreshPortList() {
DefaultListModel<String> model = new DefaultListModel<>();
comPorts.setModel(model);

listPorts().forEach(model::addElement);
}

private CommPortIdentifier getPortIDbyString(String port) {
CommPortIdentifier portID = null;

java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier portIdentifier = portEnum.nextElement();
if (portIdentifier.getName().equals(port)) {
portID = portIdentifier;
break;
}
}

return portID;
}

private ArrayList<String> listPorts() {
ArrayList<String> ports = new ArrayList<>();
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier portIdentifier = portEnum.nextElement();
if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) ports.add(portIdentifier.getName());
}

return ports;
}


public static void infoBox(String infoMessage, String titleBar) {
JOptionPane.showMessageDialog(null, infoMessage, "INFO: " + titleBar, JOptionPane.INFORMATION_MESSAGE);
}

public static void errorBox(String errorMessage, String titleBar) {
JOptionPane.showMessageDialog(null, errorMessage, "ERROR: " + titleBar, JOptionPane.ERROR_MESSAGE);
}
}

+ 37
- 0
src/main/java/de/gurkengewuerz/serialinterface/SerialInterface.java View File

@@ -0,0 +1,37 @@
package de.gurkengewuerz.serialinterface;

import com.bulenkov.darcula.DarculaLaf;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* Created by gurkengewuerz.de on 08.01.2018.
*/
public class SerialInterface {

public static void main(String... args) {
try {
UIManager.setLookAndFeel(new DarculaLaf());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}


JFrame frame = new JFrame("Serial Interface Manager");
SerialGUI gui = new SerialGUI();
frame.setContentPane(gui.mainPanel);

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
gui.close();
frame.dispose();
}
});

frame.pack();
frame.setVisible(true);
}
}

Loading…
Cancel
Save