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.