📄 serial.java
字号:
/** * Just like last() and readChar(). */ public char lastChar() { if (bufferIndex == bufferLast) return (char)(-1); return (char) last(); } /** * Return a byte array of anything that's in the serial buffer. * Not particularly memory/speed efficient, because it creates * a byte array on each read, but it's easier to use than * readBytes(byte b[]) (see below). */ public byte[] readBytes() { if (bufferIndex == bufferLast) return null; synchronized (buffer) { int length = bufferLast - bufferIndex; byte outgoing[] = new byte[length]; System.arraycopy(buffer, bufferIndex, outgoing, 0, length); bufferIndex = 0; // rewind bufferLast = 0; return outgoing; } } /** * Grab whatever is in the serial buffer, and stuff it into a * byte buffer passed in by the user. This is more memory/time * efficient than readBytes() returning a byte[] array. * * Returns an int for how many bytes were read. If more bytes * are available than can fit into the byte array, only those * that will fit are read. */ public int readBytes(byte outgoing[]) { if (bufferIndex == bufferLast) return 0; synchronized (buffer) { int length = bufferLast - bufferIndex; if (length > outgoing.length) length = outgoing.length; System.arraycopy(buffer, bufferIndex, outgoing, 0, length); bufferIndex += length; if (bufferIndex == bufferLast) { bufferIndex = 0; // rewind bufferLast = 0; } return length; } } /** * Reads from the serial port into a buffer of bytes up to and * including a particular character. If the character isn't in * the serial buffer, then 'null' is returned. */ public byte[] readBytesUntil(int interesting) { if (bufferIndex == bufferLast) return null; byte what = (byte)interesting; synchronized (buffer) { int found = -1; for (int k = bufferIndex; k < bufferLast; k++) { if (buffer[k] == what) { found = k; break; } } if (found == -1) return null; int length = found - bufferIndex + 1; byte outgoing[] = new byte[length]; System.arraycopy(buffer, bufferIndex, outgoing, 0, length); bufferIndex += length; if (bufferIndex == bufferLast) { bufferIndex = 0; // rewind bufferLast = 0; } return outgoing; } } /** * Reads from the serial port into a buffer of bytes until a * particular character. If the character isn't in the serial * buffer, then 'null' is returned. * * If outgoing[] is not big enough, then -1 is returned, * and an error message is printed on the console. * If nothing is in the buffer, zero is returned. * If 'interesting' byte is not in the buffer, then 0 is returned. */ public int readBytesUntil(int interesting, byte outgoing[]) { if (bufferIndex == bufferLast) return 0; byte what = (byte)interesting; synchronized (buffer) { int found = -1; for (int k = bufferIndex; k < bufferLast; k++) { if (buffer[k] == what) { found = k; break; } } if (found == -1) return 0; int length = found - bufferIndex + 1; if (length > outgoing.length) { System.err.println("readBytesUntil() byte buffer is" + " too small for the " + length + " bytes up to and including char " + interesting); return -1; } //byte outgoing[] = new byte[length]; System.arraycopy(buffer, bufferIndex, outgoing, 0, length); bufferIndex += length; if (bufferIndex == bufferLast) { bufferIndex = 0; // rewind bufferLast = 0; } return length; } } /** * Return whatever has been read from the serial port so far * as a String. It assumes that the incoming characters are ASCII. * * If you want to move Unicode data, you can first convert the * String to a byte stream in the representation of your choice * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. */ public String readString() { if (bufferIndex == bufferLast) return null; return new String(readBytes()); } /** * Combination of readBytesUntil and readString. See caveats in * each function. Returns null if it still hasn't found what * you're looking for. * * If you want to move Unicode data, you can first convert the * String to a byte stream in the representation of your choice * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. */ public String readStringUntil(int interesting) { byte b[] = readBytesUntil(interesting); if (b == null) return null; return new String(b); } /** * This will handle both ints, bytes and chars transparently. */ public void write(int what) { // will also cover char try { output.write(what & 0xff); // for good measure do the & output.flush(); // hmm, not sure if a good idea } catch (Exception e) { // null pointer or serial port dead errorMessage("write", e); } } public void write(byte bytes[]) { try { output.write(bytes); output.flush(); // hmm, not sure if a good idea } catch (Exception e) { // null pointer or serial port dead //errorMessage("write", e); e.printStackTrace(); } } /** * Write a String to the output. Note that this doesn't account * for Unicode (two bytes per char), nor will it send UTF8 * characters.. It assumes that you mean to send a byte buffer * (most often the case for networking and serial i/o) and * will only use the bottom 8 bits of each char in the string. * (Meaning that internally it uses String.getBytes) * * If you want to move Unicode data, you can first convert the * String to a byte stream in the representation of your choice * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. */ public void write(String what) { write(what.getBytes()); } /** * If this just hangs and never completes on Windows, * it may be because the DLL doesn't have its exec bit set. * Why the hell that'd be the case, who knows. */ static public String[] list() { Vector<String> list = new Vector<String>(); try { //System.err.println("trying"); Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers(); //System.err.println("got port list"); while (portList.hasMoreElements()) { CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); //System.out.println(portId); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { String name = portId.getName(); list.addElement(name); } } } catch (UnsatisfiedLinkError e) { //System.err.println("1"); errorMessage("ports", e); } catch (Exception e) { //System.err.println("2"); errorMessage("ports", e); } //System.err.println("move out"); String outgoing[] = new String[list.size()]; list.copyInto(outgoing); return outgoing; } /** * General error reporting, all corraled here just in case * I think of something slightly more intelligent to do. */ static public void errorMessage(String where, Throwable e) { e.printStackTrace(); throw new RuntimeException("Error inside Serial." + where + "()"); }} /* class SerialMenuListener implements ItemListener { //public SerialMenuListener() { } public void itemStateChanged(ItemEvent e) { int count = serialMenu.getItemCount(); for (int i = 0; i < count; i++) { ((CheckboxMenuItem)serialMenu.getItem(i)).setState(false); } CheckboxMenuItem item = (CheckboxMenuItem)e.getSource(); item.setState(true); String name = item.getLabel(); //System.out.println(item.getLabel()); PdeBase.properties.put("serial.port", name); //System.out.println("set to " + get("serial.port")); } } */ /* protected Vector buildPortList() { // get list of names for serial ports // have the default port checked (if present) Vector list = new Vector(); //SerialMenuListener listener = new SerialMenuListener(); boolean problem = false; // if this is failing, it may be because // lib/javax.comm.properties is missing. // java is weird about how it searches for java.comm.properties // so it tends to be very fragile. i.e. quotes in the CLASSPATH // environment variable will hose things. try { //System.out.println("building port list"); Enumeration portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); //System.out.println(portId); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { //if (portId.getName().equals(port)) { String name = portId.getName(); //CheckboxMenuItem mi = //new CheckboxMenuItem(name, name.equals(defaultName)); //mi.addItemListener(listener); //serialMenu.add(mi); list.addElement(name); } } } catch (UnsatisfiedLinkError e) { e.printStackTrace(); problem = true; } catch (Exception e) { System.out.println("exception building serial menu"); e.printStackTrace(); } //if (serialMenu.getItemCount() == 0) { //System.out.println("dimming serial menu"); //serialMenu.setEnabled(false); //} // only warn them if this is the first time if (problem && PdeBase.firstTime) { JOptionPane.showMessageDialog(this, //frame, "Serial port support not installed.\n" + "Check the readme for instructions\n" + "if you need to use the serial port. ", "Serial Port Warning", JOptionPane.WARNING_MESSAGE); } return list; } */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -