Using Java to realize serial port full duplex communication

Source: Internet
Author: User

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.

Related Article

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.