Serial Communication and serial communication

Source: Internet
Author: User

Serial Communication and serial communication

I. Preparations

1. Click here to download java serial communication tools

2. Put RXTXcomm. jar in the % JAVA_HOME % \ jre \ lib \ ext \ directory, and introduce the jar package to the project.

3. Put rxtxSerial. dll in the % JAVA_HOME % \ jre \ bin \ directory.

 

Ii. Serial Port Related Categories

1. Serial Port: comatrix. class

1 public class commp ORT {2/** 3 * serial port name, for example, COM1, COM2 4 */5 private String commp ortname = ""; 6 7/** 8 * serial port owner name, usually the application name 9 */10 private String ownerName; 11 12/** 13 * baud rate 14 */15 private String baudRate = ""; 16 17/** 18 * Data bit 19 */20 private String dataBit = ""; 21 22/** 23 * check digit 24 */25 private String partityBit = ""; 26 27/** 28 * Stop bit 29 */30 private String stopBit = ""; 31 32 public String getcomatrix ortname () {33 return comatrix ortname; 34} 35 36 public void setcomatrix ortname (String comatrix ortname) {37 this. comatrix name = comatrix name; 38} 39 40 public String getOwnerName () {41 return ownerName; 42} 43 44 public void setOwnerName (String ownerName) {45 this. ownerName = ownerName; 46} 47 48 public String getBaudRate () {49 return baudRate; 50} 51 52 public void setBaudRate (String baudRate) {53 this. baudRate = baudRate; 54} 55 56 public String getDataBit () {57 return dataBit; 58} 59 60 public void setDataBit (String dataBit) {61 this. dataBit = dataBit; 62} 63 64 public String getPartityBit () {65 return partityBit; 66} 67 68 public void setPartityBit (String partityBit) {69 this. partityBit = partityBit; 70} 71 72 public String getStopBit () {73 return stopBit; 74} 75 76 public void setStopBit (String stopBit) {77 this. stopBit = stopBit; 78} 79 80}View Code


2. serial port administrator class: commp ortmanager. class

1 import gnu. io. commp ortidentifier; 2 import gnu. io. serialPort; 3 4 import java. io. bufferedReader; 5 import java. io. bufferedWriter; 6 import java. io. dataInputStream; 7 import java. io. dataOutputStream; 8 import java. io. inputStream; 9 import java. io. inputStreamReader; 10 import java. io. outputStream; 11 import java. io. outputStreamWriter; 12 import java. util. enumeration; 13 14 import com. erp. common. configHolder; 15 16 public class commortmanager {17/** 18 * serial port Definition 19 */20 private commort; 21 22/** 23 * serial port object 24/25 private SerialPort serialPort; 26 27/** 28 * byte input 29 */30 private InputStream in; 31 32/** 33 * bytes output 34 */35 private OutputStream out; 36 37/** 38 * characters input 39 */40 private BufferedReader bufReader; 41 42/** 43 * character output 44 */45 private BufferedWriter bufWriter; 46 47/** 48 * Data Input 49 */50 private DataInputStream dataIn; 51 52/** 53 * data output 54 */55 private DataOutputStream dataOut; 56 57/** 58 * Whether the serial port is using 59 */60 private boolean isUse; 61 62 public commp ortmanager (commp ORT) {63 this. comatrix ORT = comatrix ORT; 64} 65 66/** 67 * open serial port 68 * @ throws Exception 69 */70 public void open () throws Exception {71 comatrix ortidentifier comatrix ortid = comatrix ortidentifier. getPortIdentifier (commp ort. getcommp ortname (); 72 73 // The first parameter: usually set to your application name; the second parameter: Number of milliseconds for enabling port timeout 74 serialPort = (SerialPort) commp ortid. open (comatrix. getOwnerName (), 5000); 75 serialPort. setOutputBufferSize (Integer. valueOf (ConfigHolder. getValue ("comm. buffersize "); 76 77 in = serialPort. getInputStream (); 78 out = serialPort. getOutputStream (); 79 80 bufReader = new BufferedReader (new InputStreamReader (in, "Unicode"); 81 bufWriter = new BufferedWriter (new OutputStreamWriter (out )); 82 83 dataIn = new DataInputStream (in); 84 dataOut = new DataOutputStream (out); 85 86 // set the serial port parameter 87 serialPort. setSerialPortParams (Integer. valueOf (commp ort. getBaudRate (), Integer. valueOf (commp ort. getDataBit (), 88 Integer. valueOf (commp ort. getStopBit (), Integer. valueOf (commp ort. getPartityBit (); 89} 90 91/** 92 * determine whether the serial port is available 93 */94 public boolean commp ortenable () {95 boolean result = false; 96 Enumeration en = commp ortidentifier. getPortIdentifiers (); 97 while (en. hasMoreElements () {98 comatrix ortidentifier portId = (comatrix ortidentifier) en. nextElement (); 99 if (portId. getPortType () = comatrix ortidentifier. PORT_SERIAL100 & portId. getName (). equalsIgnoreCase (commp ort. getcommp ortname () {101 result = true; 102} 103} 104 return result; 105} 106 107/** 108 * set the serial port receiving timeout time 109 * @ param timeout time, in seconds 110 * @ throws Exception111 */112 public void setReceiveTimeout (int timeout) throws Exception {113 getSerialPort (). enableReceiveTimeout (timeout * 1000); 114} 115 116 public commort getcommort () {117 return commort; 118} 119 120 public SerialPort getSerialPort () {121 return serialPort; 122} 123 124 public InputStream getIn () {125 return in; 126} 127 128 public OutputStream getOut () {129 return out; 130} 131 132 public BufferedReader getBufReader () {133 return bufReader; 134} 135 136 public BufferedWriter getBufWriter () {137 return bufWriter; 138} 139 140 public DataInputStream getDataIn () {141 return dataIn; 142} 143 144 public DataOutputStream getDataOut () {145 return dataOut; 146} 147 148 public boolean isUse () {149 return isUse; 150} 151 152 public void setUse (boolean isUse) {153 this. isUse = isUse; 154} 155}View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.