Asynchronous Implementation of serial port operations: Follow the midp2.0 button event handling method

Source: Internet
Author: User

Based on the protocol encapsulation of the serial port through commconnection, the mechanism of synchronization is adopted. The general process is as follows:

Sending commands --- reading and responding to data cyclically; this operation is very simple. When there are a large number of interactions or the state machine is used, the Code is a large loop and a small loop, and the structure is not good, I want to encapsulate the serial port to implement an Asynchronous Method. For upper-layer applications, the requirements are as follows: 1. start; start the serial port; 2. send: send command; 3. if the transport layer protocol is available, enable the timer. If no data is received after a certain period of time, it is deemed as abnormal. 4. after receiving data, the callback function is called to read data. 5. close; close the serial port; In midp2.0, the MIDP button event source and the underlying operating system use the queue as the interface. MIDP extracts the event from the queue and responds after processing. Finally, it calls back the keypressed of the canvas; with reference to this structure, I implemented the Serial Asynchronous Method: the code is as follows: Class commhandler implements runnable {<br/> Public synchronized void run () {<br/> while (m_bdefaultcommhandler) {<br/> try {<br/> int ilen = s_dis.available (); <br/> If (ilen> 0) {<br/> byte [] bdata = new byte [ilen]; <br/> int iret = s_dis.read (bdata); <br/> If (iret> 0) {<br/> for (INT I = 0; I <iret; I ++) {<br/> If (s_commqueue.isfull ()) {<br/> try {<br/> wait (); <br/>} catch (interruptedexception IE) {<br/> IE. printstacktrace (); <br/> break; <br/>}< br/>} else {<br/> s_commqueue.push (bdata [I]); <br/>}< br/>} catch (ioexception IOE) {<br/> IOE. printstacktrace (); <br/> break; <br/>}< br/> try {<br/> wait (); <br/>} catch (interruptedexception IE) {<br/> IE. printstacktrace (); <br/> break; <br/>}</P> <p> Public synchronized void proceed () {<br/> try {<br/> Policy (); <br/>} catch (throwable t) {<br/> T. printstacktrace (); <br/>}</P> <p> Public synchronized void send (byte [] bdata) throws ioexception {<br/> s_dos.write (bdata); <br/>}< br/>}The role of the private class commhandler is to take data from the serial port and press it into the queue. commhandler also provides two methods: the proceed method is used to control the thread to continue retrieving data from the serial port; the send method is used to send data through the serial port; commdataqueue is a private class that implements the function of cyclic queue; Class commdataqueue {<br/> int m_ifront = 0; <br/> int m_irear = 0; <br/> int queue_len = 1000; <br/> byte [] m_bqueue = new byte [queue_len]; </P> <p> Public synchronized void push (byte bdata) {<br/> // If (isfull () {<br/> // try {<br/> // wait (); <br/> //} catch (interruptedexception IE) {<br/> // IE. printstacktrace (); <br/> //} </P> <p> m_irear = (m_irear + 1) % queue_len; <br/> m_bqueue [m_irear] = bdata; <br/>}</P> <p> Public synchronized byte get () {<br/> If (isempty ()) {<br/> return (byte) 0xff; <br/>}</P> <p> m_ifront = (m_ifront + 1) % queue_len; <br/> return m_bqueue [m_ifront]; <br/>}</P> <p> Public synchronized int available () {<br/> return (m_iRear-m_iFront + queue_len) % queue_len; <br/>}</P> <p> Public Boolean isfull () {<br/> return (m_irear + 1) % queue_len = m_ifront; <br/>}</P> <p> Public Boolean isempty () {<br/> return m_irear = m_ifront; <br/>}< br/>}Queueeventhandler is a private class that calls back when there is data in the queue. When there is no data in the queue, commhandler is triggered to read data from the serial port; m_cb is an interface, definition: Class queueeventhandler implements runnable {<br/> Public synchronized void run () {<br/> while (m_bdefaultcommhandler) {<br/> If (s_commqueue.isempty ()) {<br/> s_commhandler.proceed (); <br/>} else {<br/> int ilen = s_commqueue.available (); <br/> byte [] bdata = new byte [ilen]; <br/> for (INT I = 0; I <ilen; I ++) {<br/> bdata [I] = s_commqueue.get (); <br/>}< br/> m_cb.datacomming (bdata); <br/> // s_commhandler.proceed (); <br/>}< br/>}  Private defaultcommhandlercb m_cb; </P> <p> Public interface defaultcommhandlercb {<br/> Public void datacomming (byte [] bdata); <br/>}< br/>The commhandler private class, queueeventhandler private class, And commdataqueue private class constitute a class defaultcommhandler. The usage of the upper layer is as follows: Class closet extends thread {<br/> Public void run () {<br/> defaultcommhandler comm = defaultcommhandler. getdefacommcommhandler (); <br/> try {<br/> comm. close (); <br/>}catch (ioexception IOE) {<br/> m_frm.append ("Close ioexception:" + IOE. getmessage ()); <br/>}</P> <p> class sendt extends thread {<br/> Public void run () {<br/> defaultcommhandler comm = defaultcommhandler. getdefacommcommhandler (); <br/> try {<br/> comm. send (m_tfsend.getstring (). getbytes (); <br/>} catch (ioexception IOE) {<br/> m_frm.append ("Send ioexception:" + IOE. getmessage ()); <br/>}< br/> class inquery implements defaultcommhandlercb {<br/> Public void datacomming (byte [] bdata) {<br/> m_tfrecv.setstring (new string (bdata, 0, bdata. length); <br/>}< br/>}After the program is tested, the effect is good.

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.