Java serial port technology agreement

来源:互联网 发布:纯甘油兑水比例知乎 编辑:程序博客网 时间:2024/06/06 08:24

Java serial port technology agreement


Tag: javascript, API, Communications, j2se, OUTput, Java



Serial Communication outlined the 

There are a variety of serial communication protocol, such as RS232, RS485, RS422, or the now popular USB serial communication protocol. Serial communication technology applications everywhere. We may see the most is the computer's serial port and Modem communications. I remember the PC just pop up in China (about five years ago, in the 1990s), when even someone with a serial line data sharing between the two computers. In addition to these, mobile phones, PDAs, USB mouse, keyboard, etc. are connected to the computer serial communication. Author nature of their work, have access to a more like a multiport serial cards, various kinds of testing and measuring instruments with a serial communication interface, serial communication network equipment. 

Serial communication there are many, but I know in the entire electronic communications products, RS232 communication is the most common. USB interface electronics is endless, but look at Java serial communication technology as well as necessary, maybe readers who would like to use this technique to write sharing of data between a PDA and a computer program.

In this paper, RS232-based serial communication to explain the Java technology. 

RS232 communications infrastructure 

RS-232-C (also known as EIA RS-232-C, hereinafter referred to as RS232) is to develop joint Bell System, modem manufacturers and computer terminal manufacturer in 1970 by the Electronic Industries Association (EIA) for serial communication standards. RS232 is a full-duplex communication protocol, it can receive and send data at the same time. Usually have two kinds: 9-pin (DB9) RS232 port and 25-pin (DB25). 

DB9 and DB25 common pin definitions Common edges 

Common means of communication is a three-wire, two RS232 devices sender (TXD) and the receiver (RXD) and ground terminal (GND) are connected to each other, but also many readers know the connection: 
This way both ends of the RS232 interface - 3,3 --- 2, 5 (7) --- (7) pin connector. 2 is a data receiving line (RXD), the data transmission line (TXD), 5 (7) is grounded (RND). If you have a desktop PC and a Notebook computer, you can connect this way. With a three-wire RS232 device is connected. But if you recognize died 2 - 3,3 - 2, 5 (7) - 5 (7) butt this argument, you will find even some RS232 device does not work. This is because some devices within the circuit has 2 and 3-wire exchange over a one-to-one on the line as long as 2, 3, 5 (7) needle. 

Tips: how to identify the TXD and RXD port? 

Engage in electronic hand should be standing a meter to Cece voltage, resistance will be useful. You just were measured RS232 port 2 - or 3 - 5 pin voltage, usually between the TXD pin and GND, there will be a negative voltage of about 15V, which means that it is the TXD pin. 
Install the Java Communications API 

Sun's J2SE and does not directly provide the above-mentioned any kind of serial communication protocol development kit, but independent in jar posted on java.sun.com (download from here) ---- Comm.jar called Java ™ Communications API, it is a standard extension of J2SE. comm.jar not only recently, as early as 1998, sun had already released the development kit.comm.jar common RS232 serial port and IEEE1284 parallel port communications support. The sun's comm.jar only two versions of Windows and Solaris platforms, if you need the Linux platform, can be found in http://www.geeksville.com/ ~ kevinh / linuxcomm.html. 

For before comm.jar, you must know how to install it. This is also a problem plagued many beginner java RS232 communication. If we installed on your computer JDK, it will at the same time we install a JRE (the Java Runtime Entironment), usually we run the program JRE to run. Following installation for the JRE. If you are using JDK to run the program, insert the appropriate changed. 

The after to download comm.jar development kit, together with the two important documents, win32com.dll and the javax.comm.properties. comm.jar provides communication with the Java API, and win32com.dll local drive interface for comm.jar call. Javax.comm.properties the driver class configuration file. The first comm.jar copy to the libext directory. The then win21com.dll copy to your RS232 applications running directory, or user.dir. And then copy javax.comm.properties lib directory. 

Preparations for Communication 

If you do not off-the-shelf standard RS232 serial device, you can be your own computer simulation into two different serial devices. Usually host computer behind panel provides two 9-pin serial port, set both serial 2,3,5 feet connected by the method described above. Electronics market has a ready-made connectors to sell, please do not buy the kind of package tightly fitting and buy a package can be opened with a screw connector, so you can easily according to their own need to connect each pin. 
Comm API basis 

I have no intention in the use of the Comm API classes and interfaces described in detail here, but I will introduce the class structure of the Comm API and several important usage of the API. 

The comm API is located in the javax.comm package below. Comm API javadoc introduced to us only a few of the following 13 classes or interfaces: 

javax.comm.CommDriver 

javax.comm.CommPort 

javax.comm.ParallelPort 

javax.comm.SerialPort 

javax.comm.CommPortIdentifier 

javax.comm.CommPortOwnershipListener 

javax.comm.ParallelPortEvent 

javax.comm.SerialPortEvent 

javax.comm.ParallelPortEventListener (extends java.util.EventListener) 

javax.comm.SerialPortEventListener (extends java.util.EventListener) 

javax.comm.NoSuchPortException 

javax.comm.PortInUseException 

javax.comm.UnsupportedCommOperationException 

Explain several major class or interface. 

Enumerate all the RS232 port 

Before you start using the RS232 port, we want to know which ports are available, the following code lists all available RS232 port system: 

Enumeration en = CommPortIdentifier.getPortIdentifiers (); 

CommPortIdentifier portId; 

while (en.hasMoreElements, ()) 



portId = (CommPortIdentifier) ??en.nextElement (); 

/ * If the port type is serial, then print out the port information * / 

if (portId.getPortType () == CommPortIdentifier.PORT_SERIAL) 



System.out.println (portId.getName, ()); 





