Read from Serial Port

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

    #1

    Read from Serial Port

    I'd like to read ASCII data from a serial port, but (once again) I'm having
    trouble getting started. (Can't seem to find the basic level of docs to
    get going <sigh>)

    I'd like to use only standard "built-in" modules if possible.

    Could somebody offer a simple code-snippet to get me started reading from a
    serial port?

    Thanks!
    --
    Casey Bralla
    Chief Nerd in Residence
    The NerdWorld Organisation

  • Steve Holden

    #2
    Re: Read from Serial Port

    Casey Bralla wrote:[color=blue]
    > I'd like to read ASCII data from a serial port, but (once again) I'm having
    > trouble getting started. (Can't seem to find the basic level of docs to
    > get going <sigh>)
    >
    > I'd like to use only standard "built-in" modules if possible.
    >
    > Could somebody offer a simple code-snippet to get me started reading from a
    > serial port?
    >[/color]
    While it isn't built in, the standard solution to these problems is the
    excellent "pyserial" module.

    Google is your friend, and since pyserial is the first hit on a search
    for "python serial port" I would recommend you remember that in future
    .... that's why there's no URL in this message : Googling yourself will
    help the lesson sink in.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/

    Comment

    • Grant Edwards

      #3
      Re: Read from Serial Port

      On 2006-01-21, Casey Bralla <Nobody@Nowhere .com> wrote:
      [color=blue]
      > I'd like to read ASCII data from a serial port, but (once
      > again) I'm having trouble getting started. (Can't seem to
      > find the basic level of docs to get going <sigh>)
      >
      > I'd like to use only standard "built-in" modules if possible.[/color]

      Then there aren't going to be any "basic level docs" because
      it's just not an easy thing to do. You pretty much have to do
      exactly what you would do in C. So, for Linux/Unix, study up on
      http://www.easysw.com/~mike/serial/serial.html. Then do the
      exact same thing in python using the "os" module stuff that
      deals with file descriptors and termios. And you'll probably to
      use select to impliment a timeout when there's no data:

      The official home of the Python Programming Language




      For Win32, you'll have to do all of the nasty Win32
      CreateFile() calls and such-like. IMO, It's even uglier than
      the Posix termios stuff (though not by much). For example,
      CreateFile() takes seven parameters, and none of them default
      to useful values (it's places like that where Win32's VMS
      heritage comes shining through in a manner that instead of
      illumnating anything temporarily blinds you and causes you to
      run into a tree).
      [color=blue]
      > Could somebody offer a simple code-snippet to get me started
      > reading from a serial port?[/color]

      Not using just the built-in modules. There is no such simple
      code-snippet. If you want simple use pyserial which handles
      all of the tricky bits for you:



      It's not a "built-in" module, but it's what you want to use
      unless you already know all the contortions require to do
      serial port stuff using Posix or Win32 system calls.

      Really.

      If you truly, madly, deeply want to use just built-in modules,
      then your best bet is to download pyserial and copy the
      appropriate platform-specific code. I'll be happy to answer
      questions about the Posix parts of it, but I had the portion of
      my brain containing Win32 knowledge cauterized.

      --
      Grant Edwards grante Yow! I'm losing my
      at hair...did it go to
      visi.com ATLANTIC CITY??

      Comment

      • Diez B. Roggisch

        #4
        Re: Read from Serial Port

        > appropriate platform-specific code. I'll be happy to answer[color=blue]
        > questions about the Posix parts of it, but I had the portion of
        > my brain containing Win32 knowledge cauterized.[/color]

        I'm working on that. Last time I checked there was even some AMIGA
        pre-AGA-stuff in there. Which I actually think is cute, but could be
        filled with other more useful stuff. Any suggestions for a good
        cauterization service?

        SCNR,

        Diez

        Comment

        • Peter Hansen

          #5
          Re: Read from Serial Port

          Grant Edwards wrote:[color=blue]
          > On 2006-01-21, Casey Bralla <Nobody@Nowhere .com> wrote:[color=green]
          >>I'd like to use only standard "built-in" modules if possible.[/color]
          >
          > Then there aren't going to be any "basic level docs" because
          > it's just not an easy thing to do. You pretty much have to do
          > exactly what you would do in C.[/color]
          [color=blue]
          > For Win32, you'll have to do all of the nasty Win32
          > CreateFile() calls and such-like. IMO,[/color]

          And given that win32all is *not* standard and built-in, at least with
          the standard distribution, you can't do Win32 at all if you insist on
          builtin modules.

          But doing so would be unreasonable, and fortunately the OP indicates
          that that's just his desire, not a firm requirement, so pyserial it is! :-)

          -Peter

          Comment

          • Grant Edwards

            #6
            Re: Read from Serial Port

            On 2006-01-21, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=blue][color=green]
            >> appropriate platform-specific code. I'll be happy to answer
            >> questions about the Posix parts of it, but I had the portion of
            >> my brain containing Win32 knowledge cauterized.[/color]
            >
            > I'm working on that. Last time I checked there was even some AMIGA
            > pre-AGA-stuff in there. Which I actually think is cute, but could be
            > filled with other more useful stuff. Any suggestions for a good
            > cauterization service?[/color]

            Yea, um, hmm. Don't remember that either. Sloppy work then, eh?
            Just as well.

            --
            Grant Edwards grante Yow! ... A housewife
            at is wearing a polypyrene
            visi.com jumpsuit!!

            Comment

            • Neil Benn

              #7
              Re: Read from Serial Port

              Casey Bralla wrote:
              [color=blue]
              >I'd like to read ASCII data from a serial port, but (once again) I'm having
              >trouble getting started. (Can't seem to find the basic level of docs to
              >get going <sigh>)
              >
              >I'd like to use only standard "built-in" modules if possible.
              >
              >Could somebody offer a simple code-snippet to get me started reading from a
              >serial port?
              >
              >Thanks!
              >
              >[/color]
              Here's some code which should help to get you started, I thought rather
              than leaving you in the hands of Google (which is not your friend, no
              more than MaccyDs is, as Flavour Flav said - 'Don;t believe the hype'),
              this may be helpful :

              Here's a bit cut out from my own code using pySerial - some of the
              variables are not detailed but you can get the idea :

              self.__objSeria lPort = serial.Serial(s elf._dctConnect ionParams
              ['ConnectionID'],
              self._dctConnec tionParams
              ['BaudRate'],
              self._dctConnec tionParams
              ['DataBits'],
              self._dctConnec tionParams
              ['Parity'],
              self._dctConnec tionParams
              ['StopBits'],
              10000, #TimeOut

              int(self._dctCo nnectionParams
              ['XONXOFF']),

              int(self._dctCo nnectionParams
              ['RTSCTS']))

              To send data with pyserial, you do this :

              self.__objLock. acquire()
              try:
              try:
              self.__objSeria lPort.write(pSt rMessage)
              except StandardError:
              self._objCurren tState = self._STATUS_CO NSTS.ERROR
              raise
              else:
              self._objCurren tState = self._STATUS_CO NSTS.CONNECTED
              finally:
              self.__objLock. release()

              The lock on this isn't needed for single threaded code but I would
              recommend making a class which deals with the communication in a
              thread-safe manner (the connection and disconnection as well - the above
              bit should be in a lock as well).

              PySerial doesn't have the concept of Observer patterns to get
              message coming back in so you'll have to make a polling thread to
              observe the serial port when you want to read data, here's the checking
              bit - I'll leave the threading and observers up to you:

              def __checkSerial(s elf):
              """
              Checks serial port to see if anything is available to read
              """
              self.__objLock. acquire()
              try:
              try:
              intNoChars = self.__objSeria lPort.inWaiting ()
              if intNoChars > 0:
              strReceivedStri ng =
              self.__objSeria lPort.read(intN oChars)
              self.fireNewMes sage(strReceive dString)
              except:
              raise
              finally:
              self.__objLock. release()

              PySerial wraps all the platform specific stuff you, so you should
              really use that, it behaves fairly well - the only real problem is a
              lack of an observer interface but you can solve that as detailed above.
              One final thing, don;t forget that RS232 isn't really a standard - it's
              more like a rumour :-).

              Cheers,

              Neil

              --

              Neil Benn
              Senior Automation Engineer
              Cenix BioScience
              BioInnovations Zentrum
              Tatzberg 47
              D-01307
              Dresden
              Germany

              Tel : +49 (0)351 4173 154
              e-mail : benn@cenix-bioscience.com
              Cenix Website : http://www.cenix-bioscience.com

              Comment

              Working...