Home > Software engineering >  Windows API serial programming
Windows API serial programming

Time:10-10

Refer to as bosses, using Windows API to write a serial program, can only send a scale of 0-127 to correct, greater than 128 system will remove the 8 bit, such as sending 0 x26 is normal, but I send 0 xa6 (greater than 127), received or 0 x26, consult everybody warrior, where a wrong ah, example is written according to the serial port communication programming practice book,
Be very grateful!!!!!
The code is as follows:
BOOL bErrorFlag;
BYTE TX_DataBuffer [100].
DWORD dwBytesToTX;

BYTE RX_DataBuffer [101].
DWORD nLenOut;

DWORD dwBytesWritten=0;


The static const int g_nZhenMax=500;

//set the serial port configuration information
DCB DCB.
COMMTIMEOUTS timeouts.


HANDLE g_hCom=CreateFile (" COM3, "GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0).

If (g_hCom==INVALID_HANDLE_VALUE)
{
Printf (" ERR! \ r \ n \ r \ n ");

Goto the end;
}


//set the serial port
DCB. DCBlength=sizeof (DCB);
DCB. BaudRate=9600;//baud rate to 9600
DCB. Parity=0;//check way to check
DCB. ByteSize=8;//data bits for eight
DCB. StopBits=ONESTOPBIT;//stop bit is one
DCB. FBinary=TRUE;



if (! GetCommState (g_hCom, & amp; DCB))
{
Printf (" Get COM State Error!!!!!! \ r \ n \ r \ n ");
Goto the end;
}

if (! SetCommState (g_hCom, & amp; DCB))
{
Printf (" Set COM Error!!!!!! \ r \ n \ r \ n ");
Goto the end;
}


//set read timeout
GetCommTimeouts (g_hCom, & amp; Timeouts);
Timeouts. ReadIntervalTimeout=0;
Timeouts. ReadTotalTimeoutMultiplier=0;
Timeouts. ReadTotalTimeoutConstant=1000;
Timeouts. WriteTotalTimeoutMultiplier=0;
Timeouts. WriteTotalTimeoutConstant=0;
SetCommTimeouts (g_hCom, & amp; Timeouts);

//set the read/write buffer size

if (! SetupComm (g_hCom g_nZhenMax, g_nZhenMax))
{
Printf (" Setup COM Buffer Error!!!!!! \ r \ n \ r \ n ");
Goto the end;
}

//to empty buffer
PurgeComm (g_hCom, PURGE_RXCLEAR | PURGE_TXCLEAR);

//remove error
DWORD dwError;
The COMSTAT cs;
if (! ClearCommError (g_hCom, & amp; DwError, & amp; Cs))
{
Printf (" the Clear COM Error!!!!!! \ r \ n \ r \ n ");

Goto the end;
}


TX_DataBuffer [0]=0 xa6;

BErrorFlag=FALSE;
DwBytesToTX=5;//(DWORD) strlen (TX_DataBuffer);
DwBytesWritten=0;
BErrorFlag=WriteFile (
G_hCom,//the open file handle
TX_DataBuffer,//the start of the data to write
DwBytesToTX,//the number of bytes to write
& DwBytesWritten,//the number of bytes that were written
NULL);//no overlapped structure

If (FALSE==bErrorFlag)
{
Printf (" send data!!!!!! \ r \ n \ r \ n ");
return;
}
The else
{
If (dwBytesWritten!=dwBytesToTX)
{
Printf (" written bytes is not right!!!!!! \ r \ n \ r \ n ");
return;
}
}


//read serial data
Memset (RX_DataBuffer, 0, 101);
NLenOut=0;

If (ReadFile (g_hCom RX_DataBuffer, 100, & amp; NLenOut, NULL))
{
If (nLenOut)//success
{

Printf (" a Received data: % 2 x \ r \ n \ r \ n ", RX_DataBuffer [0]).
}
The else//timeout
{
Printf (" time out!!!!!! \ r \ n \ r \ n ");
return;
}
}
The else
{
//failure
Printf (" Read Error!!!!!! \ r \ n \ r \ n ");
return;
}

CodePudding user response:

Use your program to send serial assistants receive to see if normal?
Use serial assistants send target program receive to see if normal?

CodePudding user response:

I am the TX and RX received a piece of short, using serial assistants send is normal, in my own hair, receives is not normal

CodePudding user response:

I wonder which parameter is not set, can only send a 0-127, cannot send between 128-255, online search along while also didn't find the related situation

CodePudding user response:

reference qdleeming reply: 3/f
I wonder which parameter is not set, can only send a 0-127, cannot send between 128-255, online search along while also didn't find the related situation

Will all char to unsigned char try again?

CodePudding user response:

reference 4 floor zhao4zhong1 response:
Quote: reference qdleeming reply: 3/f

I wonder which parameter is not set, can only send a 0-127, cannot send between 128-255, online search along while also didn't find the related situation

Will all char to unsigned char try again?


Find the problem last night, a serial port parameters should be set on reading after ~
Don't give or thank you very much ~
  • Related