The above program on my computer output the following results: 

COM1 

COM2 

The class getPortIdentifiers CommPortIdentifier can find all the serial port of the system, each serial port corresponding to an instance of the class CommPortIdentifier. 

(2) open ports 

If you use a port, you must first open it. 

try { 

CommPort serialPort = portId.open ("My App", 60); 

/ * Read data from the port * / 

InputStream input = serialPort.getInputStream (); 

input.read (...); 

/ * Write data to the port * / 

OutputStream output = serialPort.getOutputStream (); 

output.write (...) 

... 

} Catch (PortInUseException, ex) 

{...} 

Through the open method of CommPortIdentifier return a CommPort object. The open method has two parameters, the first is a String, is usually set to the name of your application. The second parameter is the time to open the port number of milliseconds timeout. When the port is occupied by another application, will throw abnormal PortInUseException. 

Here CommPortIdentifier classes and CommPort of class what difference does it make? In fact, both of them are one-to-one relationship. Major CommPortIdentifier responsible for port initialization and open, and manage their tenure. CommPort is associated with the actual input and output functions. By the getInputStream CommPort () to obtain the port input stream, which is the java.io.InputStream an instance of the interface. We can use operating interface standard InputStream to read the data in the stream, as by FileInputSteam read the contents of the file. Corresponding getOutputStream of CommPort can obtain the output port flow, so can the serial output data. 

3 Close the port 

You are finished using the port, you must remember to turn it off, which would allow other programs have the opportunity to use it, or other programs using the port may be thrown when an error in the port is in use. The strange thing is that the The the CommPortIdentifier class provides methods to open the port, and to shut down the port, close () method will have to call CommPort class. 
The the input stream CommPort read the file input stream some do not like, that is, you may never know how this InputStream when the end of the end of transmission unless the other OutputStream to send you a specific data, you receive this particular character , then the line Close your InputStream. Comm.jar provides two flexible way allows you to read data. 

1 polling (Polling) 

For example, with GF get together and go out to see a movie, but your GF fashionable, this dress is probably half an hour or even more than one hour. Then you have lost its reminders every two minutes "did not?" Thus, until your GF said OK considered finished. This is called polling (Polling). 

In the program, the polling is usually designed as a closed loop, that is the end of the cycle when a condition is met. Just that example, your GF said "OK!" This is the end of your polling.In a single-threaded program, when the cycle has been to perform a task and can not predict when the end of your program might look like a crash. VB program, this problem can be used in the loop structure to solve to insert a doEvent statement. Java, the best way is to use threads, like the following code snippet. 

public TestPort extend Thread 



... 

InputStream input = serialPort.getInputStream (); 

StringBuffer buf = new StringBuffer (); 

boolean stopped = false; 

... 

public void run () 



try { 

while (! stopped) 

int ch = input.read (); 

if (ch == 'q' | | ch == 'Q') 



/ * End of the read, shut down the port ... * / 

stopped = true; 

... 



else 



buf.append ((char) ch); 

... 



} Catch (InterruptedException e) {} 






(2) listening (listening) 

COMM API support standard Java Bean-event model. That is, you can use of similar AddXXXListener this method for a serial register their listener to listen for data reading. 

If you want to port to listen, you must first obtain an instance of class CommPortIdentifier, 

CommPort serialPort = portId.open ("My App", 60); 

To obtain of the SerialPort, and then call the addEventListener method to add a listener for it, 

serialPort.addEventListener (new MyPortListener ()); 

The SerialPort listener must inherit in SerialPortEventListener interface. Any SerialPort event occurs, it will automatically call the listener in serialEvent methods. Serial event have the following types: 

BI - communication is interrupted. 

CD - carrier Detection. 

CTS - Clear to Send. 

The DATA_AVAILABLE - data arrives. 

DSR - Data equipment ready. 

FE - framing error. 

OE - overflow error. 

OUTPUT_BUFFER_EMPTY - the output buffer is empty. 

PE - Parity Error. 

RI - ringing indication. 

Here is an example of a listener: 

public void MyPortListener implements SerialPortEventListener 



public void serialEvent (SerialPortEvent evt) 



switch (evt.getEventType, ()) 



case SerialPortEvent.CTS: 

System.out.println ("CTS event occured."); 

break; 

case SerialPortEvent.CD: 

System.out.println ("CD event occured."); 

break; 

case SerialPortEvent.BI: 

System.out.println ("BI event occured."); 

break; 

case SerialPortEvent.DSR: 

System.out.println ("DSR event occured."); 

break; 

case SerialPortEvent.FE: 

System.out.println ("FE event occured."); 

break; 

case SerialPortEvent.OE: 

System.out.println ("OE event occured."); 

break; 

case SerialPortEvent.PE: 

System.out.println ("PE event occured."); 

break; 

case SerialPortEvent.RI: 

System.out.println ("RI event occured."); 

break; 

case SerialPortEvent.OUTPUT_BUFFER_EMPTY: 

System.out.println ("OUTPUT_BUFFER_EMPTY event occured."); 

break; 

case SerialPortEvent.DATA_AVAILABLE: 

System.out.println ("DATA_AVAILABLE event occured."); 

int ch; 

StringBuffer buf = new StringBuffer (); 

InputStream input = serialPort.getInputStream 

try { 

while ((ch = input.read ())> 0) { 

buf.append ((char) ch); 



The System.out.print (buf); 

} Catch (IOException e) {} 

break; 





This listener simply print the name of each event. For most applications, usually concerned is DATA_AVAILABLE events, when data is transmitted from an external device to the port up will trigger this event. At this point you can use the previously mentioned methods, serialPort.getInputStream () to the data read from the InputStream.
原创粉丝点击