473,779 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(p ort=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=1152 00
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
6 4644
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(p ort=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=1152 00
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
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(p ort=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=1152 00
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
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(p ort=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=1152 00
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
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(p ort=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=1152 00
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
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(p ort=1, timeout=None, baudrate=9600)
print s
time.sleep(5)
print "Enter CFG"
s.write('CFG')
print "Change baud"
s.baudrate=1152 00
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
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.pytho n:
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.netc om.com wu******@bestia ria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: we******@bestia ria.com)
HTTP://www.bestiaria.com/
Oct 11 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
43073
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number (or would HDD be better, or both?) and put that info into VB prog so the program won't work on another computer. My program uses an MSAccess table. Much appreciated if you can help! Thanks
18
4893
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
6
3590
by: Casey Bralla | last post by:
I'd like to read ASCII data from a serial port, but (once again) I'm having trouble getting started. (Can't seem to find the basic level of docs to get going <sigh>) I'd like to use only standard "built-in" modules if possible. Could somebody offer a simple code-snippet to get me started reading from a serial port? Thanks!
1
9170
by: ssc | last post by:
I'm new to C#, but have been doing embedded programming for years. I have an application that talks to an embedded radio on the serial port of my PC. I have most of the application running pretty well, but if I click a button before the radio sends its response from the previous command, things get ugly. I need the application to "lock" waiting on a response from the serial port before accepting any more commands be sent. I found the...
1
2176
by: jim.omalley | last post by:
I'm trying to write an interface to send scoreboard data from an XML file generated by a football stats program to a Chyron CODi character generator connected to my COM1 serial port (all operations on same machine). I'm already able to send static commands written into HTTP forms to control the CG via serial COM1. What I'm needing is a way to read and parse variables from the XML, then generate a CODi command string which can be written...
7
4872
by: alexandre_irrthum | last post by:
Hi there, I am trying to use pyserial to read data from a temperature logger device (T-logger). T-logger is based on the DS1615 temperature recorder chip (Dallas Semiconductor). According to the DS1615 docs, writing to the chip is performed one byte at a time. To read from the chip, one must issue the "read page" command (33h), followed by the two-byte address of the requested page (pages are 32 bytes long). After receiving this, the...
6
8163
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql server mobile ce database. Until recently I was synching everything thru a com port serial cable. The devices would connect to the computer thru activesync and are able to acquire an internet connection. The sync for the program occurs thru a website...
0
1459
by: darkking | last post by:
Ok, I'm trying to read data from the serial port. Problem is, that i always have to query the port for new data, and in 99.9% of acses my query doesn't return the result cause of the fact it's done when the modems is seding the answers. I've tried using a timer, but still i get errors. Same while using DTR enable/false. How i can safely send the query without getting over the momdem response ? I'm using DataRecived event, to read the...
2
10403
by: mmrasheed | last post by:
Hi, I am newbie in python. I am working on Telit GM862 GPS/GPRS module which has python interpreter built in. But it seems this problem is pretty much related to general python structure. I need a promt/terminal when the device is connected to PC. If user enters a command by serial port and press "Enter" then the data is read by the device and work on the command. This is similar to readline() function. Unfortunately there is no...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.