Click here to Skip to main content
15,922,155 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionwhere can I download Win2000 DDK free? Pin
white jungle20-Feb-03 17:41
white jungle20-Feb-03 17:41 
GeneralCVS and VS.NET Pin
Nick Blumhardt20-Feb-03 17:36
Nick Blumhardt20-Feb-03 17:36 
GeneralGDI & Windowing System Pin
vikramlinux20-Feb-03 17:25
vikramlinux20-Feb-03 17:25 
GeneralSTL list use of own class Pin
kiken20-Feb-03 16:32
kiken20-Feb-03 16:32 
GeneralRe: STL list use of own class Pin
Paul M Watt20-Feb-03 16:44
mentorPaul M Watt20-Feb-03 16:44 
GeneralRe: STL list use of own class Pin
kiken20-Feb-03 17:11
kiken20-Feb-03 17:11 
GeneralThread Control and WaitCommEvent() Pin
lead2gold20-Feb-03 16:31
lead2gold20-Feb-03 16:31 
GeneralRe: Thread Control and WaitCommEvent() Pin
Roger Allen21-Feb-03 1:58
Roger Allen21-Feb-03 1:58 
I would stay away from WaitCommEvent myself. In the past I have always configured my comm port such that when you do a WriteFile() it returns after all output characters have actually been sent and the receive returns immediately regards on whether any characters were read or not - you can then check the number of characters received. I do this in a loop with regular Sleep()s in it and a while (!bKillYourself) flag which can be set externally.

Somthing like this: (A quick hack of one of my projects)

DWORD                   dwNumBytesRead;
DWORD                   dwNumBytesWritten;
SECURITY_ATTRIBUTES     sa;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;     // use default descriptor
sa.bInheritHandle = true;               // allow sub-threads to inherit handle

// OPEN SERIAL PORT FOR COMMUNICATION
m_hSerialPortHandle = CreateFile((LPCTSTR)m_csCommPortName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    &sa,                            // needed for inherit
    OPEN_EXISTING,
    0,
    0);

if (m_hSerialPortHandle != INVALID_HANDLE_VALUE)
{
    m_bOpen = true;

    if (!GetCommState(m_hSerialPortHandle, &m_DCB))
    {
        text = "Serial port DCB failure.";
        ::SendMessage(m_hwndParent, UWM_UC_STATUS, (LONG)(LPCTSTR)text, 0);
    }
    else
    {
        // make sure we are using the correct settings
        m_DCB.BaudRate = CBR_9600;                  // 9600 baud
        m_DCB.ByteSize = 8;                     // 8 data bits
        m_DCB.Parity = NOPARITY;                    // no parity
        m_DCB.fParity = false;                      // parity checking off
        m_DCB.StopBits = ONESTOPBIT;                // 1 stop bit
        m_DCB.fDtrControl = DTR_CONTROL_DISABLE;    // no DTR input flow control
        m_DCB.fOutxCtsFlow = false;             // don't monitor CTS
        m_DCB.fDsrSensitivity = false;              // always read bytes received
        m_DCB.fTXContinueOnXoff = true;         // don't wait for XONN / XOFF
        m_DCB.fOutX = false;                        // No XON / XOFF control
        m_DCB.fRtsControl = RTS_CONTROL_DISABLE;    // don't use RTS comms control
        m_DCB.fAbortOnError = false;                // don't quit on error

        // now apply the correct comm state to the device
        if (!SetCommState(m_hSerialPortHandle, &m_DCB))
        {
            text = "Serial port set com status failure.";
            ::SendMessage(m_hwndParent, UWM_UC_STATUS, (LONG)(LPCTSTR)text, 0);
        }
        // set the communication event flags
        if (!SetCommMask(m_hSerialPortHandle, EV_RXCHAR))
        {
            text = "Serial port set com mask failure.";
            ::SendMessage(m_hwndParent, UWM_UC_STATUS, (LONG)(LPCTSTR)text, 0);
        }
        COMMTIMEOUTS    timeouts;

        // return immediately on reads
        // return after completion on writes
        timeouts.ReadIntervalTimeout = MAXDWORD;
        timeouts.ReadTotalTimeoutConstant = 0;
        timeouts.ReadTotalTimeoutMultiplier = 0;
        timeouts.WriteTotalTimeoutConstant = 0;
        timeouts.WriteTotalTimeoutMultiplier = 0;

        SetCommTimeouts(m_hSerialPortHandle, &timeouts);
    }
}
else
{
    text = "Unable to open serial port \"" + m_csCommPortName + "\"";
    ::SendMessage(m_hwndParent, UWM_UC_STATUS, (LONG)(LPCTSTR)text, 0);
    m_bThreadDead = true;
    return;
}

