Есть примеры работы с com портом и библиотекой javax.comm , вот как их запустить , точнее вот что надо: SimpleRead: Код |
import java.io.*; import java.util.*; import javax.comm.*;
/** * Class declaration * * * @author * @version 1.8, 08/03/00 */ public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread;
/** * Method declaration * * * @param args * * @see */ public static void main(String[] args) { boolean portFound = false; String defaultPort = "/dev/term/a";
if (args.length > 0) { defaultPort = args[0]; } portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals(defaultPort)) { System.out.println("Found port: "+defaultPort); portFound = true; SimpleRead reader = new SimpleRead(); } } } if (!portFound) { System.out.println("port " + defaultPort + " not found."); } }
/** * Constructor declaration * * * @see */ public SimpleRead() { try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {}
try { inputStream = serialPort.getInputStream(); } catch (IOException e) {}
try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start(); }
/** * Method declaration * * * @see */ public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) {} }
/** * Method declaration * * * @param event * * @see */ 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: byte[] readBuffer = new byte[20];
try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); }
System.out.print(new String(readBuffer)); } catch (IOException e) {}
break; } }
}
|
SimpeWrite: Код |
import java.io.*; import java.util.*; import javax.comm.*;
/** * Class declaration * * * @author * @version 1.10, 08/04/00 */ public class SimpleWrite { static Enumeration portList; static CommPortIdentifier portId; static String messageString = "Hello, world!"; static SerialPort serialPort; static OutputStream outputStream; static boolean outputBufferEmptyFlag = false; /** * Method declaration * * * @param args * * @see */ public static void main(String[] args) { boolean portFound = false; String defaultPort = "/dev/term/a";
if (args.length > 0) { defaultPort = args[0]; }
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) { System.out.println("Found port " + defaultPort);
portFound = true;
try { serialPort = (SerialPort) portId.open("SimpleWrite", 2000); } catch (PortInUseException e) { System.out.println("Port in use.");
continue; }
try { outputStream = serialPort.getOutputStream(); } catch (IOException e) {}
try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {}
try { serialPort.notifyOnOutputEmpty(true); } catch (Exception e) { System.out.println("Error setting event notification"); System.out.println(e.toString()); System.exit(-1); } System.out.println( "Writing \""+messageString+"\" to " +serialPort.getName());
try { outputStream.write(messageString.getBytes()); } catch (IOException e) {}
try { Thread.sleep(2000); // Be sure data is xferred before closing } catch (Exception e) {} serialPort.close(); System.exit(1); } } }
if (!portFound) { System.out.println("port " + defaultPort + " not found."); } }
}
|
Убираю main , ставлю просто метод и создаю main class сам в своем проекте, все это делается , что получилась писалка - читалка , но ничаго не выходит.. как мне правильно эти классы вывести из main , чтоб мне можно было правильно оперировать в своем main? Добавлено через 6 минут и 57 секундМне бы просто самый простой пример составили , отправил сообщение на com , и просчитал ответ, был бы неимоверно благодарен...
--------------------
Нехорошо блин!!!
|