Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Windows Serial w/ CreateFile/ReadFile/WriteFile problems (Crosspost)

CytCyt Member Posts: 557
I have this strange problem with a serial driver of mine using CreateFile/ReadFile/WriteFile

All characters sent have their most significant bit cleared.

Am I missing something obvious? Have tried to search MSDN and google, but haven't come up with good solutions. Debugging is of course possible, but a bit tricky not having a good Terminal program.

Could supply code, but it is really lengthuy, and I have no idea which part of the code might be wrong (or just bad sync - which I don't believe, as MSb is sent as the LAST one!).

Have tried with bot 1 and 2 stop bits, and with 2400-57600 Baud - the problem is exactly the same. I think I'm going to explode... Oh well :o)

Regards

Comments

  • whoiewhoie Member Posts: 672
    : I have this strange problem with a serial driver of mine using CreateFile/ReadFile/WriteFile
    :
    : All characters sent have their most significant bit cleared.
    :
    : Am I missing something obvious?

    It could be, and we both know that is an all-too-often happenstance. Are you sure there isn't something forcing the characters to 7-bit ASCII? I seem to remember getting bitten by that one once. Are you sure that you are getting eight data bits total? If you are, then I would tend to believe you have something masking off the MSB.


    : Debugging is of course possible, but a bit tricky not having a good Terminal program.

    That is a necessity! Doesn't Windows come with Hyperterminal? It's pretty basic, but it does the job for me...


    : Could supply code, but it is really lengthuy, and I have no idea which part of the code might be wrong (or just bad sync - which I don't believe, as MSb is sent as the LAST one!).

    A bad sync would be unlikely if all characters are exhibiting this behavior.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • Pappy1942Pappy1942 Member Posts: 46
    Hi,
    Try something like this.

    DCB dcb;

    fSuccess = GetCommState(hCom, &dcb);//hCom is handle from CreateFile

    if (!fSuccess) {
    printf ("GetCommState failed with error %d.
    ", GetLastError());
    return (2);
    }

    dcb.BaudRate = 1200; // set the baud rate
    dcb.ByteSize = 8; // data size, xmit, and rcv
    dcb.Parity = NOPARITY; // no parity bit
    dcb.StopBits = ONESTOPBIT; // one stop bit

    fSuccess = SetCommState(hCom, &dcb);

    if (!fSuccess) {
    printf ("SetCommState failed with error %d.
    ", GetLastError());
    getch();
    return (3);
    }

    Hope this helps.

    Pappy
    Pappy
    You learn something new everyday.

  • CytCyt Member Posts: 557
    I'm doing that, just in another way:
    [code]
    // ...
    char DCBString[]="baud=xxxxxx parity=x data=xx stop=x";
    DCB DeviceControlBlock;
    sprintf(DCBString,"baud=%d parity=%c data=%d stop=%d",m_nBaudRate,m_bParityOn ? 'E':'N',m_nDataBits,m_nStopBits);
    if(BuildCommDCB(DCBString,&DeviceControlBlock)==FALSE) return false;
    DeviceControlBlock.fRtsControl=RTS_CONTROL_ENABLE;

    char SerialPortName[5]="COM";
    SerialPortName[3]=(char)nCommPort+'0';

    m_hSerialFile=CreateFile(SerialPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
    if(m_hSerialFile==INVALID_HANDLE_VALUE) return false;

    if(GetCommState(m_hSerialFile,&DeviceControlBlock)==FALSE)
    {
    CloseHandle(m_hSerialFile);
    return false;
    }

    if(SetCommState(m_hSerialFile,&DeviceControlBlock)==FALSE)
    {
    CloseHandle(m_hSerialFile);
    return false;
    }
    // ...
    [/code]

    Also, some threads are started.

    My test is that I send characters from one computer to another both using the same code here to start the serial port.

    And whoie: Yeah - I'm pretty sure, that I'm chopping something of myself, but I have no idea where it's happening...

    More code?

    When receiving a character (in main thread that waits for autogenerated-from-the-driver-events):
    [code]
    case TE_FileRead:
    if(!m_bIsOpen) break; // Seemingly, a character is erroneously read upon exit - after having closed the port
    ClearCommError(m_hSerialFile,&dwDummy,&sComStat);
    while(sComStat.cbInQue)
    {
    if(GetOverlappedResult(m_hSerialFile,&m_sOverlapped,&dwDummy,TRUE))
    {
    if(ReadFile(m_hSerialFile,&c,1,&dwDummy,&m_sOverlapped)==FALSE)
    {
    int n=GetLastError();
    }
    else
    {
    sComStat.cbInQue--;
    Notify(c);
    }
    }
    }
    break;
    [/code]

    And in the bit that writes:

    [code]
    case TE_FileWrite:
    ResetEvent(m_eThreadEvents[TE_FileWrite]);

    EnterCriticalSection(&m_WriteCriticalSection);

    m_sOverlapped.Offset = 0;
    m_sOverlapped.OffsetHigh = 0;

    PurgeComm(m_hSerialFile,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT);

    WriteFile(m_hSerialFile,m_pWriteData,m_nWriteLength,&dwDummy,&m_sOverlapped);

    LeaveCriticalSection(&m_WriteCriticalSection);

    SetEvent(m_hWriteCompleteEvent);

    break;
    [/code]

    I mean - it might be really simple, if one knows where to look :o)

    Hope to get more help.

    By the way: I've hardcoded something down onto about this layer so that my main program still uses this code, but is overridden by the code on top - which pretends to be the other end but simply reads from a file and expects answers back. There - no chopping off occurs.

    Cyt
  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    ( http://forcoder.org ) free ebooks and video tutorials about / JavaScript PHP Java C++ Python Delphi Assembly Scratch Ruby C Visual Basic .NET R MATLAB Perl PL/SQL Go Objective-C Swift Visual Basic C# Erlang Clojure LabVIEW Hack Kotlin Fortran FoxPro Crystal Prolog F# Lisp Scala Lua Apex Rust Julia COBOL SAS ABAP Logo Ada Dart Bash ML Alice D Awk Scheme Transact-SQL VBScript / _____

Sign In or Register to comment.