Ich versuche gerade den Seriellen Port anzustueren, habe dazu die CommAPI von Sun runtergeladen
und anhand eines beiliegenden Beispiels versucht ein eigenes Programm zu schreiben.
Doch leider bekomme ich als verfuegbare Serielle Ports nur null als rueckgabewert...
Hier mal mein bisheriges Programm:
und anhand eines beiliegenden Beispiels versucht ein eigenes Programm zu schreiben.
Doch leider bekomme ich als verfuegbare Serielle Ports nur null als rueckgabewert...
Hier mal mein bisheriges Programm:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.comm.*;
public class SerialConfig extends JFrame implements ActionListener{
TextArea taIn;
TextArea taOut;
JButton b,b2;
Choice portChoice;
SerialConnection connect;
SerialParameters parameters;
public SerialConfig(){
taIn=new TextArea();
taOut=new TextArea();
b=new JButton("abc");
b2=new JButton("open Port");
portChoice=new Choice();
parameters=new SerialParameters();
parameters.setBaudRate(4800);
connect=new SerialConnection(this,parameters,taOut,taIn);
JMenuBar mb=new JMenuBar();
JMenu m=new JMenu("Datei");
JMenu m2=new JMenu("Bearbeiten");
JMenuItem mo=new JMenuItem("Öffnen");
JMenuItem ms=new JMenuItem("Schließen");
JMenuItem me=new JMenuItem("Beenden");
mo.addActionListener(this);
ms.addActionListener(this);
me.addActionListener(this);
b2.addActionListener(this);
m.add(mo);
m.add(ms);
m.add(me);
mb.add(m);
mb.add(m2);
mo.setBackground(Color.WHITE);
ms.setBackground(Color.WHITE);
me.setBackground(Color.WHITE);
this.setJMenuBar(mb);
this.getContentPane().setLayout(new GridLayout(4,1));
this.getContentPane().add(b);
this.getContentPane().add(taOut);
this.getContentPane().add(portChoice);
this.getContentPane().add(b2);
this.setSize(300,200);
this.setVisible(true);
listPortChoices();
portChoice.select(parameters.getPortName());
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(b)){
taIn.append(b.getText());
}else if(ae.getSource().equals(b2)){
Cursor previousCursor = getCursor();
setNewCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try{
connect.openConnection();
}catch(Exception e){
System.out.print("Connection fails");
setNewCursor(previousCursor);
}
}
}
public void portClosed(){
}
private void setNewCursor(Cursor c) {
setCursor(c);
taIn.setCursor(c);
taOut.setCursor(c);
}
public void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
while (en.hasMoreElements()) {
portId = (CommPortIdentifier)en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
portChoice.addItem(portId.getName());
}
}
portChoice.select(parameters.getPortName());
}
public static void main(String []args){
SerialConfig sc=new SerialConfig();
}
}