Palm J2ME Serial Communication Program Writing Debug
Author: Lee Luqun Source: CCID Palm is 3Com's products, its PalmOS operating system is a 32-bit embedded operating system. Palm hardware provides a standard serial communication interface (RS232 interface) and an infrared transmission interface. It can be easily communicated with other external devices with other external devices. At present, 3COM has actively cooperated with Sun, which has provided very well-embedded Java (J2ME) and provides CLDC 1.03 and Palm. The Java virtual machine based on the Palmos platform is KVM. This virtual machine has provided a complete SDK program development package read and written in the PALM serial communication port. Using this development kit, it is convenient to develop a serial communication of Palm and peripheral devices. Serial communication has always been one of the processed applications and research contents in computer software. The computer serial communication system is less than 15 meters, and if the photoelectric converter can be extended to several kilometers if the photoelectric converter is used. If the communication between the communication uses MODEM, the telephone line can be used to enable serial communication between the communication between any position. Serial communication is widely used in industrial data acquisition, IC card reading and other fields. Since the writing and debugging steps of the PALM program are different from the desktop PC, some devices with Palm serial communication must get outdoors to use it. For example, GPS receivers, GPS's global positioning system, GPS receiving devices, etc. They must be outdoors, and must receive sufficient satellite at a broader location, and the GPS receiver can solve the current position. Then, the resulting result will be sent out by the NMEA0183 protocol through the NMEA0183 protocol through the NMEA0183 protocol, and achieves positioning. This makes Palm serial communication program debugging very complicated. The author has been engaged in J2ME embedded procedures for 1 year. It has found a way to prepare and debug Palm J2ME embedded programs. The following will be described in detail.
Development steps for Palm serial communication applications
Usually the development of the PALM application is written using the desktop PC, written into a Palm application, and then imports it into the PALM emulator of the desktop PC. Run debugging. After the program debugging is completed, it is imported into the real PALM hardware through the Synchronous Software (HotSync) of the desktop PC, which implements the application on the PALM hardware (see Figure 1).
Figure 1 Palm application development step UML activity diagram
Palm serial communication hardware environment
Since Palm serial communication involves the operation of the hardware, the PALM simulator cannot debug Palm serial communication program, and the method of combining software and hardware must be used. The method is as follows: First mapping the serial communication port of the PALM simulator to the physical serial communication port of the desktop PC. Specific operations For the mouse, right-click the Palm Simulator, select Setting, then select Properties, select the Serial Port, and finally select COM1. Note that this serial communication port device must be used by other devices. The PC physical communication port is then connected to the PALM communication port with the PALM. When the PALM simulator is completed on the same PC and PC serial communication, a NULL MODEM serial communication cable is required, and the relevant serial communication data can be made in detail.
Serial communication auxiliary debugging software
The terminal emulation program is an application software comes with Windows 9x / NT / 2000. It is a powerful serial communication auxiliary debugging software that can easily complete serial communication between the PC computer. We introduce the terminal simulation program, not to simply introduce the software that comes with the Windows system, mainly to debug the serial communication program. Terminal Simulation Program is located in Windows Tools "Start - Program - Accessories - Communication - Hyper Terminals." If you have not installed a super terminal program in the system, select Control Panel - Add Remove Programs, select Windows, Select Communication, Select Super Terminal Resets, follow the prompts to insert the Windows 9x / NT / 2000 system installation CD, complete the terminal emulation program installation. Start the super terminal program HPERTRM.EXE, select the serial communication port (COM1 or COM2), and set the communication rate and check digit. For example, the serial communication rate is 9600 baud, the data bit is 8 bits, no parity, stop bit is 1 bit (see Figure 2, Figure 3). Figure 2 Super Terminal Serial Communication Port Set
Figure 3 Super Terminal Serial Communication Rate Setting
Palm J2ME Serial Communication Program Design
In general, both parties of serial communication must comply with the same communication data format and write their respective communication programs. Due to the writing of the communication program involves the communication, any part of the communication program will lead to the failure of communication, and will bring many difficulties to the writing and debug communication. Through repeated trials, the author uses the Windows terminal simulation communication program to simulate communication between the communication parties, and then debug communication between the other party and the Windows terminal simulates communication. When the communication is successfully debugged with the Windows terminal, you can implement it. Serial communication. This makes it possible to minimize the debugging effort of serial communication programs, and improve program development speed (see Figure 4). Assuming that two serial communication systems are currently designed, the PC display PALM data system and Palm display PC-side data system, and their specific implementations are as follows:
Figure 4 Serial Communication UML Relationship Diagram
PC display PALM data system
Put the Palm's database content via serial communication, according to the serial communication 9600 baud rate, the data bit is 8 bits, no parity, stop bit is transferred to the PC computer, and display. This system involves Palm-end programs and PC computer-end programs. 1. Palm end program (see Figure 5)
Figure 5 Palm end program UML state diagram
The Palm end program is mainly divided into three steps: (1) Open the Palm internal database, and the logo of the database inside the PALM is:
DBTYPE = GPSD, CREATORID = 6657;
Static int dbtype = 0x67707364; // GPSD
Static int dbcreator = 0x36363537; // '6657'
Database DB = New Database (DBTYPE, DBCREATOR, DATABASE.READOONLY);
......
Since the write operation of the database is not involved, this is opened in a read-only manner. (2) Open the Palm serial communication port, construct the SerialPort object, open the serial port using the SerialPort.Open method.
Protocol serialport = new protocol ();
SerialPort.Open
("0; baudrate = 9600; bitsperchar = 8; stopbits = 1; Parity = None;
Autocts = Off; autocts = OFF; Blocking = OFF ", 1, TRUE); ......
(3) Send data, obtain the OutputStream data stream through the OpenOutputStream () method of the SerialPort object, and assign the OS object, the code is OutputStream OS = SerialPort.OpenOutputStream (). Then, read each recording content of the Palm internal database GPSD, transfer the content by segment through serial communication to the PC, and write data into the serial port through Os.Write (). The main code is:
BYTE [] RNL = {13, 10};
For (int i = 0; i { Graphics.getgraphics (). DrawString ("Send Record NO." I, 30, 100); Os.Write (DB.GetRecord (i)); Os.write (RNL); } Os.flush (); ...... If the serial communication data is transmitted, close the serial communication port and the Palm internal database. code show as below: System.out.println ("Send Finished!"); Os.Close (); System.out.println ("Connection Closed."); ...... Compile this program, securely check, encapsulate PRC applications in Palm format, and then transfer them to the Palm Simulator. Mapping the serial communication port of the Palm Simulator into the COM1 communication port of the PC, using the Windows comes with the Terminal Simulation Program Simulation PC to complete serial communication. Start the end of the Windows self-contained, set the port to COM2, set the serial communication rate of 9600 baud, the data bit is 8 bits, no parity, stop bit is 1 bit, then use Null Modem serial The communication cable physically connects the COM1 and COM2 ports of the PC. Run the above Palm program can be found that the Palm Simulator will transfer internal data to the dialog window of the internal data to the Terminal emulation program of Windows. Since this dialog window can only save the content of 500 rows, if it is greater than 500 lines, you can select the "Transfer" menu of the Windows HyperTerminal, select Capture Text, save the dialogue directly to a standard text file. 2. PC side serial program (see Figure 6) Figure 6 PC break program UML state diagram You can view the PC side serial program as an application similar to the super terminal. The work of the PC end string is mainly available, and the serial communication port is opened according to the set transfer rate (which must be consistent with the serial communication parameters of the PALM side), then start the thread, and listen to the serial communication port data. After receiving the data, the data is displayed. The specific program steps are as follows: (1) Install the Sun Java Serial Communication SDK class library, introduce the corresponding classes in the program, the code is import javax.comm. *. Then use the method of circulating enumeration, it is determined that the status of the serial communication port on the PC (here the communication port is COM2), the code is as follows: Static enumeration portlist; Portlist = commportidentifier .getportidentiFiers (); While (portlist.hasmorelements ()) { PortID = (CommPortidentifier) PortList.nexTelement (); IF (portid.getporttype () == comMportidentifier.Port_serial) {if (portid.getname (). Equals ("COM2")) { Reader Reader = new reader (); } ..... (2) Open the serial port, obtain the serial communication data stream, the code is as follows: Public reader () { Try { SerialPort = (SerialPort) Portid.Open ("Li", 2000); } catCH (portinuseexception e) {} Try { InputStream = serialport.getinputstream (); } catch (ioexception e) {} (3) In the SerialPorteventListener event, it is determined whether the serial port has data, if there is, the data is displayed, the code is as follows: Public void serialEvent (serialPortevent Event) { Switch (Event.geteventType ()) { Case SerialPortevent.bi: Case SerialPortevent.oe: Case SerialPortevent.fe: Case SerialPortevent.pe: Case SerialPortevent.cd: Case SerialPortevent.ct.ct.ct.CTS: Case SerialPortevent.dsr: Case SerialPortevent.ri: Case SerialPortevent. Output_buffer_empty: Break; Case SerialPortevent.Data_available: Byte [] readbuffer = new byte [200]; Try { While (InputStream.availa ()> 0) { Int numBytes = InputStream.read (Readbuffer); } System.out.print (New String (Readbuffer); } catch (ioexception e) {} Break; ..... In order to debug the serial communication program of the microcomputer, we use the Windows self-contained terminal emulation program debugging. This time, we use the Windows Terminal Simulation to simulate the Palm serial communication terminal. Set the terminal simulation program Use the port to COM1, set the serial communication rate of 9600 baud, the data bit is 8 bits, no parity, stop bit is 1 bit, then use the Null Modem serial communication cable to put the PC computer COM1 Physical connection with the COM2 port. Select Windows Terminal Simulation Menu "Transfer Send Text File" to send analog data text file. Run the above PC program, start the Windows Terminal Simulation program, can be found that the PC can receive data from serial communication reception. PALM Displays a PC-side data system Serial communication is often both sides, in system 1, we introduce the PALM to send data to the PC computer through serial communication. This time, we will introduce the PC computer to send data from the PALM through serial communication. Suppose the system 2 content is to put the PC file E: /GPS.TXT, according to the serial communication 9600 baud rate, the data bit is 8 bits, no parity, stop bit, and transfer content to Palm by serial communication And display data. Thus, system 2 also involves Palm end programs and PC computer end programs. The reader can refer to the above Windows terminal simulation program, debug PALM and PC and PC. The serial communication program on both ends of Palm and PC is described below. 1. Palm end programs (1) Open the serial port, the code is as follows: ..... Protocol serialport = new protocol (); SerialPort.Open ("0; baudrate = 9600; bitsperchar = 8; stopbits = 1; parity = none; Autocts = OFF; Autocts = OFF; Blocking = OFF ", 1, TRUE); System.out.println ("... OPENED"); .... (2) After obtaining the data input stream via the SerialPort, read the data according to the set read data buffer size, the code is as follows: InputStream IS = SerialPort.openputStream (); System.out.println ("Reading Response ..."); ... Int len = is.read (readbuffer); System.out.println ("Read:" Len "Bytes"); (3) Convert the read data into a string, then display, the code is as follows: String s = new string (readbuffer, 0, len); System.out.println ("[" S "]"); .... (4) After the communication is completed, turn off the stream and serial port, the code is as follows: Is.close (); SerialPort.Close (); System.out.println ("Connection Closed."); } catch (exception ie) { System.out.println ("IO EX:" IOE.TOString ()); } ..... 2. PC Main Communication Program (1) Open the serial communication port, the code is as follows: ... Try { SerialPort = (SerialPort) Portid.open ("SimpleWriteApp", 2000); } catch (portinuseexception e) {system.out.println (e); .... (2) Get serial communication port data stream, the code is as follows: Try { OutputStream = serialport.getOutputStream (); } catch (ioException e) {system.out.println (e); (3) Open the gps.txt text file, read each line in the text file, write the data to the output stream acquired by serial communication, the code is as follows: Try { //outputstream.write (message ()); RandomaccessFile Rf = New RandomaccessFile ("E: //gps.txt", "rw"); STRING SS; While (ss = rf.readline ())! = null) { OutputStream.write (ss.getbytes ()); System.out.println (SS); } .... } (4) After the communication is completed, the data stream is turned off, and the code is: Rf.close (); ..... In general, the writing and commissioning of J2ME serial communication procedures requires many techniques, reasonable application of Windows emulation terminals to improve writing and debugging PALM serial communication program development speed, is a relatively scientific serial communication debugging method.