Commands to Utec supported.

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@518 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
Tgui 2007-02-16 04:34:49 +00:00
parent 59360b5512
commit 9fbedabaab
3 changed files with 280 additions and 193 deletions

View File

@ -24,7 +24,7 @@ import enginuity.logger.utec.mapData.UtecMapData;
import gnu.io.*;
public class UtecControl implements SerialPortEventListener{
public class UtecSerialConnection implements SerialPortEventListener{
// Parent object organizing connections to and from UTEC
//private JPanel parent;
@ -76,7 +76,7 @@ public class UtecControl implements SerialPortEventListener{
*/
//public SerialConnection(JPanel parent, SerialParameters parameters,
// TextArea messageAreaOut, TextArea messageAreaIn) {
public UtecControl(SerialParameters parameters) {
public UtecSerialConnection(SerialParameters parameters) {
//this.parent = parent;
//this.messageAreaOut = messageAreaOut;
//this.messageAreaIn = messageAreaIn;
@ -101,8 +101,8 @@ public class UtecControl implements SerialPortEventListener{
*/
public void resetUtec(){
//OutPut 2 ctrl-x to UTEC
this.sendDataToUtec('\u0018');
this.sendDataToUtec('\u0018');
//this.sendDataToUtec('\u0018');
//this.sendDataToUtec('\u0018');
this.sendDataToUtec(33);
this.sendDataToUtec(33);
}
@ -187,7 +187,12 @@ public class UtecControl implements SerialPortEventListener{
*
* @param charValue
*/
private void sendDataToUtec(int charValue){
public void sendDataToUtec(int charValue){
if(this.sPort == null){
System.err.println("No Port Selected.");
return;
}
try{
outputToUtecStream.write(charValue);
}
@ -197,6 +202,8 @@ public class UtecControl implements SerialPortEventListener{
}
}
/**
* Opens a connection to the defined serial port If attempt fails,
* SerialConnectionException is thrown
@ -205,6 +212,11 @@ public class UtecControl implements SerialPortEventListener{
*/
public void openConnection() throws SerialConnectionException {
if(sPort == null){
System.err.println("No port selected.");
return;
}
// Obtain a CommPortIdentifier object for the port you want to open.
try {
//System.out.println("PORT: "+parameters.getPortName());
@ -223,7 +235,7 @@ public class UtecControl implements SerialPortEventListener{
sPort = (SerialPort) portId.open("SerialDemo", 30000);
} catch (PortInUseException e) {
System.err.println("Can't open serial port");
throw new SerialConnectionException(e.getMessage());
//throw new SerialConnectionException(e.getMessage());
}
// Set the parameters of the connection. If they won't set, close the
@ -289,6 +301,10 @@ public class UtecControl implements SerialPortEventListener{
*/
public void setConnectionParameters() throws SerialConnectionException {
if(sPort == null){
System.err.println("No port selected.");
return;
}
// Save state of parameters before trying a set.
int oldBaudRate = sPort.getBaudRate();
int oldDatabits = sPort.getDataBits();

View File

@ -34,7 +34,7 @@ public class UtecInterface{
private static SerialParameters parameters = new SerialParameters();
//Actual connection entity
private static UtecControl utecControl = new UtecControl(parameters);
private static UtecSerialConnection utecControl = new UtecSerialConnection(parameters);
public static boolean ISOPEN = utecControl.isOpen();
@ -51,6 +51,14 @@ public class UtecInterface{
utecControl.pullMapData(mapNumber, listener);
}
/**
* Pass a command to the UTEC
* @param charValue
*/
public static void sendDataToUtec(int charValue){
utecControl.sendDataToUtec(charValue);
}
/**
* Open a port as per names defined in get port names method
*

View File

@ -24,42 +24,56 @@ import enginuity.logger.utec.commInterface.UtecInterface;
/**
* @author botman
*/
public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecListener{
//Top level desktop pane
public class JutecGUI extends JFrame implements ActionListener,
GetMapFromUtecListener, KeyListener {
// Top level desktop pane
public JLayeredPane desktop = null;
//Top menu bar
// Top menu bar
private JMenuBar menuBar;
//A known existing port
// A known existing port
private String defaultPort = null;
//Currently selected port
// Currently selected port
private String currentPort = null;
private BottomUtecControl bottomPanel = null;
private JFileChooser fileChooser = null;
private int fileChosen;
private File selectedFile = null;
//FileMenu Items
// FileMenu Items
public JMenuItem saveItem = new JMenuItem("Save Log");
public JMenuItem saveMapItem = new JMenuItem("Save Map To File");
public JMenuItem exitItem = new JMenuItem("Exit");
public JMenuItem loadMapOne = new JMenuItem("Load Map #1");
public JMenuItem loadMapTwo = new JMenuItem("Load Map #2");
public JMenuItem loadMapThree = new JMenuItem("Load Map #3");
public JMenuItem loadMapFour = new JMenuItem("Load Map #4");
public JMenuItem loadMapFive = new JMenuItem("Load Map #5");
private static JutecGUI instance = null;
private UtecMapData currentMap = null;
public static JutecGUI getInstance(){
// Text input field for sending commands directly to the UTEC
//private JTextArea textInput = new JTextArea();
private JTextField textInput = new JTextField();
private JLabel utecInputLabel = new JLabel(" Send Commands to UTEC: ");
public static JutecGUI getInstance() {
return instance;
}
@ -72,34 +86,33 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
this.setResizable(false);
this.setDefaultCloseOperation(setDefaultCloseOperation);
//*************************
//Voice the welcome message
//*************************
// *************************
// Voice the welcome message
// *************************
VoiceThread vc = new VoiceThread("Welcome to you teck logger! Use at your own risk.");
VoiceThread vc = new VoiceThread(
"Welcome to you teck logger! Use at your own risk.");
vc.start();
//Actions to take when window is closing
// Actions to take when window is closing
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("JUTEC Exiting");
//Use interface to close the connecetion to the Utec
// Use interface to close the connecetion to the Utec
UtecInterface.closeConnection();
}
});
//-----------------------
//Start Menu bar addtions
//-----------------------
// -----------------------
// Start Menu bar addtions
// -----------------------
menuBar = new JMenuBar();
//*********************************************
//Add a menu item for basic application actions
//*********************************************
// *********************************************
// Add a menu item for basic application actions
// *********************************************
// Define the menu system
JMenu fileMenu = new JMenu("File");
saveItem.addActionListener(this);
@ -110,7 +123,6 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
fileMenu.add(exitItem);
menuBar.add(fileMenu);
JMenu getMapsMenu = new JMenu("Load Map");
loadMapOne.addActionListener(this);
loadMapTwo.addActionListener(this);
@ -124,25 +136,25 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
getMapsMenu.add(loadMapFive);
menuBar.add(getMapsMenu);
//----------------------------------
//Add a menu item for comm port selection
//----------------------------------
JMenu portsMenu =new JMenu("Ports");
// ----------------------------------
// Add a menu item for comm port selection
// ----------------------------------
JMenu portsMenu = new JMenu("Ports");
//Gather list of ports from interface
Vector portsVector =UtecInterface.getPortsVector();
// Gather list of ports from interface
Vector portsVector = UtecInterface.getPortsVector();
Iterator portsIterator = portsVector.iterator();
int counter = 0;
while(portsIterator.hasNext()){
while (portsIterator.hasNext()) {
counter++;
Object o = portsIterator.next();
String theName = (String)o;
String theName = (String) o;
JMenuItem item = new JMenuItem(theName);
item.setName(theName);
item.addActionListener(this);
portsMenu.add(item);
if(counter == 1){
if (counter == 1) {
defaultPort = theName;
UtecInterface.setPortChoice(defaultPort);
}
@ -152,33 +164,39 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
// Add menu item to the JFrame
this.setJMenuBar(menuBar);
// *********************************************
// Start Adding GUI Elements to the Window
// *********************************************
JPanel totalPanel = new JPanel(new BorderLayout());
JPanel commandPanel = new JPanel(new BorderLayout());
this.textInput.addKeyListener(this);
//*********************************************
//Start Adding GUI Elements to the Window
//*********************************************
commandPanel.add(this.utecInputLabel, BorderLayout.WEST);
commandPanel.add(this.textInput, BorderLayout.CENTER);
bottomPanel = new BottomUtecControl();
JTabbedPane topTabbedPane = new JTabbedPane();
topTabbedPane.add("Graph Data",new RealTimeData());
topTabbedPane.add("Graph Data", new RealTimeData());
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setDividerLocation(435);
splitPane.setTopComponent(topTabbedPane);
splitPane.setBottomComponent(bottomPanel);
splitPane.setPreferredSize(new Dimension(800,600));
splitPane.setPreferredSize(new Dimension(800, 600));
this.getContentPane().add(splitPane);
totalPanel.add(commandPanel, BorderLayout.NORTH);
totalPanel.add(splitPane, BorderLayout.CENTER);
this.getContentPane().add(totalPanel);
//***********************
//Define the file chooser
//***********************
// ***********************
// Define the file chooser
// ***********************
fileChooser = new JFileChooser();
//Save singleton
// Save singleton
instance = this;
}
@ -189,36 +207,49 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
*
*/
public void actionPerformed(ActionEvent e) {
// Detect whether or not this is a command to be sent to the UTEC
if (e.getSource() == this.textInput) {
System.out.println("Text Input received:"+this.textInput.getText());
String tempString = this.textInput.getText();
tempString = tempString.substring(0, 1);
System.out.println("Reduce Input:"+tempString+":");
this.textInput.setText("");
}
// Else this is either a port event or other
else {
String cmd = e.getActionCommand();
//Open Port
// Open Port
if (cmd.equals("New")) {
System.out.println("New action occuring");
}
//Close Port
// Close Port
else if (cmd.equals("Open")) {
System.out.println("Open action occuring");
}
//Start Capture
// Start Capture
else if (cmd.equals("Save Log")) {
String saveFileName = null;
System.out.println("Save action occuring");
fileChosen = fileChooser.showSaveDialog(this);
if(fileChosen == JFileChooser.APPROVE_OPTION)
{
if (fileChosen == JFileChooser.APPROVE_OPTION) {
saveFileName = fileChooser.getSelectedFile().getPath();
//selectedFile = fileChooser.getSelectedFile();
// selectedFile = fileChooser.getSelectedFile();
try {
File file = new File(saveFileName);
FileWriter out = new FileWriter(file);
out.write(bottomPanel.totalLog);
out.close();
bottomPanel.totalLog = "";
}
catch (IOException e2) {
System.out.println("Couldn't save file " + saveFileName);
} catch (IOException e2) {
System.out
.println("Couldn't save file " + saveFileName);
e2.printStackTrace();
}
}
@ -227,18 +258,17 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
else if (cmd.equals("Save Map To File")) {
System.out.println("Saving map to file.");
if(this.currentMap != null){
if (this.currentMap != null) {
String saveFileName = null;
System.out.println("Save map now.");
fileChosen = fileChooser.showSaveDialog(this);
if(fileChosen == JFileChooser.APPROVE_OPTION)
{
if (fileChosen == JFileChooser.APPROVE_OPTION) {
saveFileName = fileChooser.getSelectedFile().getPath();
this.currentMap.writeMapToFile(saveFileName);
}
}else{
} else {
System.out.println("Map is null.");
}
}
@ -273,34 +303,36 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
UtecInterface.getMap(5, this);
}
//Stop Capture
// Stop Capture
else if (cmd.equals("Exit")) {
//Use interface to finally close the connection to the Utec
// Use interface to finally close the connection to the Utec
UtecInterface.closeConnection();
System.out.println("Exit action occuring");
//Close out the application
// Close out the application
System.exit(0);
}
//Only non explicitly defined actions are those generated by ports.
//Since an arbitrary machine could have any number of serial ports
//its impossible to hard code choices based on menu items generated on the fly.
//Must pull the calling object and interrogate
else{
JMenuItem theItem = (JMenuItem)e.getSource();
// Only non explicitly defined actions are those generated by ports.
// Since an arbitrary machine could have any number of serial ports
// its impossible to hard code choices based on menu items generated
// on the fly.
// Must pull the calling object and interrogate
else {
JMenuItem theItem = (JMenuItem) e.getSource();
String portChoice = theItem.getName();
System.out.println("Port chosen: "+portChoice);
System.out.println("Port chosen: " + portChoice);
currentPort = portChoice;
UtecInterface.setPortChoice(currentPort);
bottomPanel.setEnabled(true);
//Notify the infoPane of the current port choice
//infoPane.setPort(currentPort);
// Notify the infoPane of the current port choice
// infoPane.setPort(currentPort);
}
}
}
public static void startLogger(final int defaultCloseOperation, final Settings settings) {
public static void startLogger(final int defaultCloseOperation,
final Settings settings) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JutecGUI application = new JutecGUI(defaultCloseOperation);
@ -311,6 +343,7 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
@ -320,7 +353,37 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
}
public void mapRetrieved(UtecMapData theMap) {
System.out.println("Got a map from the utec:"+theMap.getMapName());
System.out.println("Got a map from the utec:" + theMap.getMapName());
this.currentMap = theMap;
}
public void keyPressed(KeyEvent arg0) {
//System.out.println("Key Pressed");
// System.out.println("Key Pressed:"+arg0.getKeyCode()+" :"+arg0.getKeyChar()+" :"+arg0.getModifiers()+" :"+(int)arg0.getKeyChar());
if((int)arg0.getKeyChar() != 65535){
int charCodeInt = (int)arg0.getKeyChar();
System.out.println("Key Code entered:"+charCodeInt);
//Pass along command to the UTEC
UtecInterface.openConnection();
UtecInterface.sendDataToUtec(charCodeInt);
}
// Empty out the text field
this.textInput.setText("");
}
public void keyReleased(KeyEvent arg0) {
//System.out.println("Key Released");
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
//System.out.println("Key Typed:"+arg0.getKeyCode()+" :"+arg0.getKeyChar()+" :"+arg0.getModifiers());
// TODO Auto-generated method stub
}
}