An embedded system usually needs to carry out Full-duplex communication with its main control system, for example, a pipelined control system needs to continuously accept the query and control information sent from the master control system, and send the execution result or query result back to the master control system. This paper introduces a simple Java class library which realizes Full-duplex communication through serial port, which simplifies the operation process of serial port greatly.
This class library mainly includes: Serialbean.java (interface with other applications), Serialbuffer.java (used to save the data received from the serial port buffer area), Readserial.java (from the serial port to read the data program). In addition, this class library also provides a routine Serialexample.java as a demonstration. Each of these sections is described in detail in the following section.
1.SerialBean
Serialbean is the interface of this class library with other applications. This class library defines the construction method of Serialbean and the function of initializing serial port, reading data from serial port, writing data to serial port and shutting down serial port. The details are as follows:
Public Serialbean (int portid)
This function constructs a serialbean that points to a specific serial port, which is specified by the parameter portid. Portid = 1 means that Com1,portid = 2 represents COM2, and so on.
public int Initialize ()
This function initializes the specified serial port and returns the initialization result. Returns 1 if the initialization succeeds in returning 1. The result of initialization is that the serial port is Serialbean exclusive and its parameters are set to 9600, N, 8, 1. If the serial port is initialized successfully, a process is opened to read the incoming data from the serial port and save it in a buffer.
Public String readport (int Length)
This function reads a string of a specified length from a serial port (buffer). Parameter length specifies the length of the returned string.
public void Writeport (String Msg)
This function sends a string to the serial port. Parameter msg is a string that needs to be sent.
public void Closeport ()
This function stops the serial port detection process and closes the serial port.