By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

Read data from Serial Command

P: n/a
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(port=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=115200
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
Oct 10 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 2008-10-10, brianrpsgt1 <br*******@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(port=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=115200
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 ...
Oct 10 '08 #2

P: n/a
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 <br*******@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(port=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=115200
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 ...
Oct 10 '08 #3

P: n/a
On 2008-10-10, brianrpsgt1 <br*******@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(port=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=115200
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:

http://pyserial.svn.sourceforge.net/...rial/examples/

--
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
Oct 10 '08 #4

P: n/a
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 <br*******@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(port=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=115200
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:

http://pyserial.svn.sourceforge.net/...rial/examples/

--
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
Oct 10 '08 #5

P: n/a
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 <br*******@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(port=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=115200
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:

http://pyserial.svn.sourceforge.net/...rial/examples/

--
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
Oct 10 '08 #6

P: n/a
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
<br*******@cox.netdeclaimed the following in comp.lang.python:
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
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/
Oct 11 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.