Several serial communication programming under WIN32

Source: Internet
Author: User
Tags bool readfile win32

To complete the serial communication, in 32-bit mode, in general there are the following four ways:

1. Open serial port in file mode:

Here is the use of Win32 API functions, so either in BCB or VC can be implemented. The meaning of the specific function can be referenced by the Win32 API. Here is an easy-to-use BCB class that can be easily used in a serial port.

A brief description is given below:

(1) Application of CreateFile () to initialize the serial port

Fhfilecomm=createfile (
Fcommname.c_str (),
Generic_read | Generic_write,
0,//exclusive access
&lpsecurity,
Open_existing,
File_attribute_normal | file_flag_overlapped,//overlapped I/O
NULL);
if (Fhfilecomm = = INVALID_HANDLE_VALUE)
return false;

(2) using WriteFile () and ReadFile () to read and write data from the serial port


BOOL Mycomm::writebuffer (unsigned char *buffer,unsigned long length)
{
return WriteFile (fhfilecomm,buffer,length,&length,&fgoverlapped);
}
BOOL Mycomm::readbuffer (unsigned char *buffer,unsigned long length)
{
Return ReadFile (fhfilecomm,buffer,length,&length,&fgoverlapped);
}

The corresponding source file is UsageComm.cpp UsageComm.h

2. Use ready-made controls:

Easy to use is Microsoft's Ms Communication control, which is provided as an. ocx.

You can add it to the BCB with import ActiveX control in BCB, which is loaded in an ActiveX page by default. It can be used as a normal BCB. (There are also many other hardware manipulation controls provided by third parties)

3. Direct embedding of the Assembly law:

(This method can not be used in NT, because 98 of the incomplete protection, the following code can be used) BCB directly embedded assembly function, you can in the BCB directly to the serial operation.

As shown below, the assembly code is used in BCB as follows

byte __fastcall readbyte (WORD address)
Eax:address
{
Asm
{
MOV Dx,ax
In AL,DX
}
}
void __fastcall writebyte (unsigned char byte, WORD address)
Eax:byte edx:address
{
Asm
{
Out Dx,al
}
}

Here you must use the __fastcall calling convention to pass arguments with registers when the function is called.

(The assembly instruction code in and out used in BCB must be lowercase, (the rest can be uppercase) register name can be cased)

Other such as writing multiple bytes, read multiple bytes, can also be achieved.

4. The most difficult to use a VxD to achieve:

This is not a sudden can be said clearly, if you want to do, of course, to find a book.

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.