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.