Skip to content

Commit

Permalink
small bug fixes
Browse files Browse the repository at this point in the history
Signed-off-by: vstarlinger <valentin@starlinger.net>
  • Loading branch information
vstarlinger committed Aug 29, 2017
1 parent 8b8e4b1 commit 560ab8f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 42 deletions.
Expand Up @@ -144,6 +144,7 @@ public void run() {
counter--;
}
if (counter < 0) {
error("Error: Couldn't connect to INBOX");
return;
}

Expand Down
Expand Up @@ -27,6 +27,8 @@
import com.ksatstuttgart.usoc.data.SerialEvent;
import java.awt.event.ActionEvent;
import static java.lang.Thread.sleep;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -82,51 +84,30 @@ public void start(String sp, int baudrate) {
}

public void close() {
try {
try {
serialPort.closePort();
} catch (SerialPortException ex) {
error("Error: Couldn't close Serialport.");
}
}

public void send(String msg) {
if(!isOpen()){
error("Error: Serial port is not connected!");
return;
}
try {
//add a $ to the beginning and a # at the end
serialPort.writeBytes(("$" + msg + "#").getBytes());
serialPort.writeBytes(msg.getBytes(StandardCharsets.US_ASCII));
} catch (SerialPortException ex) {
error("Error: Couldn't send message: " + msg + ".");
}
}

public static void sendAction(ActionEvent e) {
System.out.println(SerialCommand.valueOf(e.getActionCommand()).getCommand()+"0\n");
if (SerialComm.getInstance().isOpen()) {
switch (e.getActionCommand()) {
case "LaunchMacro":
new Thread() {
@Override
public void run() {
try {
SerialComm.getInstance().send("Cameras");
sleep(3 * 1000);
SerialComm.getInstance().send("Prelaunch");
sleep(20 * 1000);
SerialComm.getInstance().send("Active");
sleep(5 * 1000);
SerialComm.getInstance().send("Cameras");
sleep(2 * 1000);
SerialComm.getInstance().send("Cameras");
sleep(2 * 1000);
SerialComm.getInstance().send("Prelaunch");
} catch (InterruptedException ex) {
}

}
}.start();
break;
default:
SerialComm.getInstance().send(e.getActionCommand());
break;
}
String command = SerialCommand.valueOf(e.getActionCommand()).getCommand()+"0\n";
SerialComm.getInstance().send(command);
}
}

Expand All @@ -141,16 +122,20 @@ public boolean isOpen() {
}

public List<String> getAvailableCommands() {
return Arrays.asList(SerialComm.COMMANDS);
ArrayList<String> commands = new ArrayList<>();
for (SerialCommand command : SerialCommand.values()) {
commands.add(command.toString());
}
return commands;
}

public List<String> getAvailableBaudrates() {
return Arrays.asList(SerialComm.BAUDRATES);
}

/**
* This class reads chars from the serial port and reports new incoming data.
* Incoming data is detected by newLine characters.
/**
* This class reads chars from the serial port and reports new incoming
* data. Incoming data is detected by newLine characters.
*/
private class SerialPortReader implements SerialPortEventListener {

Expand All @@ -166,9 +151,10 @@ public void serialEvent(SerialPortEvent event) {
if (c == '\n') {
messageReceived(new SerialEvent(buffer, event.getPortName(),
System.currentTimeMillis(), DataSource.SERIAL));
System.out.println(buffer);
buffer = "";
} else {
buffer += c;
buffer += (char)c;
}
}
} catch (SerialPortException ex) {
Expand Down
@@ -0,0 +1,49 @@
/*
* The MIT License
*
* Copyright 2017 KSat Stuttgart e.V..
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.ksatstuttgart.usoc.controller.communication;

/**
*
* @author valentinstarlinger
*/
public enum SerialCommand {
SendImage ("0"),
ActivateSimulation ("1"),
DeactivateSimulation ("2"),
ActivateAscend ("3"),
DeactivateAscend ("4"),
PerformSelfTest ("5"),
Reboot ("6"),
Shutdown ("7");

private String command;

private SerialCommand (String command){
this.command = command;
}

public String getCommand(){
return command;
}
}
Expand Up @@ -26,7 +26,7 @@
import com.ksatstuttgart.usoc.data.SerialEvent;

/**
* <h1>MailUpdateListener</h1>
* <h1>SerialListener</h1>
* This interface enables classes to listen to events happening on an open serial
* port via the SerialComm class.
*
Expand Down
Expand Up @@ -79,7 +79,7 @@ private void initialize(){

JTabbedPane bottomPanel = new JTabbedPane();
bottomPanel.addTab("Iridium", ip);
//bottomPanel.addTab("Serial", sp);
bottomPanel.addTab("Serial", sp);

mainPanel.setLayout(new BorderLayout());
mainPanel.add(dataTabs, BorderLayout.WEST);
Expand Down
Expand Up @@ -57,17 +57,15 @@ public class SerialPanel extends DataPanel {
private final LogPanel lp;
private final JComboBox ports, baudrate;

private static final String[] baudrates = {"38400", "57600"};
private static final String[] commands = {"Ping", "Testing", "Camtest", "Cameras", "Abort", "Prelaunch", "Active", "Reset", "LaunchMacro"};


public SerialPanel() {
super();

commandBox = new JComboBox(commands);
commandBox = new JComboBox(SerialComm.getInstance().getAvailableCommands().toArray());
send = new JButton("Send Command");
send.addActionListener(new SendListener());
ports = new JComboBox();
baudrate = new JComboBox(baudrates);
baudrate = new JComboBox(SerialComm.getInstance().getAvailableBaudrates().toArray());
connect = new JButton("Connect");
connect.addActionListener(new ConnectListener());

Expand All @@ -90,6 +88,7 @@ public SerialPanel() {
this.add(lp, BorderLayout.CENTER);
this.setPreferredSize(new java.awt.Dimension(0, 250));

MainController.startPortThread(this);
}

@Override
Expand Down

0 comments on commit 560ab8f

Please sign in to comment.