Read data from Serial Command

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brianrpsgt1

    Read data from Serial Command

    I am new to scripting. I am trying to read the settings from a serial
    device using Python. I have been able to successfully connect to the
    device and change baud rate settings, ect... with PySerial. I am
    trying to send a command to the serial device and capture the returned
    info, however, it is not working. Code is below:

    import serial
    import time

    s = serial.Serial(p ort=1, timeout=None, baudrate=9600)
    print s
    time.sleep(5)
    print "Enter CFG"
    s.write('CFG')
    print "Change baud"
    s.baudrate=1152 00
    print s
    time.sleep(5)
    print "New line"
    s.write('\n')


    time.sleep(2)
    print "Show Encryption Setting"
    nw = s.write('sh nw enc')

    time.sleep(1)

    print nw
    s.close()

    Thanks

    B
  • Grant Edwards

    #2
    Re: Read data from Serial Command

    On 2008-10-10, brianrpsgt1 <brianlong@cox. netwrote:
    I am new to scripting. I am trying to read the settings from a serial
    device using Python. I have been able to successfully connect to the
    device and change baud rate settings, ect... with PySerial. I am
    trying to send a command to the serial device and capture the returned
    info, however, it is not working.
    It works fine for me.

    I'm afraid you're going to have to be a bit more detailed than
    "it is not working". Are we supposed to guess what it's doing
    and how that differs from what you want it to do?

    Do you have the serial cable plugged in?

    Is the device to which you're talking powered on?
    Code is below:
    >
    import serial
    import time
    >
    s = serial.Serial(p ort=1, timeout=None, baudrate=9600)
    print s
    time.sleep(5)
    print "Enter CFG"
    s.write('CFG')
    print "Change baud"
    s.baudrate=1152 00
    print s
    time.sleep(5)
    print "New line"
    s.write('\n')
    >
    >
    time.sleep(2)
    print "Show Encryption Setting"
    nw = s.write('sh nw enc')
    >
    time.sleep(1)
    >
    print nw
    s.close()
    --
    Grant Edwards grante Yow! Well, I'm INVISIBLE
    at AGAIN ... I might as well
    visi.com pay a visit to the LADIES
    ROOM ...

    Comment

    • brianrpsgt1

      #3
      Re: Read data from Serial Command

      Thanks for the message

      What exactly is happening is that the return is "None" for the command
      that I am sending. If I connect through Hyperterminal and execute the
      'sh nw enc' command, it returns 'WEP'

      I have confirmed that the serial port is correct and open with the
      s.isOpen() function. Also able to successfully connect in
      Hypterterminal with the same configuration settings.




      Grant Edwards wrote:
      On 2008-10-10, brianrpsgt1 <brianlong@cox. netwrote:
      I am new to scripting. I am trying to read the settings from a serial
      device using Python. I have been able to successfully connect to the
      device and change baud rate settings, ect... with PySerial. I am
      trying to send a command to the serial device and capture the returned
      info, however, it is not working.
      >
      It works fine for me.
      >
      I'm afraid you're going to have to be a bit more detailed than
      "it is not working". Are we supposed to guess what it's doing
      and how that differs from what you want it to do?
      >
      Do you have the serial cable plugged in?
      >
      Is the device to which you're talking powered on?
      >
      Code is below:

      import serial
      import time

      s = serial.Serial(p ort=1, timeout=None, baudrate=9600)
      print s
      time.sleep(5)
      print "Enter CFG"
      s.write('CFG')
      print "Change baud"
      s.baudrate=1152 00
      print s
      time.sleep(5)
      print "New line"
      s.write('\n')


      time.sleep(2)
      print "Show Encryption Setting"
      nw = s.write('sh nw enc')

      time.sleep(1)

      print nw
      s.close()
      >
      --
      Grant Edwards grante Yow! Well, I'm INVISIBLE
      at AGAIN ... I might as well
      visi.com pay a visit to the LADIES
      ROOM ...

      Comment

      • Grant Edwards

        #4
        Re: Read data from Serial Command

        On 2008-10-10, brianrpsgt1 <brianlong@cox. netwrote:
        Thanks for the message
        >
        What exactly is happening is that the return is "None" for the command
        that I am sending. If I connect through Hyperterminal and execute the
        'sh nw enc' command, it returns 'WEP'
        It looks to me like you're never reading from the serial port.
        All you're calling is write().

        Also, are you sure that the device doesn't expect commands to
        be termined by carriage returns?
        import serial
        import time
        >
        s = serial.Serial(p ort=1, timeout=None, baudrate=9600)
        print s
        time.sleep(5)
        print "Enter CFG"
        s.write('CFG')
        print "Change baud"
        s.baudrate=1152 00
        print s
        time.sleep(5)
        print "New line"
        s.write('\n')
        >
        >
        time.sleep(2)
        print "Show Encryption Setting"
        nw = s.write('sh nw enc')
        time.sleep(1)
        Try changing that to

        s.write('sh nw enc')
        time.sleep(1)
        nw = s.read(1024)
        print nw
        s.close()
        There are plenty of example programs at:

        Download Python Serial Port Extension for free. Multiplatform Serial Port Module for Python (Win32, Jython, Linux, BSD and more)


        --
        Grant Edwards grante Yow!
        at BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-
        visi.com

        Comment

        • brianrpsgt1

          #5
          Re: Read data from Serial Command

          Gave that a shot.... what is happening is that the script is
          hanging. Does that mean that the write function is not making it
          through, thus there is nothing to return?



          Grant Edwards wrote:
          On 2008-10-10, brianrpsgt1 <brianlong@cox. netwrote:
          Thanks for the message

          What exactly is happening is that the return is "None" for the command
          that I am sending. If I connect through Hyperterminal and execute the
          'sh nw enc' command, it returns 'WEP'
          >
          It looks to me like you're never reading from the serial port.
          All you're calling is write().
          >
          Also, are you sure that the device doesn't expect commands to
          be termined by carriage returns?
          >
          import serial
          import time

          s = serial.Serial(p ort=1, timeout=None, baudrate=9600)
          print s
          time.sleep(5)
          print "Enter CFG"
          s.write('CFG')
          print "Change baud"
          s.baudrate=1152 00
          print s
          time.sleep(5)
          print "New line"
          s.write('\n')


          time.sleep(2)
          print "Show Encryption Setting"
          nw = s.write('sh nw enc')
          time.sleep(1)
          >
          Try changing that to
          >
          s.write('sh nw enc')
          time.sleep(1)
          nw = s.read(1024)
          >
          print nw
          s.close()
          >
          There are plenty of example programs at:
          >
          Download Python Serial Port Extension for free. Multiplatform Serial Port Module for Python (Win32, Jython, Linux, BSD and more)

          >
          --
          Grant Edwards grante Yow!
          at BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-
          visi.com

          Comment

          • brianrpsgt1

            #6
            Re: Read data from Serial Command

            Again, what is weird is that all works fine in Hyperterminal, but not
            with the Python script.



            brianrpsgt1 wrote:
            Gave that a shot.... what is happening is that the script is
            hanging. Does that mean that the write function is not making it
            through, thus there is nothing to return?
            >
            >
            >
            Grant Edwards wrote:
            On 2008-10-10, brianrpsgt1 <brianlong@cox. netwrote:
            Thanks for the message
            >
            What exactly is happening is that the return is "None" for the command
            that I am sending. If I connect through Hyperterminal and execute the
            'sh nw enc' command, it returns 'WEP'
            It looks to me like you're never reading from the serial port.
            All you're calling is write().

            Also, are you sure that the device doesn't expect commands to
            be termined by carriage returns?
            import serial
            import time
            >
            s = serial.Serial(p ort=1, timeout=None, baudrate=9600)
            print s
            time.sleep(5)
            print "Enter CFG"
            s.write('CFG')
            print "Change baud"
            s.baudrate=1152 00
            print s
            time.sleep(5)
            print "New line"
            s.write('\n')
            >
            >
            time.sleep(2)
            print "Show Encryption Setting"
            nw = s.write('sh nw enc')
            time.sleep(1)
            Try changing that to

            s.write('sh nw enc')
            time.sleep(1)
            nw = s.read(1024)
            print nw
            s.close()
            There are plenty of example programs at:

            Download Python Serial Port Extension for free. Multiplatform Serial Port Module for Python (Win32, Jython, Linux, BSD and more)


            --
            Grant Edwards grante Yow!
            at BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-BI-
            visi.com

            Comment

            • brianrpsgt1

              #7
              Re: Read data from Serial Command

              That did it! The fix was the '\r'

              Thanks for the assistance Dennis and Grant!

              Dennis Lee Bieber wrote:
              On Fri, 10 Oct 2008 15:40:08 -0700 (PDT), brianrpsgt1
              <brianlong@cox. netdeclaimed the following in comp.lang.pytho n:
              >
              Again, what is weird is that all works fine in Hyperterminal, but not
              with the Python script.
              And are you hitting the return key when using Hyperterminal?
              >

              Try changing that to

              s.write('sh nw enc')
              >
              s.write("sh nw enc\r") #presuming you have to hit the return key in
              Hyperterminal
              >
              You may also want to configure the serial port with a read timeout
              --
              Wulfraed Dennis Lee Bieber KD6MOG
              wlfraed@ix.netc om.com wulfraed@bestia ria.com

              (Bestiaria Support Staff: web-asst@bestiaria. com)
              HTTP://www.bestiaria.com/

              Comment

              Working...