Application of "Taobao" com in wince Serial Communication

Source: Internet
Author: User

 

Auto: http://space.baidu.com/lewutian/blog/item/83afc03d445ae7e53c6d974b.html

 

Design and Implementation of controls

Generally, the handheld computer is equipped with only Serial Port 1, while Serial Port 2 requires an external adapter. This article is mainly for long white company production of blue sky LT-90-1 models provide auxiliary functions, so the control default Use Serial 1. Because the resources of the handheld computer are relatively small, in order to reduce CPU usage as much as possible, this control uses the method of sampling the serial port at a certain interval, A time-limited method is adopted for each read/write of the serial port to ensure that the control can end the call in a short period of time, and the call efficiency is achieved by sacrificing a certain success rate, avoid waiting for the serial port when there is no time limit. Controls are designed:

Purpose: To enable, disable, and read/write the serial communication.

The main part of the control design is the interaction process with the user, that is, the functions provided by the control to the user and the corresponding processing by notifying the user through events. The following are the events and methods implemented by the control.

Control-triggered events: During serial communication, the control notifies the user to handle the following two events.

Newdata: When this event is triggered, it indicates that new data is received at the serial port. In this case, the program responds to the event and collects data in time.

Writeerror: When this event is triggered, the write operation on the serial port fails, and the program should handle the error.

Control implementation method: the control provides four methods for the user to call. After obtaining the control pointer, the user can use these four functions to open, close, and read/write the serial port. Open: No parameter. In the first use, you should call this function to initialize the serial port. The default serial port parameter is 9600 baud rate, 8 characters, one-bit calibration bit, and parity. Returns true if Initialization is successful, and false if initialization fails. Read: The readbuffer and readsize parameters. readbuffer is a buffer for reading characters. In the response function of the newdata event, read is used to read the characters entered from the serial port. If the call succeeds, the function returns true, readsize returns the number of bytes read, and readbuffer returns the characters read. If the call fails, the function returns false. Write: The wtitebuffer and writesize parameters. writebuffer is the character to be written to the serial port. This function can be called at any time. If the function returns false, the input parameter is incorrect. If the function returns true, the input parameter is correct. If an error occurs during transmission, the control will trigger the writeerror event, notifying the application that the write operation failed and the application will handle it accordingly. Close: No parameter. After the serial port is used, call this function to release the serial port resources opened by the control, so that other programs can continue to use this resource.

Basic serial communication programming serial devices are considered as conventional and installable stream devices used to open, close, read, and write serial ports. Win32 API provides a set of communication functions. Windows CE supports most of them.

Enable and disable the serial port: You can use createfile to enable the serial port device on all stream devices. The general call method is as follows:

Hser = createfile (text ("COM1:"), generic_read | generic_write, 0, null, open_existing, 0, null );

Because Windows CE does not support overlapping I/O of devices, the file_flag_overlapped flag cannot be passed. If the call fails, invalild_handle_value is returned. Otherwise, the open serial port handle is returned.

You can call closehandle to close a serial port:

Closehandle (hser );

Read/write serial port: You can use readfile and writefile to read and write serial ports. To read data from a serial port, you only need to call the following:

Int RC;

DWORD cbytes;

Byte ch;

Rc = readfile (hser, & Ch, 1, & cbytes, null );

If the call succeeds, the variable ch will be read into one byte, and cbytes will be set to the number of read bytes.

To write data from a serial port, you only need to call the following:

Int RC;

DWORD cbytes;

Byte ch;

Ch = text ('A ');

Rc = writefile (hser, & Ch, 1, & cbytes, null );

The code above writes the letter A to an opened port. If it succeeds, both readfile and writefile will return true.

The debugging of the control varies with the development platform of the control. After the program is compiled, Visual C ++ uses the synchronization server to transmit the program to the handheld computer. At this time, Serial Port 1 is used between the handheld computer and the host. If the synchronization server is not disabled, then, when you use the control on your handheld computer to initialize Serial 1, it will fail. Therefore, the connection to the host must be closed after the program is transmitted to the handheld computer, and the synchronization server must be disabled on the host. Otherwise, the server will continuously send synchronous signals to the handheld computer. Because the serial port is disabled, you cannot use the debugging function provided by Visual C ++. You can only output debugging information in the program and correct the program. The basic debugging method is to use the afxmessagebox function to output the variable content where the program needs to view the variable, or add a text editing control in the program dialog box to output the program information.

After implementing a WindowsCE control in the desktop version of the control, developers will face a problem. When you want to use the control in your application, you cannot find the control you designed in the desktop registry, so it cannot be used in Visual C ++ or Visual Basic. This is because the CE version of the Active X control is registered in the corresponding WindowsCE device registry, rather than registered on the desktop.

To solve this problem, you must implement two ce controls. Version 1 is registered on your WindowsCE device, and Version 2 is registered in the registry of the desktop that you develop applications. The second version of the control implements the shell of the control on WindowsCE, that is, it provides all the methods and events of the control, but there is no specific function implementation. It is only used to provide all connections from applications to controls.

3. Conclusion

This article discusses the development of a COM application based on the Windows CE platform, and puts forward the implementation of a serial communication control as an example. This control can be embedded in most Windows CE programs to complete the basic tasks of current serial communication.

At present, COM is still in the stage of continuous development, and new technologies are constantly emerging. The Distributed Component Object Model (DCOM for short) extends the location transparency of COM to different computers. COM + extends the intermediate components to support hundreds of concurrent accesses. The COM + component service provides standard service implementation for services that are frequently required by component developers. This frees developers and enables them to focus on business processing. Microsoft's Windows DNA (Windows distributed Internet applicationarchitecture) is another mode for developing Windows platform applications based on COM. It makes development of clients and servers more specific and simplified. These new technologies will inevitably bring greater vitality to the application development of the Windows CE platform. We believe that Windows CE will be brighter tomorrow.

 

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.