トップページ > dsPIC入門 > Javaでシリアル通信(7)
いままで作ってきたものをコピペしつつ、拡張しただけです。 ソースコードが冗長ですが、見れば何をやっているか分かるはずです。 dsPIC側のプログラムと 組み合わせて使用します。
import javax.swing.*; import java.awt.*; import java.awt.event.*; import gnu.io.*; import java.io.*; public class gui_serial2 { public static void main(String[] args) { //Frameを生成 JFrame frame = new JFrame("gui_serial2"); //閉じる処理を指定 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //サイズを設定 frame.setSize(300,300); //見えるように… frame.setVisible(true); //コンテントペインを生成 JPanel contentPane = new JPanel(); //============================================================================ //ボタン生成の際に、アクションをセットします(今回はボタンが5個) JButton button_left = new JButton(new left_action()); JButton button_stop = new JButton(new stop_action()); JButton button_right = new JButton(new right_action()); //ボタンを、コンテントペインへ登録 contentPane.add(button_right); contentPane.add(button_stop); contentPane.add(button_left); //コンテントペインを、フレームへ登録 frame.setContentPane(contentPane); //レイアウトは、今回はグリットレイアウトを使用。 //自動的に、登録されたオブジェクトが縦に並びます。 contentPane.setLayout(new GridLayout(3,1)); } } //****************************************************************************** //****************************************************************************** //ボタンが押された際のイベント処理は、アクションクラスとして完全にクラス化! class right_action extends AbstractAction { right_action() { putValue(Action.NAME,"RIGHT"); } public void actionPerformed(ActionEvent e) { try { CommPortIdentifier ports = CommPortIdentifier.getPortIdentifier( "COM5" ); CommPort commPort = ports.open("hoge",2000); SerialPort port = (SerialPort)commPort; port.setSerialPortParams( 115200,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE ); port.setFlowControlMode( SerialPort.FLOWCONTROL_NONE ); OutputStream out = port.getOutputStream(); out.write( 'r' ); out.write( 'i' ); out.write( 'g' ); out.write( 'h' ); out.write( 't' ); out.write( '\r' ); out.close(); port.close(); } catch( Exception ex ) { System.out.println( "Error発生:" + ex ); } } } //****************************************************************************** class stop_action extends AbstractAction { stop_action() { putValue(Action.NAME,"STOP"); } public void actionPerformed(ActionEvent e) { try { CommPortIdentifier ports = CommPortIdentifier.getPortIdentifier( "COM5" ); CommPort commPort = ports.open("hoge",2000); SerialPort port = (SerialPort)commPort; port.setSerialPortParams( 115200,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE ); port.setFlowControlMode( SerialPort.FLOWCONTROL_NONE ); OutputStream out = port.getOutputStream(); out.write( 's' ); out.write( 't' ); out.write( 'o' ); out.write( 'p' ); out.write( '\r' ); out.close(); port.close(); } catch( Exception ex ) { System.out.println( "Error発生:" + ex ); } } } //****************************************************************************** class left_action extends AbstractAction { left_action() { putValue(Action.NAME,"LEFT"); } public void actionPerformed(ActionEvent e) { try { CommPortIdentifier ports = CommPortIdentifier.getPortIdentifier( "COM5" ); CommPort commPort = ports.open("hoge",2000); SerialPort port = (SerialPort)commPort; port.setSerialPortParams( 115200,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE ); port.setFlowControlMode( SerialPort.FLOWCONTROL_NONE ); OutputStream out = port.getOutputStream(); out.write( 'l' ); out.write( 'e' ); out.write( 'f' ); out.write( 't' ); out.write( '\r' ); out.close(); port.close(); } catch( Exception ex ) { System.out.println( "Error発生:" + ex ); } } }