Home > Back-end >  BCB under Windows 7 with API 6 a serial port to send and receive
BCB under Windows 7 with API 6 a serial port to send and receive

Time:11-14

Code is online, is as follows:

1. Define variables

Bool ComState=FALSE;//a serial port state
HANDLE hCom=0;//serial port open return value
DCB DCB.
OVERLAPPED OverRead, OverWrite;
The COMSTAT COMSTAT;
OVERLAPPED OS;
DWORD dwEvtMask=0;
String dat.
HANDLE m_pThread;

COMMTIMEOUTS ComTimeouts;

Char ReadBuff [1024].//read buffer
DWORD ReadCount;//read bytes

2. A serial port initialization

Void __fastcall TForm1: : btn1ClickSerialPortInit (TObject * Sender)
{

If (FALSE==ComState)
{
HCom=CreateFile (COM3,//filename
GENERIC_READ | GENERIC_WRITE,//access mode allows read/write
0,//the must be 0
NULL,//no security parameters
OPEN_EXISTING,//
create way////FILE_FLAG_OVERLAPPED. Asynchronous works
0,
NULL);
If (hCom==INVALID_HANDLE_VALUE)
{
ShowMessage (" Can not open the port!" );
The CloseHandle (hCom);
HCom=0;

}

if(! GetCommState (hCom, & amp; DCB))//serial port Settings and use it fill the DCB structure
{
ShowMessage (" GetCommState failed ");
}

if (! SetupComm (hCom, 1024102, 4))//set the input and output buffer size
{
ShowMessage (" SetupComm failed ");
}

//set to receive a timeout limit

ComTimeouts. ReadIntervalTimeout=MAXDWORD;
ComTimeouts. ReadTotalTimeoutMultiplier=MAXDWORD;
ComTimeouts. ReadTotalTimeoutConstant=1000;
SetCommTimeouts (hCom, & amp; ComTimeouts);


//set the DCB structure member variable
DCB. BaudRate=115200;
DCB. FParity=0;
DCB. Parity=NOPARITY;
DCB. StopBits=ONESTOPBIT;
DCB. ByteSize=8;
DCB. FNull=FALSE;

if(! SetCommState (hCom, & amp; DCB))//reconfiguration of a serial port
{
ShowMessage (" SetCommState failed ");
}

//to empty the serial buffer, exit all related operations
PurgeComm (hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
Form1 - & gt; Caption=comname + communication success! "" ;
ComState=TRUE;
Btn1 - & gt; SetTextBuf (" close serial port ");
}
The else
{
The CloseHandle (hCom);

ComState=FALSE;
Btn1 - & gt; SetTextBuf (" open the serial port ");
}


}

2. Send function

Void __fastcall TForm1: : btn2ClickSendData (TObject * Sender)
{
//send data
BOOL WriteState;
Unsigned long Written;
DWORD dwError;

Int Size=EditSndData - & gt; GetTextLen ();//Get the length of the string in Edit1
Char * p=new char [Size];//Creates Buffer dynamic variable
//allocate memory, there must be, otherwise the program will appear error
EditSndData - & gt; GetTextBuf (p, Size);

WriteState=WriteFile (hCom,//CreateFile file handle
P,//output buffer first address
The Size,//request output bytes
& Written,//actual output bytes,
& OverWrite);//overlapping data structure, operating mode

If (WriteState & amp; & GetLastError ()==ERROR_IO_PENDING)
{
ShowMessage (" Error!!!!!! ");
}
}

3. Receive data (timing query receiving)

Void __fastcall TForm1: : tmr1TimerReceiveData (TObject * Sender)
{
//TODO: Add your source code here

DWORD nBytesRead dwEvent, dwError;
The COMSTAT cs;//used to store a serial port state

If (hCom==INVALID_HANDLE_VALUE) return;
The ClearCommError (hCom, & amp; DwError, & amp; Cs);
If (cs. CbInQue> Sizeof (ReadBuff))
{
PurgeComm (hCom, PURGE_RXCLEAR);
return;
}//data more than the buffer? : invalid data received to remove
ReadFile (hCom, ReadBuff, cs cbInQue, & amp; NBytesRead,
NULL);//receives the data read
ReadBuff [cs cbInQue]='\ 0';
If (cs) cbInQue & gt; 0)
{
EditRcvData - & gt; Text=ReadBuff;//to display the data
}

}


The above code in bcb6, compile under Windows 7, they found there are three problems

1, the received data frames phenomenon, use serial assistants to receive 76 every time, receive 60 or so, with the software (sometimes is 59, sometimes is 60, sometimes 61)
2, after the initialize serial port, some error in the order after the send button "Can not open the port" "GetCommState failed" "SetupComm failed" "GetCommState failed"
3, the sending code merge into btn1ClickSerialPortInit (TObject * Sender), and is not an error, but also did not send,

Please which friend know what reason be? If anyone has BCB 6 Windows 7 to run a serial port to send and receive procedures based on API, please also send me a reference, [email protected] thank you.

CodePudding user response:

ReadFile (hCom, ReadBuff, cs cbInQue, & amp; NBytesRead,
NULL);//receives the data read
ReadBuff [cs cbInQue]='\ 0';
If (cs) cbInQue & gt; 0)
{
EditRcvData - & gt; Text=ReadBuff;//to display the data
}

This treatment is not reasonable? Null character if they met in the data (space), so just disconnect,

CodePudding user response:

CreateFile pass a pointer to try the first parameter,


 if (hCom==INVALID_HANDLE_VALUE) 
{
ShowMessage (" Can not open the port!" );
//the CloseHandle (hCom);//open the failure, invalid pointer
HCom=0;
return;//open the serial port fails, it shall directly exit, continued also reported more wrong,

}

CodePudding user response:

1, before the call GetCommState, to structure the members of the incoming DCBlength do an initialization, DCB. DCBlength=sizeof (DCB); Such a try,
2, about the phenomenon of frames, you did not use when creating port handle overlapping way,
HCom=CreateFile (COM3,//filename
GENERIC_READ | GENERIC_WRITE,//access mode allows read/write
0,//the must be 0
NULL,//no security parameters
OPEN_EXISTING,//
create way////FILE_FLAG_OVERLAPPED. Asynchronous works
0,
NULL);
But at the time of sending data with the overlapping way,
WriteState=WriteFile (hCom,//CreateFile file handle
P,//output buffer first address
The Size,//request output bytes
& Written,//actual output bytes,
& OverWrite);//overlapping data structure, operating mode
But I don't know have to use such problems as: D, haven't tried,
In addition, char * p=new char [Size]; This data allocated memory didn't release,

In addition, don't know what to send large amount of data is not large, how about frequency, and how much is the cycle in the timer,
If the amount of data is frequency, threads can be used to receive,

The following code to remove see,
nullnullnullnullnullnullnullnull
  • Related