Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Writing to serial port problem. [modified]

Writing to serial port problem. [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasequestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VCSharp007
    wrote on last edited by
    #1

    Hi, I had got an error as "Invalid handle specified" while trying write to a serial port(COM1)using WriteFile(). But the handle to serial port is seems to be valid. One by one step I had followed is below. Step 1: Create a connection HANDLE hSerial = CreateFile( "COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL); Step 2: Configure the settings DCB db; memset( &db,0,sizeof(db)); db.DCBlength = sizeof(db); if( ! GetCommState( hSerial, &db )) { return; } if( ! SetCommState( hSerial, &db ) ) { DWORD dw = GetLastError(); return; } Step 3: DWORD dwWritten; OVERLAPPED or; if( WriteFile( hSerial, "Write", 6, &dwWritten, &or )) { AfxMessageBox( "Write Success!!!"); } Step1 and Step2 are successfully executed. But step3 returns an "Invalid handle specified" error. What may be prob? Thanx in Advance. -- modified at 0:33 Friday 11th August, 2006

    C K 2 Replies Last reply
    0
    • V VCSharp007

      Hi, I had got an error as "Invalid handle specified" while trying write to a serial port(COM1)using WriteFile(). But the handle to serial port is seems to be valid. One by one step I had followed is below. Step 1: Create a connection HANDLE hSerial = CreateFile( "COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL); Step 2: Configure the settings DCB db; memset( &db,0,sizeof(db)); db.DCBlength = sizeof(db); if( ! GetCommState( hSerial, &db )) { return; } if( ! SetCommState( hSerial, &db ) ) { DWORD dw = GetLastError(); return; } Step 3: DWORD dwWritten; OVERLAPPED or; if( WriteFile( hSerial, "Write", 6, &dwWritten, &or )) { AfxMessageBox( "Write Success!!!"); } Step1 and Step2 are successfully executed. But step3 returns an "Invalid handle specified" error. What may be prob? Thanx in Advance. -- modified at 0:33 Friday 11th August, 2006

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      What is the value of hSerial at this point (check with your debugger) ? It should be something else than 0xFFFFFFFF


      Cédric Moonen Software developer
      Charting control

      1 Reply Last reply
      0
      • V VCSharp007

        Hi, I had got an error as "Invalid handle specified" while trying write to a serial port(COM1)using WriteFile(). But the handle to serial port is seems to be valid. One by one step I had followed is below. Step 1: Create a connection HANDLE hSerial = CreateFile( "COM1",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL); Step 2: Configure the settings DCB db; memset( &db,0,sizeof(db)); db.DCBlength = sizeof(db); if( ! GetCommState( hSerial, &db )) { return; } if( ! SetCommState( hSerial, &db ) ) { DWORD dw = GetLastError(); return; } Step 3: DWORD dwWritten; OVERLAPPED or; if( WriteFile( hSerial, "Write", 6, &dwWritten, &or )) { AfxMessageBox( "Write Success!!!"); } Step1 and Step2 are successfully executed. But step3 returns an "Invalid handle specified" error. What may be prob? Thanx in Advance. -- modified at 0:33 Friday 11th August, 2006

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        Try to zero out or before using it. ZeroMemory(&or, sizeof(OVERLAPPED)); Then set the hEvent member to your handle (hSerial). Or an easier way: Use a ready made serial communication class. There are quite a few here at CP. -- modified at 3:15 Friday 11th August, 2006

        V 1 Reply Last reply
        0
        • K kakan

          Try to zero out or before using it. ZeroMemory(&or, sizeof(OVERLAPPED)); Then set the hEvent member to your handle (hSerial). Or an easier way: Use a ready made serial communication class. There are quite a few here at CP. -- modified at 3:15 Friday 11th August, 2006

          V Offline
          V Offline
          VCSharp007
          wrote on last edited by
          #4

          oops! That was the problem. Thanx you very much.

          K 1 Reply Last reply
          0
          • V VCSharp007

            oops! That was the problem. Thanx you very much.

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #5

            I'm glad to help you. But i would suggest you to use a ready-made serial comms class. It's not easy to handle two different overlapped structures (one for read, one for write). And if you don't use overlapped, then your reads will hang until there is data to read. A serial comms class will take care of that problem for you.

            V 1 Reply Last reply
            0
            • K kakan

              I'm glad to help you. But i would suggest you to use a ready-made serial comms class. It's not easy to handle two different overlapped structures (one for read, one for write). And if you don't use overlapped, then your reads will hang until there is data to read. A serial comms class will take care of that problem for you.

              V Offline
              V Offline
              VCSharp007
              wrote on last edited by
              #6

              I decided to use non-overlapped communication. I had made a worker thread for reading the serial port hoping that the blocking issue get solved. But from the worker thread if I call ReadFile() using the same handle, it do not wait, even if there is no data in the serial port. why this happens? Plz help.. -- modified at 7:04 Friday 11th August, 2006

              K 1 Reply Last reply
              0
              • V VCSharp007

                I decided to use non-overlapped communication. I had made a worker thread for reading the serial port hoping that the blocking issue get solved. But from the worker thread if I call ReadFile() using the same handle, it do not wait, even if there is no data in the serial port. why this happens? Plz help.. -- modified at 7:04 Friday 11th August, 2006

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #7

                Well, I'm not sure what happens. My suggestion is to read about ReadFile at MSDN. Or, as I said before... Use a ready-made serial comms class... That way, you won't have to reinvent the wheel. Serial comms ain't easy to handle. I'm leaving for the weekend now.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups