
October 10th, 2008, 10:05 PM
| | | Read data from Serial Command
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 | 
October 10th, 2008, 10:25 PM
| | | Re: Read data from Serial Command
On 2008-10-10, brianrpsgt1 <brianlong@cox.netwrote: Quote:
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? Quote:
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 ... | 
October 10th, 2008, 10:25 PM
| | | Re: Read data from Serial Command
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: Quote:
On 2008-10-10, brianrpsgt1 <brianlong@cox.netwrote: Quote:
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?
> Quote:
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 ...
| | 
October 10th, 2008, 11:05 PM
| | | Re: Read data from Serial Command
On 2008-10-10, brianrpsgt1 <brianlong@cox.netwrote: Quote:
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? Quote: Quote: Quote:
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) 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 | 
October 10th, 2008, 11:45 PM
| | | Re: Read data from Serial Command
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: Quote:
On 2008-10-10, brianrpsgt1 <brianlong@cox.netwrote: Quote:
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?
> Quote: Quote:
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)
>>
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
| | 
October 10th, 2008, 11:45 PM
| | | Re: Read data from Serial Command
Again, what is weird is that all works fine in Hyperterminal, but not
with the Python script.
brianrpsgt1 wrote: Quote:
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: Quote:
On 2008-10-10, brianrpsgt1 <brianlong@cox.netwrote: Quote:
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? Quote:
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) 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
| | | 
October 11th, 2008, 01:05 AM
| | | Re: Read data from Serial Command
That did it! The fix was the '\r'
Thanks for the assistance Dennis and Grant!
Dennis Lee Bieber wrote: Quote:
On Fri, 10 Oct 2008 15:40:08 -0700 (PDT), brianrpsgt1
<brianlong@cox.netdeclaimed the following in comp.lang.python:
> Quote:
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?
> Quote: Quote:
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 wlfraed@ix.netcom.com wulfraed@bestiaria.com HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com) HTTP://www.bestiaria.com/ | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|