// COMM PORT OPEN Now talk to connected instrument
while (!m_bKillThread)
{
    // we loop until m_bKillThread becomes true

    // calls to WriteFile()...

    // Read stuff (1 byte at a time)
    ReadFile(m_hSerialPortHandle, szInputBuffer, 1, &dwNumBytesRead, NULL);
    //TRACE("%c 0x%x\n", szInputBuffer[0], (int)szInputBuffer[0]);
    if (dwNumBytesRead == 1)
    {
    // do something withthe read data
    }
}
// release resources
CloseHandle(m_hSerialPortHandle);
m_bOpen = false;
m_bThreadDead = true;
m_bKillThread = false;
// thread dies on exit of routine



Roger Allen
Sonork 100.10016

Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003
GeneralRe: Thread Control and WaitCommEvent() Pin
lead2gold21-Feb-03 16:26
lead2gold21-Feb-03 16:26 
GeneralRe: Thread Control and WaitCommEvent() Pin
Daniel Lohmann22-Feb-03 0:22
Daniel Lohmann22-Feb-03 0:22 
GeneralRe: Thread Control and WaitCommEvent() Pin
lead2gold23-Feb-03 6:07
lead2gold23-Feb-03 6:07 
GeneralRe: Thread Control and WaitCommEvent() Pin
Daniel Lohmann23-Feb-03 7:46
Daniel Lohmann23-Feb-03 7:46 
GeneralRe: Thread Control and WaitCommEvent() Pin
lead2gold24-Feb-03 18:28
lead2gold24-Feb-03 18:28 
Generalgraphics.h Pin
InternetMill20-Feb-03 16:25
InternetMill20-Feb-03 16:25 
GeneralRe: graphics.h Pin
Paul M Watt20-Feb-03 16:46
mentorPaul M Watt20-Feb-03 16:46 
GeneralRe: graphics.h Pin
InternetMill21-Feb-03 10:08
InternetMill21-Feb-03 10:08 
GeneralGDI Pin
jeva20-Feb-03 16:01
jeva20-Feb-03 16:01 
GeneralRe: GDI Pin
Christian Graus20-Feb-03 16:07
protectorChristian Graus20-Feb-03 16:07 
QuestionChange the background colour of CCombobox? Pin
Zenith7420-Feb-03 14:23
Zenith7420-Feb-03 14:23 
AnswerRe: Change the background colour of CCombobox? Pin
vikramlinux20-Feb-03 23:18
vikramlinux20-Feb-03 23:18 
GeneralInterfaces to other applications Pin
User 988520-Feb-03 12:57
User 988520-Feb-03 12:57 
GeneralRe: Interfaces to other applications Pin
Brian Olej20-Feb-03 13:52
Brian Olej20-Feb-03 13:52 
GeneralRe: Interfaces to other applications Pin
User 988520-Feb-03 13:59
User 988520-Feb-03 13:59 
GeneralRe: Interfaces to other applications Pin
Brian Olej20-Feb-03 14:26
Brian Olej20-Feb-03 14:26 
GeneralRe: Interfaces to other applications Pin
Anders Molin20-Feb-03 14:07
professionalAnders Molin20-Feb-03 14:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.