# define _CRT_SECURE_NO_WARNINGS
//# include & lt; Iostream>
#include
#include
#include
#include
# include "tchar. H"
Void the send ()
{
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * to open the serial port * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
HANDLE hCom.//global variables, a serial port handle
COMMTIMEOUTS TimeOuts.
DCB DCB.
DWORD dwWriteLen=0;
//DWORD dwWriteLen=0;
Int data_in;
Unsigned char sendData publishes the event [1].//write a serial port data cache area
DWORD dwError;
The COMSTAT cs;
//int the error=GetLastError ();
HCom=CreateFile (_T (" COM4 "),
GENERIC_READ | GENERIC_WRITE,//read/write
0,//exclusive way
NULL,
OPEN_EXISTING,
0,//synchronously
//FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERAPPLE,//overlapping way
NULL);
If (hCom==INVALID_HANDLE_VALUE)
{
Printf (" send open failed ");
//printf (" CreateFile () error: % d ", GetLastError ());
}
The else
{
Printf (" send open successed \ n ");
}
/* * * * * * * * * * * * * * to configure a serial port * * * * * * * * * * * */
SetupComm (hCom, 20480, 20480);//buffer
//COMMTIMEOUTS TimeOuts.
TimeOuts. ReadIntervalTimeout=1000;//read timeout
TimeOuts. ReadTotalTimeoutMultiplier=500;
TimeOuts. ReadTotalTimeoutConstant=5000;
TimeOuts. WriteTotalTimeoutMultiplier=500;//write timeout
TimeOuts. WriteTotalTimeoutConstant=2000;
SetCommTimeouts (hCom, & amp; TimeOuts);//set the timeout
//DCB DCB;
GetCommState (hCom, & amp; DCB);
DCB. BaudRate=115200;//115200 baud rate
DCB. ByteSize=8;//8 bit/byte
DCB. Parity=NOPARITY;//and white parity bit
DCB. StopBits=ONESTOPBIT;//a stop bit
DCB. FParity=FALSE;
DCB. FNull=FALSE;
SetCommState (hCom, & amp; DCB);
//PurgeComm (hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);//read/write a serial port to empty buffer before
/* * * * * * * synchronous read and write a serial port of * * * * * * * */
The scanf (" % x ", & amp; Data_in);
Printf (" % d ", & amp; Data_in);
//sendData publishes the event [1]=(data_in & amp; 0 x80) & amp; & 1;//open
SendData publishes the event [0]=data_in;//open
//data_in=data_in & lt; <1;
////////////////see buffer bytes//////////////////
if(! WriteFile (hCom, sendData publishes the event, 1, & amp; DwWriteLen, NULL))
{
Printf (" send failed \ n ");
}
Printf (" send successed \ n ");
Printf (" send data is \ n ");
Printf (" % x \ n ", sendData publishes the event [0]).
//return 1;
if (! The ClearCommError (hCom, & amp; DwError, & amp; Cs))
{
Printf (" ClearCommError () failed \ n ");
}
The else
{
Printf (" ClearCommError () successed \ n ");
Printf (" cs cbOutQue=% d \ n ", cs. CbOutQue);
Printf (" cs cbInQue=% d \ n ", cs. CbInQue);
}
/////////////////////////////////////
;
/* * * * * * * * * * * close a serial port * * * * * * * * * * */
The CloseHandle (hCom);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Int the receive ()
{
HANDLE hCom.
COMMTIMEOUTS TimeOuts.
DCB dcb1;
int i;
DWORD wCount=1;
DWORD wCount1;
DWORD dwError;
The COMSTAT cs;
//DWORD lpErrors;
Unsigned char STR [1].
HCom=CreateFile (_T (" COM4 "),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
If (hCom==INVALID_HANDLE_VALUE)
{
Printf (" read the open failed \ n ");
}
The else
{
Printf (" read the open successed \ n ");
}
SetupComm (hCom, 20480, 20480);//buffer
//COMMTIMEOUTS TimeOuts.
TimeOuts. ReadIntervalTimeout=1000;//read timeout
TimeOuts. ReadTotalTimeoutMultiplier=500;
TimeOuts. ReadTotalTimeoutConstant=5000;
TimeOuts. WriteTotalTimeoutMultiplier=500;//write timeout
TimeOuts. WriteTotalTimeoutConstant=2000;
SetCommTimeouts (hCom, & amp; TimeOuts);
//DCB dcb1;
GetCommState (hCom, & amp; Dcb1);
Dcb1. BaudRate=115200;
Dcb1. ByteSize=8;//a byte 8 bit
Dcb1. Parity=NOPARITY;//no check
Dcb1. StopBits=ONESTOPBIT;//one stop bit
Dcb1. FParity=FALSE;
Dcb1. FNull=FALSE;
SetCommState (hCom, & amp; Dcb1);
//PurgeComm (hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
PurgeComm (hCom, PURGE_TXCLEAR);
////////////////see buffer bytes//////////////////*/
if (! The ClearCommError (hCom, & amp; DwError, & amp; Cs))
{
Printf (" ClearCommError () failed \ n ");
}
The else
{
Printf (" ClearCommError () successed \ n ");
Printf (" cs cbInQue=% d \ n ", cs. CbInQue);
}
/////////////////////////////////////
if(! ReadFile (hCom, STR, wCount, & amp; WCount1, NULL))
{
Printf (" read failed! \n");
}
The else
{
Printf (" read successed! \n");
Printf (" read actual length: % d \ n ", wCount1);
Printf (" the read data is \ n ");
for(i=0; i
Printf (" % 02 x \ n ", STR [I]);
}
}
The CloseHandle (hCom);
system("pause");
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull