How to read and write on SERIAL PORT
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to read and write on SERIAL PORT

  1. #1
    Join Date
    May 2007
    Posts
    25

    Question How to read and write on SERIAL PORT

    I have developed two dialog based application One for reading from COM1 port and other
    to write on COM1 port.Read application waits for com event.Problem is write application able to open com port if read applcation is not running.but not able to
    open com port if read application is running.This is not threading problem,
    Its something is regarding serial Port Basic concepts.
    My Read and write functions are as follows.I need to extend this basic functionality
    furthur,but before this simple read and write needs to work

    int CReadDlg::Readme()
    {
    CString PortSpecifier = "COM1";
    DCB dcb;
    int retVal;
    BYTE Byte;
    DWORD dwBytesTransferred;
    DWORD dwCommModemStatus;
    HANDLE hPort = CreateFile(
    PortSpecifier,
    GENERIC_READ,
    0,
    NULL,
    OPEN_EXISTING,
    0,
    NULL
    );
    if (!GetCommState(hPort,&dcb))
    return 0x100;
    dcb.BaudRate = CBR_9600; //9600 Baud
    dcb.ByteSize = 8; //8 data bits
    dcb.Parity = NOPARITY; //no parity
    dcb.StopBits = ONESTOPBIT; //1 stop
    if (!SetCommState(hPort,&dcb))
    return 0x100;
    SetCommMask (hPort, EV_RXCHAR | EV_ERR); //receive character event
    WaitCommEvent (hPort, &dwCommModemStatus, 0); //wait for character
    if (dwCommModemStatus & EV_RXCHAR)
    ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0); //read 1
    else if (dwCommModemStatus & EV_ERR)
    retVal = 0x101;
    retVal = Byte;
    CloseHandle(hPort);
    return retVal;
    }

    void CSimulatorDlg::OnWrite()
    {

    CString PortSpecifier = "COM1";
    CString data = "A";// "0xAA1g/L8H8L12.30xF5";
    DCB dcb;
    DWORD byteswritten;
    HANDLE hPort = CreateFile(PortSpecifier,GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
    if (!GetCommState(hPort,&dcb))
    return ;
    dcb.BaudRate = CBR_9600; //9600 Baud
    dcb.ByteSize = 8; //8 data bits
    dcb.Parity = NOPARITY; //no parity
    dcb.StopBits = ONESTOPBIT; //1 stop
    if (!SetCommState(hPort,&dcb))
    return ;
    bool retVal = WriteFile(hPort,data,1,&byteswritten,NULL);
    CloseHandle(hPort); //close the handle

    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: How to read and write on SERIAL PORT

    Please use code tags [CODE][/CODE] when posting code.
    There's no need to do this by yourself since there are so many free serial port classes on the net.
    This is one example http://www.naughter.com/serialport.html

    If you really want to code it here's link describing the details http://msdn.microsoft.com/en-us/library/ms810467.aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,286

    Re: How to read and write on SERIAL PORT

    Another serial port example (using MFC):
    Serial Port I/O
    Victor Nijegorodov

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: How to read and write on SERIAL PORT

    It sounds like you're trying to run both applications on the same computer. If so, you'll run in to another problem (which you may be aware of).

    The sending of data will cause the data to be transmitted on the TxD pin of the serial port, but the read event won't be triggered unless the RxD pin receives something. You could make this happen by using a serial loopback connector (it connects TxD to RxD among other things).

    Additionally, you're serial port might be set for hardware flow control, in which case nothing would be sent until the receiving device indicates it's ready. Of course you could turn flow control off which would eliminate that problem.

    Hope this helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center