import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.swing.*;
public class JGraph extends JFrame implements Serializable, Runnable{
/**
*
*/
private static final long serialVersionUID = 1919259915485678785L;
private JMenu datei = new JMenu("Datei");
private JMenuItem laden = new JMenuItem("Laden");
private JMenuItem speichern = new JMenuItem("Save");
private JMenuBar menuBar = new JMenuBar();
private JPanel zeichnen = new JPanel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JButton senden = new JButton();
private JTextArea txtsenden = new JTextArea(10, 50);
private JTextArea txtempfangen = new JTextArea(10, 50);
private JScrollPane scrollPane = new JScrollPane(txtsenden);
private JScrollPane scrollPane2 = new JScrollPane(txtempfangen);
private JLabel lblsenden = new JLabel();
private JLabel messlab1 = new JLabel();
CommPortIdentifier serialPortID;
Enumeration enumComm;
SerialPort serialPort;
OutputStream os;
InputStream is;
boolean serialPortoffen = false;
int baudrate = 9600;
int dataBits = SerialPort.DATABITS_8;
int stopBits = SerialPort.STOPBITS_1;
int parity = SerialPort.PARITY_NONE;
String portName = "COM1";
int secondsRuntime = 60;
public JGraph(int i){
}
@SuppressWarnings("serial")
public JGraph() {
menuLaden();
setTitle("Radio mail Terminal");
setSize(1280, 800);
setDefaultCloseOperation(EXIT_ON_CLOSE);
senden.setText("Senden");
zeichnen.add(senden);
getContentPane().setLayout(new GridLayout(4,4));
JPanel panel1 = new JPanel();
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.setBackground(Color.white);
getContentPane().add(panel1);
getContentPane().setLayout(new GridLayout(4,4));
JPanel panel2 = new JPanel();
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.setBackground(Color.white);
getContentPane().add(panel2);
//Create text area for panel 2
txtsenden = new JTextArea();
txtsenden.setFont(new Font("Arial",
Font.ITALIC, 16));
txtsenden.setLineWrap(true);
txtsenden.setWrapStyleWord(true);
txtsenden.setEditable(true);
JScrollPane areaScrollPane = new
JScrollPane(txtsenden);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(
new Dimension(200,100));
areaScrollPane.setBorder(
BorderFactory.createTitledBorder("Enter Text:"));
panel2.add(areaScrollPane);
// Create text area for panel 1
txtempfangen = new JTextArea();
txtempfangen.setFont(new Font("Arial",
Font.ITALIC, 16));
txtempfangen.setLineWrap(true);
txtempfangen.setWrapStyleWord(true);
txtempfangen.setEditable(true);
JScrollPane areaScrollPane2 = new
JScrollPane(txtempfangen);
areaScrollPane2.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane2.setPreferredSize(
new Dimension(200,100));
areaScrollPane2.setBorder(
BorderFactory.createTitledBorder("Empfangener Text:"));
panel1.add(areaScrollPane2);
this.setJMenuBar(menuBar);
this.add(zeichnen,BorderLayout.WEST);
setVisible(true);
senden.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
sende();
}
});
}
void menuLaden(){
datei.add(laden);
laden.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("REINGEKLICKT");
}
});
datei.add(speichern);
menuBar.add(datei);
}
public static void main(String[] args) {
Runnable r = new JGraph();
new Thread(r).start();
}
public void sende() {
// TODO Auto-generated method stub
sendeSerialPort(txtsenden.getText());
txtsenden.setText("");
}
boolean oeffneSerialPort(String portName) {
// TODO Auto-generated method stub
boolean foundPort = false;
if (serialPortoffen == true) {
System.out.println("Serial Port bereits geöffnet!");
return false;
}
System.out.println("Öffne Serial Port...");
enumComm = CommPortIdentifier.getPortIdentifiers();
while (enumComm.hasMoreElements()) {
serialPortID = (CommPortIdentifier) enumComm.nextElement();
if (portName.contentEquals(serialPortID.getName())) {
foundPort = true;
break;
}
}
if (foundPort == false) {
System.out.println("Serialport nicht gefunden: " + portName);
return false;
}
try {
serialPort = (SerialPort) serialPortID.open("Öffnen und Senden", 500);
} catch (PortInUseException e) {
System.out.println("Port belegt!");
}
try {
os = serialPort.getOutputStream();
} catch (IOException e) {
System.out.println("Keinen Zugriff auf OutputStream");
}
try {
is = serialPort.getInputStream();
} catch (IOException e) {
System.out.println("Keinen Zugriff auf InputStream");
}
try {
serialPort.addEventListener(new serialPortEventListener());
} catch (TooManyListenersException e) {
System.out.println("TooManyListenersException für Serialport");
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(baudrate, dataBits, stopBits, parity);
} catch (UnsupportedCommOperationException e) {
System.out.println("Konnte Schnittstellen Paramter nicht setzen");
}
serialPortoffen = true;
return true;
}
void schließeSerialPort(){
if (serialPortoffen == true) {
System.out.println("Schließe Serialport");
serialPort.close();
serialPortoffen = false;
} else {
System.out.println("Serialport bereits geschlossen");
}
}
void sendeSerialPort(String nachricht)
{
System.out.println("Sende: " + nachricht);
if (serialPortoffen != true)
return;
try {
os.write(nachricht.getBytes());
} catch (IOException e) {
System.out.println("Fehler beim Senden");
}
}
public void run() {
// TODO Auto-generated method stub
if (oeffneSerialPort(portName) == true) {
System.out.println(serialPortoffen);
System.out.println("OFFEN");
}
}
void serialPortDatenVerfuegbar() {
try {
byte[] data = new byte[150];
int num;
while(is.available() > 0) {
num = is.read(data, 0, data.length);
System.out.printIn (txtempfangen + new String(data, 0, num));
}
} catch (IOException e) {
System.out.println("Fehler beim Lesen empfangener Daten");
}
}
class serialPortEventListener implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
System.out.println("serialPortEventlistener");
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
serialPortDatenVerfuegbar();
break;
case SerialPortEvent.BI:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.FE:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
case SerialPortEvent.PE:
case SerialPortEvent.RI:
default:
}
}
}
}