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. serial port communication

serial port communication

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 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.
  • R Offline
    R Offline
    RockyJames
    wrote on last edited by
    #1

    hi i am working on serial port communication,i have opened the port using CreateFile function ,wrote the data using WriteFile function,and Read data using ReadFile Function.Every thing is fine until here,Say for Example..the instrument is switched off..but connected to the port..how do detect whether the instrument is switched ON or OFF...? thanks in before James

    C K 2 Replies Last reply
    0
    • R RockyJames

      hi i am working on serial port communication,i have opened the port using CreateFile function ,wrote the data using WriteFile function,and Read data using ReadFile Function.Every thing is fine until here,Say for Example..the instrument is switched off..but connected to the port..how do detect whether the instrument is switched ON or OFF...? thanks in before James

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

      You should check the documentation of your instrument. Maybe when switched ON, there is one line that is set to high (I think it is the DTR - Data Terminal Ready). If this is the case, just check for the status of this line. Otherwise you can always send something to which the instrument is supposed to respond. If you don't get an answer, then the device is either not connected, either switched OFF. I don't think it is possible to make a difference between disconnected and switched off.


      Cédric Moonen Software developer
      Charting control [Updated - v1.1]

      R 1 Reply Last reply
      0
      • C Cedric Moonen

        You should check the documentation of your instrument. Maybe when switched ON, there is one line that is set to high (I think it is the DTR - Data Terminal Ready). If this is the case, just check for the status of this line. Otherwise you can always send something to which the instrument is supposed to respond. If you don't get an answer, then the device is either not connected, either switched OFF. I don't think it is possible to make a difference between disconnected and switched off.


        Cédric Moonen Software developer
        Charting control [Updated - v1.1]

        R Offline
        R Offline
        RockyJames
        wrote on last edited by
        #3

        how to check DTR..is there any API fucntion for it..?

        C 1 Reply Last reply
        0
        • R RockyJames

          how to check DTR..is there any API fucntion for it..?

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

          I think you can use GetCommModemStatus[^]. I think I was wrong, you have to check for the DSR (Data Set Ready) line, not the DSR (I always mix them).


          Cédric Moonen Software developer
          Charting control [Updated - v1.1]

          R 1 Reply Last reply
          0
          • C Cedric Moonen

            I think you can use GetCommModemStatus[^]. I think I was wrong, you have to check for the DSR (Data Set Ready) line, not the DSR (I always mix them).


            Cédric Moonen Software developer
            Charting control [Updated - v1.1]

            R Offline
            R Offline
            RockyJames
            wrote on last edited by
            #5

            Is there any other alternative..?..i have tried used GetCommModemStatus function..its returning false..even if..the instrument is switched on..

            C 1 Reply Last reply
            0
            • R RockyJames

              Is there any other alternative..?..i have tried used GetCommModemStatus function..its returning false..even if..the instrument is switched on..

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

              Did you pass the correct handle ? After the port has been opened ?

              RockyJames wrote:

              Is there any other alternative..?..

              Yes, as I said previously: send something to which your device is supposed to answer. If you don't receive an answer, then the device is not connected or switched off.


              Cédric Moonen Software developer
              Charting control [Updated - v1.1]

              R 2 Replies Last reply
              0
              • C Cedric Moonen

                Did you pass the correct handle ? After the port has been opened ?

                RockyJames wrote:

                Is there any other alternative..?..

                Yes, as I said previously: send something to which your device is supposed to answer. If you don't receive an answer, then the device is not connected or switched off.


                Cédric Moonen Software developer
                Charting control [Updated - v1.1]

                R Offline
                R Offline
                RockyJames
                wrote on last edited by
                #7

                yeah i have sent..correct handle..i have opened port it in OVERLAPPED MODE..i am using WaitForSingleObject function..when i write data ..its able to write..when i start reading the data..it is getting stucked..at WaitForSingleObject function..

                1 Reply Last reply
                0
                • R RockyJames

                  hi i am working on serial port communication,i have opened the port using CreateFile function ,wrote the data using WriteFile function,and Read data using ReadFile Function.Every thing is fine until here,Say for Example..the instrument is switched off..but connected to the port..how do detect whether the instrument is switched ON or OFF...? thanks in before James

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

                  As Cedric Moonen already said, you can check the DSR signal. In addition you should raise the RTS (Request To Send) signal and check for the reply from the device, CTS (Clear To Send). If you get a transition from Off to On in the CTS line, you can be pretty sure the device is powered on. For the serial communication, i suggest you use a serial commuinications library, the are some good ones here at CP.

                  Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                  R 2 Replies Last reply
                  0
                  • K kakan

                    As Cedric Moonen already said, you can check the DSR signal. In addition you should raise the RTS (Request To Send) signal and check for the reply from the device, CTS (Clear To Send). If you get a transition from Off to On in the CTS line, you can be pretty sure the device is powered on. For the serial communication, i suggest you use a serial commuinications library, the are some good ones here at CP.

                    Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                    R Offline
                    R Offline
                    RockyJames
                    wrote on last edited by
                    #9

                    hi friend ,can u be more clear.. before ReadFile..i want to know whether system is ON or OFF..can u pls suggest..me the function names to use.. thanks in before James

                    K 1 Reply Last reply
                    0
                    • C Cedric Moonen

                      Did you pass the correct handle ? After the port has been opened ?

                      RockyJames wrote:

                      Is there any other alternative..?..

                      Yes, as I said previously: send something to which your device is supposed to answer. If you don't receive an answer, then the device is not connected or switched off.


                      Cédric Moonen Software developer
                      Charting control [Updated - v1.1]

                      R Offline
                      R Offline
                      RockyJames
                      wrote on last edited by
                      #10

                      thank`s for u r support..issuse is closed..i have done..with the commtimeout....

                      1 Reply Last reply
                      0
                      • K kakan

                        As Cedric Moonen already said, you can check the DSR signal. In addition you should raise the RTS (Request To Send) signal and check for the reply from the device, CTS (Clear To Send). If you get a transition from Off to On in the CTS line, you can be pretty sure the device is powered on. For the serial communication, i suggest you use a serial commuinications library, the are some good ones here at CP.

                        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                        R Offline
                        R Offline
                        RockyJames
                        wrote on last edited by
                        #11

                        thank`s for u r support..issuse is closed..i have done..with the commtimeout....

                        1 Reply Last reply
                        0
                        • R RockyJames

                          hi friend ,can u be more clear.. before ReadFile..i want to know whether system is ON or OFF..can u pls suggest..me the function names to use.. thanks in before James

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

                          Hello. The problem with detecting serial devices is that the serial port very often can have a random status when the serial port is unconnected. I.e. the DSR signal can be in it's "On"-position even when the serial port isn't even connected! (It depends on the hardware). The only reliable way of detecting if: 1. There *is* a device conncted. 2. The device is powered on. is to catch a transition on one (or more) status lines. An easy way is to ask the device if the PC (Data Terminal) may send data to the DS (Data Set). If the DS raises it's Clear To Send signal, then the PC(DT) may send data to it. Check out MSDN for the documentation, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/dcb_str.asp[^]

                          Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                          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