1 | import java.awt.*;
|
2 | import java.awt.event.*;
|
3 | import javax.swing.*;
|
4 | import java.*;
|
5 | import java.io.*;
|
6 | import javax.comm.*;
|
7 |
|
8 |
|
9 | public class schieberegler extends Frame
|
10 | {
|
11 |
|
12 | static JTextField tf;
|
13 | static JScrollBar sb;
|
14 | static JButton aendern;
|
15 | static JTextField binär;
|
16 | static JButton senden;
|
17 | static JTextArea ausgabe;
|
18 | static JLabel label1;
|
19 | static JLabel label2;
|
20 | static JLabel label3;
|
21 | static JLabel label4;
|
22 | static JLabel label5;
|
23 | static JLabel label6;
|
24 | static CommPortIdentifier portID;
|
25 | InputStream ins;
|
26 | OutputStream out;
|
27 | static SerialPort serss;
|
28 |
|
29 |
|
30 |
|
31 | public static void main( String args[] )
|
32 | {
|
33 | try{portID = CommPortIdentifier.getPortIdentifier("CNCA0");
|
34 | serss = (SerialPort) portID.open("mein_programm",2000);
|
35 |
|
36 | }
|
37 | catch(Exception exc){System.out.println("Fehler :"+exc);}
|
38 | JFrame f = new JFrame();
|
39 | f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
|
40 | f.setTitle("RS232 Ansteuerung von ");
|
41 | f.pack();
|
42 | f.setSize(250,130);
|
43 | f.setVisible( true );
|
44 | Container c = f.getContentPane();
|
45 |
|
46 | c.setLayout( new GridLayout(6,2) );
|
47 |
|
48 | c.add (label1 = new JLabel("Aktueller SR Wert:"));
|
49 | c.add ( tf = new JTextField( 20 ) );
|
50 | c.add (label2 = new JLabel("Schieberegler:"));
|
51 | c.add ( sb = new JScrollBar( JScrollBar.HORIZONTAL, 0, 10, 0, 255+10 ) );
|
52 | c.add (label3 = new JLabel("Wert ändern:"));
|
53 | c.add(aendern = new JButton("ändern"));
|
54 | c.add (label4 = new JLabel("Binärer Wert:"));
|
55 | c.add(binär = new JTextField(20));
|
56 | c.add (label5 = new JLabel("Gäenderten Wert senden:"));
|
57 | c.add(senden = new JButton("Wert senden"));
|
58 | c.add(label6 = new JLabel("Satus:"));
|
59 | c.add(ausgabe = new JTextArea("Port COM geöffnet\n",7,40));
|
60 |
|
61 | aendern.addActionListener(new ActionListener() {
|
62 | public void actionPerformed(ActionEvent e) {
|
63 |
|
64 | String b = null;
|
65 | int Wert;
|
66 | Wert = sb.getValue();
|
67 |
|
68 | b = (new Integer(Wert)).toBinaryString(Wert);
|
69 | binär.setText(""+b);
|
70 | }
|
71 | } );
|
72 |
|
73 | tf.addActionListener( new ActionListener() {
|
74 | public void actionPerformed( ActionEvent e ) {
|
75 | sb.setValue( Integer.parseInt(tf.getText()) );
|
76 | }
|
77 | } );
|
78 |
|
79 | sb.addAdjustmentListener( new AdjustmentListener() {
|
80 | public void adjustmentValueChanged( AdjustmentEvent e ) {
|
81 | tf.setText( "" + sb.getValue() );
|
82 | }
|
83 | } );
|
84 | }
|
85 |
|
86 | public schieberegler() {
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | binär.addActionListener(new sendeListener());
|
99 | senden.addActionListener(new sendeListener());
|
100 |
|
101 | try {ins = serss.getInputStream();
|
102 | out = serss.getOutputStream();
|
103 | serss.addEventListener(new commListener());
|
104 | }
|
105 | catch (Exception e) { System.out.println("Fehler: "+e);}
|
106 | serss.notifyOnDataAvailable(true);
|
107 | try {serss.setSerialPortParams(2400,
|
108 | SerialPort.DATABITS_8,
|
109 | SerialPort.STOPBITS_1 ,
|
110 | SerialPort.PARITY_NONE);}
|
111 | catch (UnsupportedCommOperationException e) {}
|
112 | }
|
113 |
|
114 | public class sendeListener implements ActionListener{
|
115 | public void actionPerformed(ActionEvent e){
|
116 | PrintWriter aus = new PrintWriter(out);
|
117 | aus.println(binär.getText());
|
118 | aus.flush();
|
119 | aus.close();
|
120 | }
|
121 | }
|
122 |
|
123 | public class commListener implements SerialPortEventListener{
|
124 | public void serialEvent(SerialPortEvent event) {
|
125 | if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE){
|
126 | byte[] readBuffer = new byte[20];
|
127 | try {
|
128 | while (ins.available() > 0) {int numBytes = ins.read(readBuffer);}
|
129 | String nachricht = new String(readBuffer);
|
130 | ausgabe.append(nachricht);
|
131 |
|
132 | }
|
133 | catch (IOException e) {System.out.println("Fehler: "+e);}
|
134 |
|
135 | }
|
136 | }
|
137 | }
|
138 | }
|