Home > other >  A serial port to send data error
A serial port to send data error

Time:11-12

//# include "PCH. H"


# define _CRT_SECURE_NO_WARNINGS
//# include & lt; Iostream>
#include
#include
#include
#include
# include "tchar. H"


Void main ()
{
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * to open the serial port * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
HANDLE hCom.//global variables, a serial port handle
COMMTIMEOUTS TimeOuts.
DCB DCB.
DWORD dwWriteLen=0;
int i;

//DWORD dwWriteLen=0;
Int data_in;
Unsigned char sendData publishes the event [8].//write a serial port data cache area



//int the error=GetLastError ();
HCom=CreateFile (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);

for(i=0; i<8; I++)
{
SendData publishes the event [I]=(data_in & amp; 0 x00000080) & amp; & 1;//open
Data_in=data_in & lt; <1;
}
if(! WriteFile (hCom, sendData publishes the event, 1, & amp; DwWriteLen, NULL))
{
Printf (" send failed \ n ");
//GetLastError ();
//return 0;
//printf (" % d ", & amp; The error);

}
The else
{printf (" send successed \ n ");
Printf (" send data is \ n ");
for(i=0; i<8; I++)
{
Printf (" % 01 x ", sendData publishes the event [I]);
}
//return 1;
}
//system (" PAUSE ");
/* * * * * * * * * * * close a serial port * * * * * * * * * * */
The CloseHandle (hCom);

//GetLastError ();
system("pause");


}
With the serial port to monitor the elves found that no matter what to send data, write either 00, either 01
Use serial assistants can send and receive correctly, so the question appeared in my program, don't know where is wrong,
Thank you,

CodePudding user response:

If it is to press each place sends a data with
WriteFile (hCom, sendData publishes the event,/* 1 */8, & amp; DwWriteLen, NULL)




  • Related