Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Overlapped serial comunication

Status
Not open for further replies.

Projetos_afg

Junior Member level 3
Joined
Feb 3, 2010
Messages
28
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,293
Location
São Paulo, Brazil.
Activity points
1,428
Hi,

I'm have been trying to write an code in C++ by using the Win32 API to communicate through the RS232 serial port with equipment. It's a simplex communication (only the equipment sends data in blocks of 289 bytes). I'm using an dedicated thread to read the serial port. Firstly I tried to use non-overlapped communication, but I realized that is impossible to close the serial port if there's no data incoming (the thread gets stuck on WaitCommEvent function). So I tried to use overlapped I/O and WaitForMultipleObjects to solve this problem:

Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void *SerialRead(void *param)  // param is a dummy pointer
{
    OVERLAPPED overlapped;
    
    memset( &overlapped, 0, sizeof(overlapped) );
    overlapped.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
 
    SerialEvents[0] = overlapped.hEvent;
    
    ReadFile(SerialPort, SerialBuffer, 289, &dwBytesRead, &overlapped);//Try to read 289 bytes
    
    
    while (WaitForMultipleObjects(2, SerialEvents, false, INFINITE) == WAIT_OBJECT_0) //Wait for some event, if is an serial port event, executes de loop, if not, exit.
    {
 
        //...
        //Do stuff
        ReadFile(SerialPort, SerialBuffer, 289, &dwBytesRead, &overlapped); //Attempt to read the next 289 bytes
    }
 
    ClosePort();
    return 0;
}


I use the function SetEvent(SerialEvents[1]); to close serial port on the main thread.

To open the serial port:


Code C++ - [expand]
1
2
3
4
5
6
7
8
SerialPort = CreateFile(PortName,
GENERIC_READ|GENERIC_WRITE,//access ( read and write)
0,    //(share) 0:cannot share the COM port
0,    //security  (None)                
OPEN_EXISTING,// creation : open_existing
FILE_FLAG_OVERLAPPED,    // overlapped I/O
0// no templates file for COM port...
);



The problem is that even if I recive the 289 bytes, the thread keeps wating the function "WaitForMultipleObjects" return. If I recive the next 289 bytes the WaitForMultipleObjects function returns, but the data are mixed.

Someone could help me?
 
Last edited by a moderator:

I discovered the problem. It was that on setup when openning the serial port I was calling GetCommState() function before SetCommState(), so it was overwriting the DCB structure configuration, setting a wrong baud rate value. I fix it and now it's working wonderfully!
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top