Serial read ok in Putty but not in Python shell












0















The code I have sends a command to a serial device and returns a string <LF>Mycode<CR> or in Python nMYCODEr is the format I send the command in, I can see the incoming data in putty but not in Python? The incoming data also starts with a <LF> and ends with a <CR>. How do I get this data through to Python?



My code:



import time
import serial
import I2C_LCD_driver

mylcd = I2C_LCD_driver.lcd()

print ("Starting Program")
ser = serial.Serial("/dev/ttyUSB0", baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
time.sleep(1)
try:
ser.write("nWr".encode('utf-8'))
print ("data echo mode enabled")
while True:
if ser.inWaiting() > 0:
data = ser.readline()
print ("Weight", data.decode(), "kg")
mylcd.lcd_display_string("Weight" + data.decode(), 1)




except KeyboardInterrupt:
print ("Exiting Program")

except:
print ("Error Occurs, Exiting Program")

finally:
ser.close()
pass









share|improve this question





























    0















    The code I have sends a command to a serial device and returns a string <LF>Mycode<CR> or in Python nMYCODEr is the format I send the command in, I can see the incoming data in putty but not in Python? The incoming data also starts with a <LF> and ends with a <CR>. How do I get this data through to Python?



    My code:



    import time
    import serial
    import I2C_LCD_driver

    mylcd = I2C_LCD_driver.lcd()

    print ("Starting Program")
    ser = serial.Serial("/dev/ttyUSB0", baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
    )
    time.sleep(1)
    try:
    ser.write("nWr".encode('utf-8'))
    print ("data echo mode enabled")
    while True:
    if ser.inWaiting() > 0:
    data = ser.readline()
    print ("Weight", data.decode(), "kg")
    mylcd.lcd_display_string("Weight" + data.decode(), 1)




    except KeyboardInterrupt:
    print ("Exiting Program")

    except:
    print ("Error Occurs, Exiting Program")

    finally:
    ser.close()
    pass









    share|improve this question



























      0












      0








      0








      The code I have sends a command to a serial device and returns a string <LF>Mycode<CR> or in Python nMYCODEr is the format I send the command in, I can see the incoming data in putty but not in Python? The incoming data also starts with a <LF> and ends with a <CR>. How do I get this data through to Python?



      My code:



      import time
      import serial
      import I2C_LCD_driver

      mylcd = I2C_LCD_driver.lcd()

      print ("Starting Program")
      ser = serial.Serial("/dev/ttyUSB0", baudrate=9600,
      parity=serial.PARITY_NONE,
      stopbits=serial.STOPBITS_ONE,
      bytesize=serial.EIGHTBITS
      )
      time.sleep(1)
      try:
      ser.write("nWr".encode('utf-8'))
      print ("data echo mode enabled")
      while True:
      if ser.inWaiting() > 0:
      data = ser.readline()
      print ("Weight", data.decode(), "kg")
      mylcd.lcd_display_string("Weight" + data.decode(), 1)




      except KeyboardInterrupt:
      print ("Exiting Program")

      except:
      print ("Error Occurs, Exiting Program")

      finally:
      ser.close()
      pass









      share|improve this question
















      The code I have sends a command to a serial device and returns a string <LF>Mycode<CR> or in Python nMYCODEr is the format I send the command in, I can see the incoming data in putty but not in Python? The incoming data also starts with a <LF> and ends with a <CR>. How do I get this data through to Python?



      My code:



      import time
      import serial
      import I2C_LCD_driver

      mylcd = I2C_LCD_driver.lcd()

      print ("Starting Program")
      ser = serial.Serial("/dev/ttyUSB0", baudrate=9600,
      parity=serial.PARITY_NONE,
      stopbits=serial.STOPBITS_ONE,
      bytesize=serial.EIGHTBITS
      )
      time.sleep(1)
      try:
      ser.write("nWr".encode('utf-8'))
      print ("data echo mode enabled")
      while True:
      if ser.inWaiting() > 0:
      data = ser.readline()
      print ("Weight", data.decode(), "kg")
      mylcd.lcd_display_string("Weight" + data.decode(), 1)




      except KeyboardInterrupt:
      print ("Exiting Program")

      except:
      print ("Error Occurs, Exiting Program")

      finally:
      ser.close()
      pass






      python ascii






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 1:35









      Pang

      6,9011664101




      6,9011664101










      asked Nov 20 '18 at 19:24









      BradleyBradley

      247




      247
























          1 Answer
          1






          active

          oldest

          votes


















          1














          It looks like the issue is with your call to ser.readline(). The pyserial API states that it uses io.IOBase.readline which reads characters up to the newline (n) character. Since your data begines with n there are no characters before it, and hence the readline call will read zero bytes from the buffer, and return an empty string.



          You should either move the n to the end of each message, or use the read(n) call directly to read n bytes from the connection.






          share|improve this answer
























          • I have used the ser.readline(). The incoming data was quite large and most of it unused so I added [10:17] to limit the characters to what I require. ser.readling()[10:17]

            – Bradley
            Nov 20 '18 at 21:00











          • Does that make it work then? Otherwise I'd say to use the raw read() method and print the data to console to see what you're actually getting.

            – jdrd
            Nov 20 '18 at 21:02











          • It works yes, but the update is so slow in python but in putty its instant! I can use the read() but I only want to read 7 of 18 characters any idea how to do this?

            – Bradley
            Nov 20 '18 at 21:22











          • So your string slicing above ([10:17]) gives you the characters after the read completes. Putty will display each character individually as it arrives, but with python you decide how often to read and display the data that you receive. If you control the format of the data you receive back you could have each response end with n and just do a readline, then slice the string after.

            – jdrd
            Nov 20 '18 at 21:47











          • That would be the solution but unfortunately the response is preset and can’t be changed

            – Bradley
            Nov 20 '18 at 21:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400138%2fserial-read-ok-in-putty-but-not-in-python-shell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          It looks like the issue is with your call to ser.readline(). The pyserial API states that it uses io.IOBase.readline which reads characters up to the newline (n) character. Since your data begines with n there are no characters before it, and hence the readline call will read zero bytes from the buffer, and return an empty string.



          You should either move the n to the end of each message, or use the read(n) call directly to read n bytes from the connection.






          share|improve this answer
























          • I have used the ser.readline(). The incoming data was quite large and most of it unused so I added [10:17] to limit the characters to what I require. ser.readling()[10:17]

            – Bradley
            Nov 20 '18 at 21:00











          • Does that make it work then? Otherwise I'd say to use the raw read() method and print the data to console to see what you're actually getting.

            – jdrd
            Nov 20 '18 at 21:02











          • It works yes, but the update is so slow in python but in putty its instant! I can use the read() but I only want to read 7 of 18 characters any idea how to do this?

            – Bradley
            Nov 20 '18 at 21:22











          • So your string slicing above ([10:17]) gives you the characters after the read completes. Putty will display each character individually as it arrives, but with python you decide how often to read and display the data that you receive. If you control the format of the data you receive back you could have each response end with n and just do a readline, then slice the string after.

            – jdrd
            Nov 20 '18 at 21:47











          • That would be the solution but unfortunately the response is preset and can’t be changed

            – Bradley
            Nov 20 '18 at 21:52
















          1














          It looks like the issue is with your call to ser.readline(). The pyserial API states that it uses io.IOBase.readline which reads characters up to the newline (n) character. Since your data begines with n there are no characters before it, and hence the readline call will read zero bytes from the buffer, and return an empty string.



          You should either move the n to the end of each message, or use the read(n) call directly to read n bytes from the connection.






          share|improve this answer
























          • I have used the ser.readline(). The incoming data was quite large and most of it unused so I added [10:17] to limit the characters to what I require. ser.readling()[10:17]

            – Bradley
            Nov 20 '18 at 21:00











          • Does that make it work then? Otherwise I'd say to use the raw read() method and print the data to console to see what you're actually getting.

            – jdrd
            Nov 20 '18 at 21:02











          • It works yes, but the update is so slow in python but in putty its instant! I can use the read() but I only want to read 7 of 18 characters any idea how to do this?

            – Bradley
            Nov 20 '18 at 21:22











          • So your string slicing above ([10:17]) gives you the characters after the read completes. Putty will display each character individually as it arrives, but with python you decide how often to read and display the data that you receive. If you control the format of the data you receive back you could have each response end with n and just do a readline, then slice the string after.

            – jdrd
            Nov 20 '18 at 21:47











          • That would be the solution but unfortunately the response is preset and can’t be changed

            – Bradley
            Nov 20 '18 at 21:52














          1












          1








          1







          It looks like the issue is with your call to ser.readline(). The pyserial API states that it uses io.IOBase.readline which reads characters up to the newline (n) character. Since your data begines with n there are no characters before it, and hence the readline call will read zero bytes from the buffer, and return an empty string.



          You should either move the n to the end of each message, or use the read(n) call directly to read n bytes from the connection.






          share|improve this answer













          It looks like the issue is with your call to ser.readline(). The pyserial API states that it uses io.IOBase.readline which reads characters up to the newline (n) character. Since your data begines with n there are no characters before it, and hence the readline call will read zero bytes from the buffer, and return an empty string.



          You should either move the n to the end of each message, or use the read(n) call directly to read n bytes from the connection.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 19:49









          jdrdjdrd

          1297




          1297













          • I have used the ser.readline(). The incoming data was quite large and most of it unused so I added [10:17] to limit the characters to what I require. ser.readling()[10:17]

            – Bradley
            Nov 20 '18 at 21:00











          • Does that make it work then? Otherwise I'd say to use the raw read() method and print the data to console to see what you're actually getting.

            – jdrd
            Nov 20 '18 at 21:02











          • It works yes, but the update is so slow in python but in putty its instant! I can use the read() but I only want to read 7 of 18 characters any idea how to do this?

            – Bradley
            Nov 20 '18 at 21:22











          • So your string slicing above ([10:17]) gives you the characters after the read completes. Putty will display each character individually as it arrives, but with python you decide how often to read and display the data that you receive. If you control the format of the data you receive back you could have each response end with n and just do a readline, then slice the string after.

            – jdrd
            Nov 20 '18 at 21:47











          • That would be the solution but unfortunately the response is preset and can’t be changed

            – Bradley
            Nov 20 '18 at 21:52



















          • I have used the ser.readline(). The incoming data was quite large and most of it unused so I added [10:17] to limit the characters to what I require. ser.readling()[10:17]

            – Bradley
            Nov 20 '18 at 21:00











          • Does that make it work then? Otherwise I'd say to use the raw read() method and print the data to console to see what you're actually getting.

            – jdrd
            Nov 20 '18 at 21:02











          • It works yes, but the update is so slow in python but in putty its instant! I can use the read() but I only want to read 7 of 18 characters any idea how to do this?

            – Bradley
            Nov 20 '18 at 21:22











          • So your string slicing above ([10:17]) gives you the characters after the read completes. Putty will display each character individually as it arrives, but with python you decide how often to read and display the data that you receive. If you control the format of the data you receive back you could have each response end with n and just do a readline, then slice the string after.

            – jdrd
            Nov 20 '18 at 21:47











          • That would be the solution but unfortunately the response is preset and can’t be changed

            – Bradley
            Nov 20 '18 at 21:52

















          I have used the ser.readline(). The incoming data was quite large and most of it unused so I added [10:17] to limit the characters to what I require. ser.readling()[10:17]

          – Bradley
          Nov 20 '18 at 21:00





          I have used the ser.readline(). The incoming data was quite large and most of it unused so I added [10:17] to limit the characters to what I require. ser.readling()[10:17]

          – Bradley
          Nov 20 '18 at 21:00













          Does that make it work then? Otherwise I'd say to use the raw read() method and print the data to console to see what you're actually getting.

          – jdrd
          Nov 20 '18 at 21:02





          Does that make it work then? Otherwise I'd say to use the raw read() method and print the data to console to see what you're actually getting.

          – jdrd
          Nov 20 '18 at 21:02













          It works yes, but the update is so slow in python but in putty its instant! I can use the read() but I only want to read 7 of 18 characters any idea how to do this?

          – Bradley
          Nov 20 '18 at 21:22





          It works yes, but the update is so slow in python but in putty its instant! I can use the read() but I only want to read 7 of 18 characters any idea how to do this?

          – Bradley
          Nov 20 '18 at 21:22













          So your string slicing above ([10:17]) gives you the characters after the read completes. Putty will display each character individually as it arrives, but with python you decide how often to read and display the data that you receive. If you control the format of the data you receive back you could have each response end with n and just do a readline, then slice the string after.

          – jdrd
          Nov 20 '18 at 21:47





          So your string slicing above ([10:17]) gives you the characters after the read completes. Putty will display each character individually as it arrives, but with python you decide how often to read and display the data that you receive. If you control the format of the data you receive back you could have each response end with n and just do a readline, then slice the string after.

          – jdrd
          Nov 20 '18 at 21:47













          That would be the solution but unfortunately the response is preset and can’t be changed

          – Bradley
          Nov 20 '18 at 21:52





          That would be the solution but unfortunately the response is preset and can’t be changed

          – Bradley
          Nov 20 '18 at 21:52


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400138%2fserial-read-ok-in-putty-but-not-in-python-shell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith