Howdy, Stranger!

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

Categories

Serial COMM in Windows 9x

nguyenminhhiennguyenminhhien Member Posts: 1
I want to write a program that can tranfer data (file or only a message)between 2 computer, using a simple cable connecting 2 COM port(1 or 2).
(by both API or MFC are good)

Comments

  • mfeikmfeik Member Posts: 24
    : I want to write a program that can tranfer data (file or only a message)between 2 computer, using a simple cable connecting 2 COM port(1 or 2).
    : (by both API or MFC are good)
    :



    I'm not certain as to exactly what you are trying to achieve but here is some code that might get you on the right track.

    /* I chose to return a HANDLE for this application as I'm calling my serial routines from an included *.cpp file but your application might find it more useful to return a BOOL... depends on how you are handling errors.. the error routines included here are very simple and the message boxes have no owner window...*/
    HANDLE CreateSerialConnection(CString sComPort)
    {
    // consider adding wait state for cursor

    HANDLE hCom;
    DCB m_dcb;
    COMMTIMEOUTS CommTimeouts;

    m_dcb.DCBlength = sizeof (DCB);

    // open the comport
    hCom = CreateFile(sComPort, GENERIC_READ | GENERIC_WRITE,
    0, // exclusive access
    NULL, // no security
    OPEN_EXISTING, // requred for com port access
    0, // no overlapped I/O (yet)
    NULL); // no template used

    BOOL bProcessOk = SetupComm(hCom, 1280, 1280);
    // 1280 are my buffer settings... yours will depend on how bytes you
    // are sending/recieving

    if (!bProcessOk)
    {
    MessageBox(NULL, "An error occured setting the Comm",
    "SetupComm ERROR",
    MB_ICONSTOP | MB_OK | MB_SYSTEMMODAL );
    return FALSE; // I suggest you do not use this line
    }

    // call up the current CommState settings
    bProcessOk = GetCommState(hCom, &m_dcb);
    if (!bProcessOk)
    {
    MessageBox(NULL, "An error occured getting Comm State",
    "GetCommState ERROR",
    MB_ICONSTOP | MB_OK | MB_SYSTEMMODAL );
    return FALSE; // I suggest you do not use this line
    }

    // now that you have the CommState, change those settings that are
    // of interest to you...
    m_dcb.BaudRate = 9600;
    m_dcb.ByteSize = 8;
    m_dcb.Parity = NOPARITY;
    m_dcb.StopBits = ONESTOPBIT;
    // now set them...
    bProcessOk = SetCommState(hCom, &m_dcb);
    if (!bProcessOk)
    {
    MessageBox(NULL, "An error occured setting the Comm State",
    "SetCommState ERROR",
    MB_ICONSTOP | MB_OK | MB_SYSTEMMODAL );
    return FALSE; // I suggest you do not use this line
    }
    PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT |
    PURGE_TXCLEAR | PURGE_RXCLEAR );


    basically the same thing as above for the CommTimeouts...
    bProcessOk = GetCommTimeouts(hCom, &CommTimeouts);
    if (!bProcessOk)
    {
    MessageBox(NULL, "An error occured getting Comm timeouts",
    "GetCommTimeouts ERROR",
    MB_ICONSTOP | MB_OK | MB_SYSTEMMODAL );
    return FALSE; // I suggest you do not use this line
    }
    CommTimeouts.ReadIntervalTimeout = 1000;
    CommTimeouts.ReadTotalTimeoutConstant = 1000;
    CommTimeouts.ReadTotalTimeoutMultiplier = 30;
    CommTimeouts.WriteTotalTimeoutConstant = 10;
    CommTimeouts.WriteTotalTimeoutMultiplier = 3;

    bProcessOk = SetCommTimeouts (hCom, &CommTimeouts);
    if (!bProcessOk)
    {
    MessageBox(NULL, "An error occured setting Comm timeouts",
    "SetCommTimeouts ERROR",
    MB_ICONSTOP | MB_OK | MB_SYSTEMMODAL );
    return FALSE; // I suggest you do not use this line
    }

    return hCom;

    }//end of CreateSerialConnection()


    Once you have the serial port open (assuming that no errors occured you can now make calls to the serial port using the HANDLE returned...
    For example...
    a call to
    BOOL SerialWrite(HANDLE hCom, CString strOut, DWORD iBytesOut)
    {
    BOOL bWriteOk;
    DWORD iBytesWritten;

    bWriteOk = WriteFile(hCom, strOut, iBytesOut, &iBytesWritten, NULL);

    if ( !(iBytesOut == iBytesWritten) )
    {
    // your error code
    }
    return bWriteOk;
    }// end SerialWrite()

    ... would look like
    BOOL bWasWriteSucessful = SerialWrite(m_hCom, yourString,
    yourString.GetLength() )


    hope it helps...
    -Mel
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

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

Sign In or Register to comment.