M
mgiaco
Gast
Hallo ich habe mal eine Frage zu java und RS232. Ich möchte über die Serielle Schnittstelle jede 40msec ca. 60 Bytes versenden vielleicht auch noch 10 mehr oder so. Nur habe ich das Problem das ich diese Geschwindigkeit nicht erreiche. Ich verwende RXTX weil es ja die ComAPI von Sun für Windows nicht mehr gibt.
Kann es sien das diese Geschwindigkeit nicht möglich ist ? Was mache ich falsch??
Wenn ich mit dem Oszilloskop messe wird jede msec nur 1 Byte gesendet. Geht es wirklich nicht schneller?
Hier mein code.
Ich rufe in einem Thread immer wirte(data) auf.
public class RS232 implements SerialPortEventListener {
final byte LF = 0x0A, CR = 0x0D;
private SerialPort serialPort;
private InputStream inputStream;
private OutputStream outputStream;
private PrintWriter outPrint;
private java.io.BufferedOutputStream buffout;
private boolean STATUS;
public RS232(String comPort, int baudRate) {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
boolean portFound = false;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
System.out.println("Found port: " + comPort);
portFound = true;
break;
}
}
}
if (!portFound) {
if(RS232Config.DEBUG) System.out.println("port " + comPort + " not found.");
STATUS = false;
}
else if(portFound)
{
try {
serialPort = (SerialPort) portId.open("COMPORT", 2000);
} catch (PortInUseException e) {
e.printStackTrace();
//System.exit(1);
}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
outPrint = new PrintWriter(outputStream);
buffout = new BufferedOutputStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
STATUS = true;
}
}
public boolean getSTATUS()
{
return this.STATUS;
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
//while(this.availableToRec()>0)
//System.out.print(this.read());
break;
}
}
public void write(byte[] byteArray) {
outPrint.print(byteArray);
outPrint.flush();
}
/*public int availableToSend() {
return 128;
}*/
public void write(byte b) {
//outPrint.print((byte)b);
//outPrint.flush();
try {
buffout.write(b);
buffout.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void write(int b) {
outPrint.print(b);
outPrint.flush();
}
public void write(String b) {
outPrint.print(b);
outPrint.flush();
}
public int availableToRec() {
try {
return inputStream.available();
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
public int read() {
try {
return inputStream.read();
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
public int read(byte[] b) {
try {
return inputStream.read(b);
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
public void close()
{
this.serialPort.close();
}
public static String[] getPorts() {
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
LinkedList<String> portList = new LinkedList<String>();
CommPortIdentifier portId = null;
while (portIdentifiers.hasMoreElements()) {
portId = (CommPortIdentifier) portIdentifiers.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
portList.addLast(portId.getName());
if(RS232Config.DEBUG) System.out.println(portId.getName());
}
}
return ((String[])portList.toArray(new String[1]));
}
}
Besten Dank im Voraus
mfg mathias g.
Kann es sien das diese Geschwindigkeit nicht möglich ist ? Was mache ich falsch??
Wenn ich mit dem Oszilloskop messe wird jede msec nur 1 Byte gesendet. Geht es wirklich nicht schneller?
Hier mein code.
Ich rufe in einem Thread immer wirte(data) auf.
public class RS232 implements SerialPortEventListener {
final byte LF = 0x0A, CR = 0x0D;
private SerialPort serialPort;
private InputStream inputStream;
private OutputStream outputStream;
private PrintWriter outPrint;
private java.io.BufferedOutputStream buffout;
private boolean STATUS;
public RS232(String comPort, int baudRate) {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
boolean portFound = false;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
System.out.println("Found port: " + comPort);
portFound = true;
break;
}
}
}
if (!portFound) {
if(RS232Config.DEBUG) System.out.println("port " + comPort + " not found.");
STATUS = false;
}
else if(portFound)
{
try {
serialPort = (SerialPort) portId.open("COMPORT", 2000);
} catch (PortInUseException e) {
e.printStackTrace();
//System.exit(1);
}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
outPrint = new PrintWriter(outputStream);
buffout = new BufferedOutputStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
STATUS = true;
}
}
public boolean getSTATUS()
{
return this.STATUS;
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
//while(this.availableToRec()>0)
//System.out.print(this.read());
break;
}
}
public void write(byte[] byteArray) {
outPrint.print(byteArray);
outPrint.flush();
}
/*public int availableToSend() {
return 128;
}*/
public void write(byte b) {
//outPrint.print((byte)b);
//outPrint.flush();
try {
buffout.write(b);
buffout.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void write(int b) {
outPrint.print(b);
outPrint.flush();
}
public void write(String b) {
outPrint.print(b);
outPrint.flush();
}
public int availableToRec() {
try {
return inputStream.available();
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
public int read() {
try {
return inputStream.read();
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
public int read(byte[] b) {
try {
return inputStream.read(b);
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
public void close()
{
this.serialPort.close();
}
public static String[] getPorts() {
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
LinkedList<String> portList = new LinkedList<String>();
CommPortIdentifier portId = null;
while (portIdentifiers.hasMoreElements()) {
portId = (CommPortIdentifier) portIdentifiers.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
portList.addLast(portId.getName());
if(RS232Config.DEBUG) System.out.println(portId.getName());
}
}
return ((String[])portList.toArray(new String[1]));
}
}
Besten Dank im Voraus
mfg mathias g.