6,6 → 6,7 |
import java.awt.Font; |
import java.awt.FontFormatException; |
import java.awt.FontMetrics; |
import java.awt.GridLayout; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import java.awt.event.KeyEvent; |
14,8 → 15,10 |
import java.io.InputStream; |
import java.io.OutputStream; |
|
import javax.swing.BorderFactory; |
import javax.swing.ButtonGroup; |
import javax.swing.JCheckBoxMenuItem; |
import javax.swing.JComboBox; |
import javax.swing.JFrame; |
import javax.swing.JLabel; |
import javax.swing.JMenu; |
26,6 → 29,7 |
import javax.swing.JScrollPane; |
import javax.swing.JSeparator; |
import javax.swing.JTextArea; |
import javax.swing.JTextField; |
import javax.swing.KeyStroke; |
import javax.swing.UIManager; |
import javax.swing.border.BevelBorder; |
67,11 → 71,13 |
private JLabel portInfoLeft; |
private JLabel portStatus; |
private JTextArea text; |
private Font monoFont; |
private Font sansFont; |
|
//The newline separator, so that we put the correct one in the GUI so copy/paste will work nicely |
private final static String NEWLINE = System.getProperty("line.separator"); |
|
public JavaTerminal( String portToOpen, BaudRate baud, DataBits data, StopBits stop, Parity parity, FlowControl flow, boolean localEcho ){ |
public JavaTerminal( String portToOpen, BaudRate baud, DataBits data, StopBits stop, Parity parity, FlowControl flow, boolean localEcho, boolean justOpen ){ |
this.localEcho = localEcho; |
newlines = NewlineType.NL; |
|
80,15 → 86,19 |
}catch( Exception e ){} |
|
try { |
thePort = new SerialPort( portToOpen, baud, data, stop, parity, flow ); |
if( !justOpen ){ |
thePort = new SerialPort( portToOpen, baud, data, stop, parity, flow ); |
}else{ |
thePort = new SerialPort( portToOpen, justOpen ); |
} |
} catch (NoSuchPortException e) { |
System.err.println( e.getMessage() ); |
System.err.println( "ERROR: No Such Port( " + portToOpen + " )" ); |
System.exit( 0 ); |
System.exit( 1 ); |
} catch (NotASerialPortException e) { |
System.err.println( e.getMessage() ); |
System.err.println( "ERROR: Not a serial port( " + portToOpen + " )" ); |
System.exit( 0 ); |
System.exit( 2 ); |
} |
|
portInput = thePort.getInputStream(); |
95,11 → 105,10 |
portOutput = thePort.getOutputStream(); |
|
makeGUI(); |
new Thread( this, "Reading Thread" ).start(); |
} |
|
private void makeGUI(){ |
Font monoFont; |
Font sansFont; |
|
|
//First let's get the fonts that we want to use |
169,6 → 178,78 |
configMenu.setMnemonic( KeyEvent.VK_C ); |
JMenuItem port = new JMenuItem( "Port" ); |
port.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_S, KeyEvent.VK_ALT ) ); |
port.addActionListener( new ActionListener() { |
|
@Override |
public void actionPerformed(ActionEvent arg0) { |
JFrame settingFrame = new JFrame( "Configuration" ); |
|
JPanel configPanel = new JPanel( new GridLayout( 4, 3 ) ); |
configPanel.setBorder( BorderFactory.createTitledBorder( "Serial port") ); |
configPanel.add( createDialogLabel( "Port:" ) ); |
configPanel.add( createDialogLabel( "Speed:" ) ); |
configPanel.add( createDialogLabel( "Parity:" ) ); |
JComboBox port = new JComboBox(); |
port.setEditable( true ); |
port.addItem( thePort.getPortName() ); |
configPanel.add( port ); |
JComboBox speed = new JComboBox(); |
BaudRate[] values = BaudRate.values(); |
for( int x = 0; x < values.length; x++ ){ |
speed.addItem( values[ x ] ); |
if( thePort.getBaudRate() == values[ x ] ){ |
speed.setSelectedIndex( x ); |
} |
} |
speed.setEditable( false ); |
configPanel.add( speed ); |
JComboBox parity = new JComboBox(); |
Parity[] parityValues = Parity.values(); |
for( int x = 0; x < parityValues.length; x++ ){ |
parity.addItem( parityValues[ x ] ); |
if( thePort.getParity() == parityValues[ x ] ){ |
parity.setSelectedIndex( x ); |
} |
} |
parity.setEditable( false ); |
configPanel.add( parity ); |
configPanel.add( createDialogLabel( "Bits:" ) ); |
configPanel.add( createDialogLabel( "Stopbits:" ) ); |
configPanel.add( createDialogLabel( "Flow control:" ) ); |
JComboBox bits = new JComboBox(); |
DataBits[] dataValues = DataBits.values(); |
for( int x = 0; x < dataValues.length; x++ ){ |
bits.addItem( dataValues[ x ] ); |
if( thePort.getDataBits() == dataValues[ x ] ){ |
bits.setSelectedIndex( x ); |
} |
} |
configPanel.add( bits ); |
JComboBox stopbits = new JComboBox(); |
StopBits[] stopValues = StopBits.values(); |
for( int x = 0; x < stopValues.length; x++ ){ |
stopbits.addItem( stopValues[ x ] ); |
if( thePort.getStopBits() == stopValues[ x ] ){ |
stopbits.setSelectedIndex( x ); |
} |
} |
configPanel.add( stopbits ); |
JComboBox flow = new JComboBox(); |
FlowControl[] flowValues = FlowControl.values(); |
for( int x = 0; x < flowValues.length; x++ ){ |
flow.addItem( flowValues[ x ] ); |
if( thePort.getFlowControl() == flowValues[ x ] ){ |
flow.setSelectedIndex( x ); |
} |
} |
configPanel.add( flow ); |
|
settingFrame.add( configPanel ); |
|
settingFrame.setSize( 500, 300 ); |
settingFrame.setVisible( true ); |
} |
}); |
configMenu.add( port ); |
JMenuItem window = new JMenuItem( "Main Window" ); |
configMenu.add( window ); |
215,6 → 296,55 |
configMenu.add( new JSeparator() ); |
menuBar.add( configMenu ); |
|
JMenu controlSigs = new JMenu( "Control Signals" ); |
controlSigs.setMnemonic( KeyEvent.VK_S ); |
JMenuItem sendBreak = new JMenuItem( "Send Break" ); |
sendBreak.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_B, KeyEvent.VK_ALT ) ); |
controlSigs.add( sendBreak ); |
JMenuItem toggleDTR = new JMenuItem( "Toggle DTR" ); |
toggleDTR.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_F7, KeyEvent.ALT_DOWN_MASK & KeyEvent.CTRL_DOWN_MASK ) ); |
toggleDTR.addActionListener( new ActionListener() { |
|
@Override |
public void actionPerformed(ActionEvent arg0) { |
} |
}); |
controlSigs.add( toggleDTR ); |
JMenuItem toggleRTS = new JMenuItem( "Toggle RTS" ); |
toggleRTS.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_F8, KeyEvent.ALT_DOWN_MASK & KeyEvent.CTRL_DOWN_MASK ) ); |
controlSigs.add( toggleRTS ); |
menuBar.add( controlSigs ); |
|
JMenu view = new JMenu( "View" ); |
view.setMnemonic( KeyEvent.VK_V ); |
ButtonGroup asciiHexGroup = new ButtonGroup(); |
JRadioButtonMenuItem ascii = new JRadioButtonMenuItem( "ASCII" ); |
ascii.setSelected( true ); |
asciiHexGroup.add( ascii ); |
view.add( ascii ); |
JRadioButtonMenuItem hex = new JRadioButtonMenuItem( "Hex" ); |
asciiHexGroup.add( hex ); |
view.add( hex ); |
JMenu hexCharsSubMenu = new JMenu( "Hexadecimal chars" ); |
hexCharsSubMenu.setEnabled( false ); |
JMenuItem eightChars = new JMenuItem( "8" ); |
hexCharsSubMenu.add( eightChars ); |
JMenuItem tenChars = new JMenuItem( "10" ); |
hexCharsSubMenu.add( tenChars ); |
JMenuItem sixteenChars = new JMenuItem( "16" ); |
hexCharsSubMenu.add( sixteenChars ); |
JMenuItem twentyFourChars = new JMenuItem( "24" ); |
hexCharsSubMenu.add( twentyFourChars ); |
JMenuItem thirtyTwooChars = new JMenuItem( "32" ); |
hexCharsSubMenu.add( thirtyTwooChars ); |
view.add( hexCharsSubMenu ); |
JMenuItem showIndex = new JMenuItem( "Show Index" ); |
showIndex.setEnabled( false ); |
view.add( showIndex ); |
view.addSeparator(); |
JMenuItem sendHexData = new JMenuItem( "Send Hex Data" ); |
view.add( sendHexData ); |
menuBar.add( view ); |
|
//add everything to the frame |
theFrame.setJMenuBar( menuBar ); |
246,6 → 376,7 |
FlowControl flow = FlowControl.NONE; |
String portToOpen; |
boolean echo = false; |
boolean justOpen = false; |
|
if( System.getProperty( "os.name" ).toLowerCase().indexOf( "win" ) >= 0 ){ |
portToOpen = "COM1"; |
270,7 → 401,8 |
System.out.println( "--parity=<odd|even|none> or -a : Parity (Default: None)" ); |
System.out.println( "--flow=<xon|cts|none> or -w : Flow Control (Default: None)" ); |
System.out.println( "--echo or -e : Switch on local echo" ); |
System.exit( 1 ); |
System.out.println( "--open or -o : Open the serial port, do not change settings" ); |
System.exit( 0 ); |
}else if( args[ x ].startsWith( "--port=" ) ){ |
portToOpen = args[ x ].substring( args[ x ].indexOf( '=' ) + 1 ); |
}else if( args[ x ].equals( "-p" ) && x < args.length ){ |
470,10 → 602,12 |
} |
}else if( args[ x ].equals( "--echo" ) || args[ x ].equals( "-e" ) ){ |
echo = true; |
}else if( args[ x ].equals( "--open" ) || args[ x ].equals( "-o" ) ){ |
justOpen = true; |
} |
} // end for loop |
|
new JavaTerminal( portToOpen, baud, data, stop, parity, flow, echo ); |
new JavaTerminal( portToOpen, baud, data, stop, parity, flow, echo, justOpen ); |
} |
|
@Override |
518,7 → 652,12 |
} |
} catch (BadLocationException e) {} |
|
text.append( c + "" ); |
|
if( c == '\n' || c == '\r' ){ |
text.append( NEWLINE ); |
}else{ |
text.append( c + "" ); |
} |
} |
|
/** |
587,5 → 726,12 |
} |
|
} |
|
private JLabel createDialogLabel( String title ){ |
JLabel lbl = new JLabel( title ); |
lbl.setFont( this.sansFont ); |
lbl.setAlignmentY( JLabel.CENTER_ALIGNMENT ); |
return lbl; |
} |
|
} |