Wince Serial Port Development

Source: Internet
Author: User
Tags readfile

After the ftdi chip can work, it starts the serial port test. We use the ft2232d chip board to expand two standard RS232 serial ports from a USB port. The test goal is to allow both serial ports to send and receive data at the same time.

Of course, software programming is used for testing. There are a large number of serial port test programs on the PC end. Therefore, the main purpose of this test program is to enable the serial port to receive and send files. (There are also a lot of serial port test programs on the CE end, but few programs with the file transfer function are found, so I had to do it myself)

1. Open the serial port:

Hadnle hcomm = createfile (_ T ("COM1:"), generic_read | generic_write, 0, null,

Open_existing, 0, 0 );

Closehandle (hcomm) // close the serial port

2. Configure the serial port:

DCB;

Getcommstate (hcomm, & DCB)

Setcommstate (hcomm, & DCB)

I only modified some parameters, and others are the default settings.

DCB. baudrate = cbr_115200;
DCB. bytesize = 8;
DCB. fbinary = true;
DCB. fparity = true;
DCB. Parity = noparity;
DCB. stopbits = onestopbit;

DCB. fdtrcontrol = dtr_control_enable; // It is related to the hardware. What is the role of DCB?
DCB. frtscontrol = rts_control_enable; // same as above

3. Configuration Timeout:

Commtimeouts timeouts;

Setcommtimeouts (hcomm, & Timeouts)

There is no deep understanding of timeout, but the corresponding time is configured according to understanding.

Timeouts. readintervaltimeout = 100;
Timeouts. readtotaltimeoutconstant = 100;
Timeouts. readtotaltimeoutmultiplier = 10;
Timeouts. writetotaltimeoutconstant = 0;
Timeouts. writetotaltimeoutmultiplier = 0;

There are two calculation methods for read timeout. One is readintervaltimeout, which specifies the maximum interval between received characters, and the other is based on

Number of characters to be read, readtotaltimeoutconstant + readtotaltimeoutmultiplier. When two

When the time is set, it is valid at the same time and affects readfile (...) to return.

Because what we need to do is to read the file, all according to understanding, set the maximum interval between characters to 100 ms, this is used to receive the file name and text

Part Size. Control sending in the thread that sends the file. After reading the file name, obtain the file size and combine the file name and file size to make

Use writefile (...) to send data to the serial port. Then use sleep (200) to "Suspend" the sending thread for a period of time.

When a serial port event occurs, you can receive the file name and file size. After a timeout of MS, readfile (...) is returned to prevent receiving

Data.

For readtotaltimeoutconstant and readtotaltimeoutmultiplier, It is set by feeling, nothing too

I thought too much.

4. send and receive data:

Writefile (...)

Readfile (...)
Setcommmask (hcomm, ev_rxchar );

Clearcommerror (hcomm, & dwreaderror, & cmstate );

Waitcommevent (hcomm, & evtmask, null)

It seems that the sending thread is better at processing. Use cfiledialog to obtain the selected file and use createfile to open the file.

Getfilesize: get the file size. The cstring: Format method is used to easily convert data into strings.

Combination of file names and file sizes.

M_strfilename = DLG. getfilename ();
M_strpathname = DLG. getpathname ();

Hfile = createfile (m_strpathname, generic_read, 0, null, open_existing, 0, null );

Strfile. Format (_ T ("% lu"), getfilesize (hfile, null ));

Strfile = m_strfilename + _ T ('//') + strfile;

If (writefile (m_hcomm, strfile. getbuffer (), strfile. getlength () * sizeof (tchar ),
& Dwwritelen, null ))
{
Sleep (200 );
}

While... // Send file data

It is troublesome to receive the thread. Use setcommmask () to set serial events, and then wait for the event through waitcommevent ().

Occurred. (It is not clear how to set the appropriate combination of events, all events that only wait for receiving characters) because the file name and the number of files to be received

So it is set to receive the file name outside the while loop, and create the file, and then the while loop receives the data.

If (waitcommevent (hcomm, & evtmask, null ))
{
Setcommmask (hcomm, ev_rxchar );

If (evtmask & ev_rxchar)
{

Clearcommerror (hcomm, & dwreaderror, & cmstate );
Dwwillreadlen = cmstate. cbinque;

Cstring STR (_ T ('/0'), max_path );
If (readfile (hcomm, str. getbuffer (), dwwillreadlen, & dwreadlen, 0 ))
{

Str. releasebuffer ();

... // Process string

Hfile = createfile (...);

}

While... // Receives file data

}

}

This completes the programming of the serial port, which is relatively simple. After testing, the file can be sent from the PC to the handheld device, and the handheld device can receive the complete file. The two serial ports can work together, and the size of the file to be sent and received is 1 MB. At that time, some colleagues lamented how ftdi simultaneously sent data to a USB port through two serial ports.

Although there are still many problems with the test program, the Program on the handheld device can only receive files once. To receive files for the second time, you must reconnect to the serial port, the reason is that the receiving thread will exit after receiving the file. This problem should be solved through multi-thread synchronization object. There is also a memory leakage problem... the cause of the problem is unknown (not found ). In addition, in some cases, the program will strike, the handheld device cannot receive files, and the PC side can send normally...

When I first developed a serial program, I still have some questions:

1. Configure the serial port. Many parameters do not know how to configure or use them.

2. The configuration times out and is very confused. I do not know how to configure it with the program.

3. There are many serial port events that do not know how to use them, or when to use them.

4. You are not familiar with some APIs,

Clearcommerror, escapecommfunction, getcommmodemstatus,

Getcomatrix roperties, purgecomm, setupcomm, transmitcommchar

All of the above are personal understandings. Due to the limited level, there will inevitably be gaps and deficiencies. Please refer to Hai Han, the author of this article. In addition, it indicates that all technical articles in this blog are original, and the writing style is similar to work notes. In line with the principle of respecting individual labor achievements, if you need to repost or reference them, add the original link or indicate the source.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/embest_mhq/archive/2009/03/11/3980751.aspx

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.