📄 serial.java
字号:
package etaai.comunicatii.comm;import javax.comm.*;import java.io.*;import java.util.*;/** * Title: Serial LCD * Description: Comnada display lcd pe seriala * Copyright: Copyright (c) 2001 * Company: ETA AI * @author Mircea Pop * @version 1.01 * * @modif Adaugare descriere functii ( 14.01.2002) */public class Serial implements SerialPortEventListener{ /**Serial port name*/ private String portName = "COM1"; /**Serial Port*/ private SerialPort sp = null; /**Input Stream */ private InputStream sin = null; /**Output Stream*/ private OutputStream sout = null; /*BaudRate*/ private int bufferSize = 512; /** * The Class Serial implements comunication with serial port allowing sending and receiving serial data. * The comunication baudrate is 9600bps(default) * * @param String portName Serial port name * * @param int bufferSize Input output buffer size * * @throws PortInUseException,NoSuchPortException,UnsupportedCommOperationException,IOException When Exception occurs * on serial port initialization */ public Serial(String portName, int bufferSize) throws PortInUseException,NoSuchPortException,IOException,UnsupportedCommOperationException { this.portName = portName; this.bufferSize = bufferSize; serialPorts(); sp = (SerialPort)CommPortIdentifier.getPortIdentifier(portName).open("Kendro" , 40000); //Streams initialization sin = sp.getInputStream(); sout = sp.getOutputStream(); sp.setInputBufferSize(bufferSize); sp.setOutputBufferSize(bufferSize); //Flow control initialization : None setFlowControl(0); //Setting serial port parameters setSerialPortParam(9600, SerialPort.DATABITS_8 , SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } /** * SerialPortEventListener interface method * * @param SerialPortEvent event * */ public void serialEvent(SerialPortEvent event){} /* * Read serial data * * @return String The serial data that has been read * @throws IOException,NullPointerException When exception occurs reading serial data * */ public String getSerialData() throws IOException,NullPointerException { boolean valid_data = false; int count = 0; StringBuffer buffer = new StringBuffer(); while((count = sin.available()) > 0) { byte[] aux_buffer = new byte[count]; sin.read(aux_buffer,0,count); buffer.append(new String(aux_buffer)); valid_data = true; } if(!valid_data) return null; return buffer.toString(); } /** * Send data to the serial port * * @param String data The data that will be sent * @throws IOException When exception occurs reading serial data */ public void sendSerialData(String data) throws IOException { emptyBuffer(); sout.write(data.getBytes()); sout.flush(); } /** * Send data to the serial port * * @param byte[] data The data that will be sent * @throws IOException When exception occurs reading serial data */ public void sendSerialData(byte[] data) throws IOException { emptyBuffer(); sout.write(data); sout.flush(); } /** * Send data to the serial port * * @param int data The data that will be sent * @throws IOException When exception occurs reading serial data */ public void sendSerialData(int data) throws IOException { emptyBuffer(); sout.write(data); sout.flush(); } /** * Send data to the serial port * * @param int[] data The data that will be sent * @throws IOException When exception occurs reading serial data */ public void sendSerialData(int [] data) throws IOException { emptyBuffer(); for(int i=0;i<data.length;i++) sout.write(data[i]); sout.flush(); } /* * Read serial data * * @return String The serial data that has been read * @throws IOException,NullPointerException When exception occurs reading serial data * */ public int[] getSerialData(int size) throws IOException,NullPointerException { int[] data = new int[1000]; int count = 0; size = 0; while((count = sin.available()) > 0) { if(size > data.length) { int [] aux = new int[2*size]; System.arraycopy(data,0,aux,0,data.length); data = aux; } data[size] = sin.read(); size++; } int [] aux = new int[size]; System.arraycopy(data,0,aux,0,size); return aux; } /** * Empties serial port input buffer . */ private void emptyBuffer() { try { sin.skip((long)sin.available()); } catch(IOException _){} } /** * Sets the serial port parameters * * @param int baudRate Serial port baudRate * @param int data_bits Data bits (5,6,7,8) * @param int stop_bits Stop bits (1,1 1/2,2) * @param int parity Parity (even,odd,none) * * @throws UnsupportedCommOperationException When cannot set serial parameters */ public void setSerialPortParam(int baudRate, int data_bits, int stop_bits, int parity) throws UnsupportedCommOperationException { sp.setSerialPortParams(baudRate , data_bits, stop_bits, parity); } /** * Sets flow control * * @param int control Flow control type * 0 - None_Control * 1 - Soft_Control * 3 - Hard_Control * @throws UnsupportedCommOperationException When flow control cannot be set */ public void setFlowControl(int control) throws UnsupportedCommOperationException { switch(control) { case 0 : sp.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); break; case 1 : sp.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT); break; case 2 : sp.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT); break; } } /** * Sets DTR state * @param booolean value DTR State * true - DTR activated * false - DTR deactivated */ public void setDTR(boolean value) { sp.setDTR(value); } /** * Sets CTS state * @param booolean value CTS State * true - CTS activated * false - CTS deactivated */ public void setCTS(boolean value) { sp.setRTS(value); } /** * Sets RTS state * @param booolean value RTS State * true - RTS activated * false - RTS deactivated */ public void setRTS(boolean value) { sp.setRTS(value); } /** * Shows all available serial ports */ public void serialPorts() { System.out.println("Serial ports"); Enumeration enum = CommPortIdentifier.getPortIdentifiers(); System.out.println(enum.hasMoreElements()); for(;enum.hasMoreElements();) System.out.println(((CommPortIdentifier)enum.nextElement()).getName()); } /** * Close the serial port */ public void close() { sp.close(); } public void finalize() { close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -