Serial read ok in Putty but not in Python shell
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
add a comment |
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
add a comment |
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
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
python ascii
edited Nov 26 '18 at 1:35


Pang
6,9011664101
6,9011664101
asked Nov 20 '18 at 19:24
BradleyBradley
247
247
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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 rawread()
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 withn
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
|
show 1 more comment
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 rawread()
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 withn
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
|
show 1 more comment
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.
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 rawread()
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 withn
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
|
show 1 more comment
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.
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.
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 rawread()
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 withn
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
|
show 1 more comment
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 rawread()
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 withn
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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