📄 v24.java
字号:
); } /** * flush receiver * @throws IOException */ public void flushReceiver () throws IOException { while (is.available() > 0) is.read(); } /** * @param b content to be sent (do not perform flush after sending) * @throws IOException */ public synchronized void send (byte[] b) throws IOException { os.write (b); Logger.log (new LogEvent (this, "send", ISOUtil.dumpString(b))); } /** * @param b content to be sent (do not perform flush after sending) * @throws IOException */ public synchronized void send (byte b) throws IOException { os.write (b); byte[] ab = new byte[1]; ab[0] = b; Logger.log (new LogEvent (this, "send", ISOUtil.dumpString(ab))); } /** * @throws IOException */ public void flushTransmitter () throws IOException { boolean retry = true; while (retry) { try { os.flush (); retry = false; } catch (IOException e) { Thread.yield(); retry = true; } } } /** * @param s content to be sent (performs flush after sending) * @throws IOException */ public synchronized void send (String s) throws IOException { byte[] b = s.getBytes(); send (b); flushTransmitter (); Logger.log (new LogEvent (this, "send-and-flush", ISOUtil.dumpString(b))); } /** * @param b buffer * @param timeout in milliseconds * @return number of characters actually read * @throws IOException */ public int read (byte[] b, long timeout) throws IOException { int i; long max = System.currentTimeMillis() + timeout; for (i=0; i<b.length && System.currentTimeMillis() < max; ) { synchronized (this) { if (is.available() > 0) b[i++] = (byte) is.read(); else { long sleep = max - System.currentTimeMillis(); if (sleep > 0) { try { wait (sleep); // if (!port.isDSR() || (watchCD && !port.isCD())) if ((watchCD && !port.isCD())) throw new IOException ("DSR/CD off"); } catch (InterruptedException e) { } } } } } return i; } /** * @param end pattern * @param timeout in milliseconds * @param includeLast true if terminating char should be included * @return string including end character (may be 0 length) * @throws IOException */ public String readUntil (String end, long timeout, boolean includeLast) throws IOException { LogEvent evt=new LogEvent (this, "readUntil", ISOUtil.dumpString (end.getBytes())); StringBuffer buf = new StringBuffer(); timeout = Math.abs (timeout); long max = System.currentTimeMillis() + timeout; setAutoFlushReceiver(false); for (;;) { if (System.currentTimeMillis() > max) { evt.addMessage ("<timeout>"+timeout+"</timeout>"); break; } synchronized (this) { if (is.available() > 0) { char c = (char) is.read(); if (end.indexOf(c) >= 0) { if (includeLast) buf.append (c); evt.addMessage ("<match/>"); break; } if (buf.length() < MAX_STRING_SIZE) // paranoia check buf.append (c); } else { long sleep = max - System.currentTimeMillis(); if (sleep > 0) { try { wait (sleep); // if (!port.isDSR() || (watchCD && !port.isCD())) if ((watchCD && !port.isCD())) throw new IOException ("DSR/CD off"); } catch (InterruptedException e) { } } } } } String ret = buf.toString(); evt.addMessage ( "<read>"+ISOUtil.dumpString(ret.getBytes())+"</read>" ); Logger.log (evt); return ret; } /** * reads until newline or time expired * @param timeout in milliseconds * @return string (may be 0 length) * @throws IOException */ public String readLine (long timeout) throws IOException { return readUntil ("\n", timeout, false); } /** * @param timeout in milliseconds * @param maxsize maximun size * @throws IOException */ public String readString (long timeout, int maxsize) throws IOException { StringBuffer buf = new StringBuffer(); byte[] b = new byte[maxsize]; int c = read (b, timeout); return new String (b, 0, c); } /** * @param pattern * @param timeout in milliseconds * @throws IOException */ public int waitfor(String[] pattern, int timeout) throws IOException { long start = System.currentTimeMillis(); int match = -1; byte[] buf = new byte[1]; StringBuffer readBuffer = new StringBuffer(); LogEvent evt = new LogEvent (this, "waitfor"); for (int i=0; i<pattern.length; i++) evt.addMessage ( i +":" +ISOUtil.dumpString(pattern[i].getBytes()) ); long expire = System.currentTimeMillis() + timeout; while (expire > System.currentTimeMillis()) { readBuffer.append ( readString (expire - System.currentTimeMillis(), 1) ); String s = readBuffer.toString(); for (int i=0; i<pattern.length; i++) { if (s.indexOf(pattern[i]) >= 0) { match = i; expire = 0; } } } evt.addMessage ("<buffer match=\"" + match + "\" elapsed=\"" + (System.currentTimeMillis() - start) + "\">" +ISOUtil.dumpString (readBuffer.toString().getBytes()) +"</buffer>"); Logger.log (evt); return match; } /** * @param pattern * @param timeout in milliseconds * @throws IOException */ public int waitfor(String pattern, int timeout) throws IOException { String[] s = new String[1]; s[0] = pattern; return waitfor (s, timeout); } /** * @param sendString * @param pattern * @param timeout in milliseconds * @throws IOException */ public int waitfor(String sendString, String pattern, int timeout) throws IOException { send (sendString); String[] s = new String[1]; s[0] = pattern; return waitfor (s, timeout); } /** * @param sendString * @param pattern * @param timeout in milliseconds * @throws IOException */ public int waitfor(String sendString, String[] pattern, int timeout) throws IOException { send (sendString); return waitfor (pattern, timeout); } public synchronized void dtr (boolean value) throws IOException { Logger.log (new LogEvent (this, "dtr", value ? "on" : "off")); port.setDTR (value); } private CommPortIdentifier getPortIdentifier(String name) throws IOException { Enumeration ports = CommPortIdentifier.getPortIdentifiers(); LogEvent evt = new LogEvent (this, "getPortIdentifier"); while (ports.hasMoreElements()) { CommPortIdentifier id = (CommPortIdentifier) ports.nextElement(); if (id.getPortType()==CommPortIdentifier.PORT_SERIAL) { if(id.getName().equals(name)) { evt.addMessage ("found:"+id.getName()); Logger.log (evt); return id; } evt.addMessage (" got:"+id.getName()); } } IOException e = new IOException ("invalid port "+name); evt.addMessage (e); Logger.log (evt); throw e; } private void initPort () throws IOException, PortInUseException { port = (SerialPort) portId.open (realm, 2000); // if (!port.isDSR() || !port.isCD()) if (!port.isCD()) lostCD = System.currentTimeMillis(); try { port.addEventListener (this); port.notifyOnDSR(true); port.notifyOnCarrierDetect(true); port.notifyOnDataAvailable(true); } catch (TooManyListenersException e) { Logger.log (new LogEvent (this, "initPort", e)); } is = port.getInputStream(); os = port.getOutputStream(); } public static void main (String[] args) { Logger l = new Logger(); l.addListener (new SimpleLogListener (System.out)); V24 v24 = null; try { v24 = new V24 ("/dev/ttyS0", l, "V24"); v24.setSpeed (2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE, 0 // SerialPort.FLOWCONTROL_RTSCTS_IN | // SerialPort.FLOWCONTROL_RTSCTS_OUT ); Thread.sleep (1000); // v24.send ("AT\r"); // v24.waitfor ("OK", 60000); new Thread() { public void run() { for (int i=0;;i++) { System.out.println (i); Thread.yield(); } } }.start(); Thread.sleep (120000); // Modem mdm = new SimpleDialupModem (v24); // mdm.dial ("000000", 60000); } catch (IOException e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } if (v24 != null) v24.close(); } public void setAutoFlushReceiver (boolean autoFlushRX) { if ( (this.autoFlushRX = autoFlushRX) ) try { flushReceiver(); } catch (IOException e) { } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -