GCF commconnection connector implements code analysis

Source: Internet
Author: User

GCF provides a convenient architecture. Generally, the following steps are used to operate the serial port:

1. Open the serial port: commconnection cc = (commconnection) connector. Open ("COMM: com3: baudrate = ......");

2. Open the input stream: inputstream is = cc. openinputstream ()

3. Open the output stream: outputstream OS = cc. openoutputstream ();

4. Write Data: OS. Write ()

5. Read data: Is. Read ()

 

How are they implemented?

First, let's look at the implementation of ctor in cldc1.1:

Package javax. microedition. io; <br/> Public static connection open (string name); <br/> Public static connection open (string name, int mode ); <br/> Public static connection open (string name, int mode, Boolean Timeouts); <br/> Private Static connection openprim (string name, int mode, Boolean timeouts, string platform) {<br/> protocol = Name. substring (0, colon); extract the protocol before name, such as Comm <br/> class clazz = Class. forname (classroot + ". "+ Platform + ". "+ protocol + ". protocol "); <br/> the com. sun. MIDP. io. j2s. comm. the Protocol Class generates an instance of this class <br/> connectionbaseinterface UC = (connectionbaseinterface) clazz. newinstance (); <br/> UC. openprim (name, mode, Timeouts); <br/> both connection and connectionbaseinterface are interfaces. The specific implementation is in the Protocol class <br/>}< br/> 

Connectionbaseinterface is the interface defined in cldc, in COM. Sun. cldc. IO;

The Protocol class implements the commconnection interface and extends the bufferedconnectionadapter class;

The com. Sun. MIDP. Io. bufferedconnectionadapter class extends the com. Sun. MIDP. Io. connectionbaseadapter class, which implements the connectionbaseinterface interface.

The openprim method in connectionbaseadapter calls the commconnection. Connect Method.

Since connectionbaseadapter implements the streamconnection interface, the openinputstream and openoutputstream methods are implemented. The connectionbaseadapter class defines two private classes: baseinputstream and baseoutputstream. The objects of these two private classes correspond to the objects returned by open. This object is implemented by calling read or write, the differentiated part is implemented in the Protocol class.

 

This architecture perfectly embodies the object-oriented thinking.

 

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.