Pyserial never read

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

    #1

    Pyserial never read

    Hello at all
    sorry for my english but i'm Italian.
    I use pyserial to communicate via rs232 with an extarnal device called
    smartmouse.
    I write the exact line that i want , but when i read i read only the
    echo ond not the other bytes.
    When i meke the same project with delphi i solve this problem with the
    call of the sleep.
    But in python it don't work.
    Have you got some ideas for solve my problem?

    Best Regards

    Luca

  • jdhmistey@gmail.com

    #2
    Re: Pyserial never read

    I don't know alot about python but I could help you with english?
    eventhough it was really good

    best regards-
    Just

    Comment

    • luca72

      #3
      Re: Pyserial never read

      Thanks

      Luca

      Comment

      • francois.schnell@gmail.com

        #4
        Re: Pyserial never read

        Hello,

        I'm not sure I understand precisely your question but maybe you can try
        a readline and a flushinput like that:

        import serial

        port= 0 # or the port where you're device is connected
        baudrate=9600 # or the baudrate of your device

        s = serial.Serial(p ort, baudrate) # Open the port

        for i in range(100):
        s.flushInput()
        line= s.readline()
        print line

        Comment

        • luca72

          #5
          Re: Pyserial never read

          Thanks for your help, but it don't solve the problem.
          I receive only the echo and full stop.

          Many Thanks

          Best Regards

          Luca

          Comment

          • Nick Craig-Wood

            #6
            Re: Pyserial never read

            luca72 <lucaberto@libe ro.it> wrote:[color=blue]
            > Thanks for your help, but it don't solve the problem.
            > I receive only the echo and full stop.[/color]

            Try swapping pins 2 and 3 in the lead.

            --
            Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

            Comment

            • Peter Hansen

              #7
              Re: Pyserial never read

              Nick Craig-Wood wrote:[color=blue]
              > luca72 <lucaberto@libe ro.it> wrote:
              >[color=green]
              >> Thanks for your help, but it don't solve the problem.
              >> I receive only the echo and full stop.[/color]
              >
              > Try swapping pins 2 and 3 in the lead.[/color]

              Anything's possible, but given that in his original post he says it
              works when he uses Delphi, it seems unlikely making a change to the
              hardware is necessary.

              -Peter

              Comment

              • Peter Hansen

                #8
                Re: Pyserial never read

                luca72 wrote:[color=blue]
                > Thanks for your help, but it don't solve the problem.
                > I receive only the echo and full stop.[/color]

                If you want help, you'll do better to post small pieces of code that you
                are actually using, rather than making us guess or imagine what you are
                doing. There are perhaps a dozen things that can go wrong with serial
                communications, and it's not efficient for us to start suggesting them
                one at a time...

                -Peter

                Comment

                • Nick Craig-Wood

                  #9
                  Re: Pyserial never read

                  Peter Hansen <peter@engcorp. com> wrote:[color=blue]
                  > Nick Craig-Wood wrote:[color=green]
                  > > luca72 <lucaberto@libe ro.it> wrote:
                  > >[color=darkred]
                  > >> Thanks for your help, but it don't solve the problem.
                  > >> I receive only the echo and full stop.[/color]
                  > >
                  > > Try swapping pins 2 and 3 in the lead.[/color]
                  >
                  > Anything's possible, but given that in his original post he says it
                  > works when he uses Delphi, it seems unlikely making a change to the
                  > hardware is necessary.[/color]

                  Sorry missed that bit!

                  Pyserial works very well in my experience (under linux).

                  Serial ports are generally a pain though ;-)

                  --
                  Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

                  Comment

                  • luca72

                    #10
                    Re: Pyserial never read


                    Thanks to all for the help,

                    here you find the code, pls note if i use handshaking = 1 the
                    application don't start.
                    in the delphi configuratio of com port if i use or not handshaking the
                    application work.
                    Best Regards at all

                    Luca

                    import serial
                    import win32file
                    port = 2
                    baudrate = 38400
                    bytesize =serial.EIGHTBI TS
                    parity =serial.PARITY_ ODD
                    stopbits =serial.STOPBIT S_TWO
                    timeout = 1
                    ser = serial.Serial(2 , baudrate=38400, bytesize=8,
                    parity=serial.P ARITY_ODD, stopbits=2, timeout=3)
                    ct = ''
                    ch = ''
                    a = self.textCtrl1. GetValue()
                    ind = 0
                    ind1 = 2
                    lunghezza = len(a)
                    while ind < lunghezza :
                    b = a[ind:ind1]
                    b = int(b,16)
                    b = ~b
                    c = ''.join([str((b >> Digit) & 1) for Digit in range(7,
                    -1, -1)])
                    c1 = c[0:4]
                    c2 = c[4:]
                    c1 = c1[3:] + c1[2:3] + c1[1:2] + c1[0:1]
                    c2 = c2[3:] + c2[2:3] + c2[1:2] + c2[0:1]
                    c1 = hex(int(c1,2))
                    c2 = hex(int(c2,2))
                    c1 = c1[2:]
                    c2 = c2[2:]
                    c = c2+c1
                    ct = ct + c
                    ind = ind + 2
                    ind1 = ind1 + 2
                    c = int(c,16)
                    c = chr(c)
                    ch = ch + c


                    ser.write(ch)

                    elf.textCtrl2.S etValue(ct)
                    ser.readline()



                    Pls.Note i hove also try with read(number of byte ) with inWaiting(),
                    flush etc........


                    But no result.

                    Thanks Luca

                    Comment

                    • Grant Edwards

                      #11
                      Re: Pyserial never read

                      On 2006-02-22, Nick Craig-Wood <nick@craig-wood.com> wrote:
                      [color=blue][color=green]
                      >> Anything's possible, but given that in his original post he says it
                      >> works when he uses Delphi, it seems unlikely making a change to the
                      >> hardware is necessary.[/color]
                      >
                      > Sorry missed that bit!
                      >
                      > Pyserial works very well in my experience (under linux).[/color]

                      I've used it extensively under both Linux and Win32, and it
                      works very well under both.
                      [color=blue]
                      > Serial ports are generally a pain though ;-)[/color]

                      That's the truth.

                      --
                      Grant Edwards grante Yow! Somewhere in Tenafly,
                      at New Jersey, a chiropractor
                      visi.com is viewing "Leave it to
                      Beaver"!

                      Comment

                      • Petr Jakes

                        #12
                        Re: Pyserial never read

                        Well, I think it is better to start with some simple code first.
                        Try to read serial port and print it out.
                        something like this could work:

                        import serial

                        s = serial.Serial(p ort=2,baudrate= 38400, timeout=20)

                        while 1:
                        print s.readline()

                        Petr Jakes

                        Comment

                        • sam

                          #13
                          Re: Pyserial never read

                          luca72 wrote:[color=blue]
                          > Thanks to all for the help,
                          >
                          > here you find the code, pls note if i use handshaking = 1 the
                          > application don't start.
                          > in the delphi configuratio of com port if i use or not handshaking the
                          > application work.
                          > Best Regards at all
                          >
                          > Luca
                          >
                          > import serial
                          > import win32file
                          > port = 2[/color]

                          Is port = 2 correct?
                          I thought that com ports under windows are designated as a string
                          "com1","com2",. .etc.
                          [color=blue]
                          > baudrate = 38400
                          > bytesize =serial.EIGHTBI TS
                          > parity =serial.PARITY_ ODD
                          > stopbits =serial.STOPBIT S_TWO
                          > timeout = 1
                          > ser = serial.Serial(2 , baudrate=38400, bytesize=8,
                          > parity=serial.P ARITY_ODD, stopbits=2, timeout=3)
                          > ct = ''
                          > ch = ''
                          > a = self.textCtrl1. GetValue()
                          > ind = 0
                          > ind1 = 2
                          > lunghezza = len(a)
                          > while ind < lunghezza :
                          > b = a[ind:ind1]
                          > b = int(b,16)
                          > b = ~b
                          > c = ''.join([str((b >> Digit) & 1) for Digit in range(7,
                          > -1, -1)])
                          > c1 = c[0:4]
                          > c2 = c[4:]
                          > c1 = c1[3:] + c1[2:3] + c1[1:2] + c1[0:1]
                          > c2 = c2[3:] + c2[2:3] + c2[1:2] + c2[0:1]
                          > c1 = hex(int(c1,2))
                          > c2 = hex(int(c2,2))
                          > c1 = c1[2:]
                          > c2 = c2[2:]
                          > c = c2+c1
                          > ct = ct + c
                          > ind = ind + 2
                          > ind1 = ind1 + 2
                          > c = int(c,16)
                          > c = chr(c)
                          > ch = ch + c
                          >
                          >
                          > ser.write(ch)
                          >
                          > elf.textCtrl2.S etValue(ct)
                          > ser.readline()[/color]
                          You might want to try using ser.read() instead of ser.readline() as
                          you may not be getting linefeed carrage return characters . I usually
                          setup a buffer to scan for the characters I expect.
                          [color=blue]
                          >
                          >
                          >
                          > Pls.Note i hove also try with read(number of byte ) with inWaiting(),
                          > flush etc........
                          >
                          >
                          > But no result.
                          >
                          > Thanks Luca[/color]

                          Hope this helps
                          Sam Schulenburg

                          Comment

                          • Grant Edwards

                            #14
                            Re: Pyserial never read

                            On 2006-02-22, sam <samschul@pacbe ll.net> wrote:[color=blue]
                            > luca72 wrote:[color=green]
                            >> Thanks to all for the help,
                            >>
                            >> here you find the code, pls note if i use handshaking = 1 the
                            >> application don't start.
                            >> in the delphi configuratio of com port if i use or not handshaking the
                            >> application work.
                            >> Best Regards at all
                            >>
                            >> Luca
                            >>
                            >> import serial
                            >> import win32file
                            >> port = 2[/color]
                            >
                            > Is port = 2 correct?[/color]

                            Read the fine documentation at http://pyserial.sourceforge.net/:

                            Parameters for the Serial class

                            ser = serial.Serial(
                            port=None, #number of device, numbering starts at
                            #zero. if everything fails, the user
                            #can specify a device string, note
                            #that this isn't portable anymore
                            #if no port is specified an unconfigured
                            #an closed serial port object is created
                            baudrate=9600, #baudrate
                            bytesize=EIGHTB ITS, #number of databits
                            parity=PARITY_N ONE, #enable parity checking
                            stopbits=STOPBI TS_ONE, #number of stopbits
                            timeout=None, #set a timeout value, None for waiting forever
                            xonxoff=0, #enable software flow control
                            rtscts=0, #enable RTS/CTS flow control
                            )
                            [color=blue]
                            > I thought that com ports under windows are designated as a string
                            > "com1","com2",. .etc.[/color]

                            You can use those as well, but they're non-portable.
                            serial.Serial(p ort=0) will open the first serial port on either
                            windows or linux.

                            --
                            Grant Edwards grante Yow! Ha ha Ha ha Ha ha
                            at Ha Ha Ha Ha -- When will I
                            visi.com EVER stop HAVING FUN?!!

                            Comment

                            • sam

                              #15
                              Re: Pyserial never read

                              Thanks for the info Grant. It'll teach me not to read the documentation
                              :>)

                              Sam

                              Comment

                              Working...