Strange Serial Port Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buggex
    New Member
    • Jan 2008
    • 3

    Strange Serial Port Problem

    Hey!
    I have runned into a strange problem, when trying to setup a serial port. The code I use to open the port:
    Code:
    // Opening the Port "COM1"
    hComm = CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    
    // DCB Settings (Baudrate, parity, numBits, stopBit)
    FillMemory(&dcb, sizeof(dcb), 0);
    dcb.DCBlength = sizeof(dcb);
    BuildCommDCB("57600,n,8,1", &dcb);
    	
    // Communications time-outs
    timeouts.ReadIntervalTimeout = MAXDWORD; 
    timeouts.ReadTotalTimeoutMultiplier = 0;
    timeouts.ReadTotalTimeoutConstant = 0;
    timeouts.WriteTotalTimeoutMultiplier = 0;
    timeouts.WriteTotalTimeoutConstant = 0;
    SetCommTimeouts(hComm, &timeouts);
    When I execute my program i can not read or write data to the port. Then I open the serial port with PUTTY and close PUTTY. Then I execute my prpgram again, and now I can both read and write to the port. Any one that can tell me whats going on???
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Is your file handle hComm valid??

    You don't check it after CreateFile.

    Comment

    • buggex
      New Member
      • Jan 2008
      • 3

      #3
      yes the handle is valid.

      Comment

      • arthurshen
        New Member
        • Feb 2008
        • 8

        #4
        Originally posted by buggex
        yes the handle is valid.
        Hi!buggex. When I looking your code.I only find that you just opened

        your COM1 port ,set up DCB structure and Timeout . I guess that you

        you need another function "readfile() " and "writefile()".T hen,operate

        hComm handle for read or write COM port.

        Comment

        • buggex
          New Member
          • Jan 2008
          • 3

          #5
          Originally posted by arthurshen
          Hi!buggex. When I looking your code.I only find that you just opened

          your COM1 port ,set up DCB structure and Timeout . I guess that you

          you need another function "readfile() " and "writefile()".T hen,operate

          hComm handle for read or write COM port.
          Yes i have them to.

          The write command
          Code:
          WriteFile(hComm, (LPVOID)&lpBuf, 1, &dwWritten, NULL);
          The read command
          Code:
          ReadFile(hComm, lpBuf, 1, &dwRead, NULL);

          Comment

          • arthurshen
            New Member
            • Feb 2008
            • 8

            #6
            Originally posted by buggex
            Yes i have them to.

            The write command
            Code:
            WriteFile(hComm, (LPVOID)&lpBuf, 1, &dwWritten, NULL);
            The read command
            Code:
            ReadFile(hComm, lpBuf, 1, &dwRead, NULL);

            Dear:BUGGEX

            I have no ideal that your problem was already solved or not.Basically,I thought on the matter was two strange points.

            First,you excused your program and it had not error?? It's just not work for I/O.

            Second ,you excused your program after used PUTTY.But your program It's work??

            About your first strange point,As result of your codes information are not complete enough.So I can't help you.

            As for second ,I got some information from MSDN document below this URL
            http://msdn2.microsoft .com/en-us/library/aa363423(VS.85) .aspx

            Maybe,the first paragraph is what you wanna know.

            Comment

            Working...