NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » [Help] Make file python listener and order of operations

October 10, 2009
by Tug
Tug's Avatar

I've been trying to figure out how to make my led array work in sync with my vista pc. I am new to the programing world. I've downloaded PuTTy (hyper-thread for vista). No problem, COM5 at 115200. Then I've installed pySerial. After I modified the make file to look like that :

GCCFLAGS=-g -Os -Wall -mmcu=atmega168 LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P COM5 LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o

all: ledarray-upload

ledarray.hex: ledarray.c make -C ../libnerdkits avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o ledarray.o ledarray.c ${LINKOBJECTS} avr-objcopy -j .text -O ihex ledarray.o ledarray.hex

ledarray.ass: ledarray.hex avr-objdump -S -d ledarray.o > ledarray.ass

ledarray-upload: ledarray.hex avrdude ${AVRDUDEFLAGS} -U flash:w:ledarray.hex:a

"Make" it through cmd. It lights all the leds, and says "Hello". Everything hooked up to the pc. If I run putty and python-listener.py (modifed COM5 and 115200), nothing happens.

Is the atmega supposed to be in programing mode or running the script? Is the python listener supposed to be coded on the atmega as well? aka i am missing files in the make.

I've looked through the servo tutorial/led array but I am stuck now.

October 10, 2009
by mcai8sh4
mcai8sh4's Avatar

Hi Tug,

I'm not certain the exact steps with Vista, but the general plan is along these lines...

You've uploaded the code ok, as you are getting the test pattern, so your MCU is fine.

Then you run python-listener.py (on a windows command line I guess) and it will sit there, seemingly doing nothing. Then you can use putty or some other method to send your text to the port that python-listener is on (I think it's localhost port 6667) then the listener will recieve your text then send it on to the chip through the com port.

You are really close to getting everything working!

If you still have problems (I struggled with this initially), then you coulds try some of the other workarounds I did. Basically I just altered the python code to work differently.

Firstly, I have made 2 python programs, one sends text to display statically on the screen, save this as py-static.py....

#!/usr/bin/python
#
# Display static text on the LED Marquee
# using the nredkit
# -mcai8sh4 04/2009
import serial
import sys
import time

serial = serial.Serial("COM5", 115200)
mystring = sys.stdin.readline()
serial.write('a')
serial.write('z')
serial.write(mystring)
time.sleep(5)
serial.write('z')
serial.write('\n');

then to use it, try py-static.pl HELLO

I think that might work.

Then there's a scrolling version :

#!/usr/bin/python
import serial
import sys

serial = serial.Serial("COM5", 115200)
mystring = sys.stdin.readline()
scrollMode = True
lastTime = ''
serial.write('b')
for data in mystring:
    if not data == '\n':
                    serial.write(data)
                    response = serial.read()
                    if not response == 'n':
                            print "Received: " + response
                            #This is a debug line, since I'm a bit fuzzy on some details
                            break
serial.write('a')

save as py-scroll.py and invoke on the command line like this : py-scroll HELLO

I know this doesn't help with your py-listener problem, but it should get you started displaying stuff.

Note: you may have to alter the port in the program to suit your setup.

I have used Vista to display to the screen, but that utilized some other programs (that do the same as above) wrote in perl. If you want to try that method too, read the last post in the following thread :

http://www.nerdkits.com/forum/thread/148/

Good luck.

-Steve

October 10, 2009
by rusirius
rusirius's Avatar

Yeah, Steve is on the right track... I couldn't tell from your post since you didn't mention WHAT you were trying to connect to with putty... Make sure you're connecting to "localhost" or 127.0.0.1 on the right port... 6667 I believe... In other words, don't try to connect both putty and the python script to the com port...

Also, generally localhost would be open for anything, but make sure your firewall isn't blocking the connection...

October 11, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Hello, Your in the same boat i was in 6 mo's ago, only difference your vista i was xp. Sure there's other ways to get stuff going across your array the soundmeter is another one, but none will give you the satisfaction of getting the listener running sports, headlines or weather type feed. I worked on it for quite a while with Humberto guiding me (he hadn't tried it on windows) but what the bottom line is, is linux. Don't know why they don't come right out and tell the public python-listener.py will not run on windows,is why i'm here and their there. About a month later i had it started. So i didn't know squat about linux before i started and if you decide to go that way get a cheap secondhand comp don't mess with the partition stuff you'll need to keep windows the way it is. Linux is malware to windows on the same machine. Linux is supposed to be better for developing but with all the password administration crap i have my doughts.I'll never be a complete linux user, windows all the way for this cowboy.

fjc

October 11, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Well since going to Kubuntu i read that it might be possible to run python-listener.py by changing the .py to a .exe. I spent the entire afternoon trying to load the py2exe on my windows machine. Directions straight from their website and there's no way that i could load py2exe to be a command. All the directions were bogus, either that or there's some crisscrossing of numerous python installs, uninstalls and just plain abuse from trying to get python-listener.py running. Windows wasn't working today like Kubuntu does most day's. Another one of them that JUST DON'T MAKE SENCE. There i feel better. I can offer you this code just copy it into wordpad name it something you'll recognize with a .py on the end. Save it in the python folder, cd to that folder enter python "name of your .py script" enter. That text will scroll like that untill you shut the command line down. What's good about this script is that you can add anything you want to it whatever length you want "in capitals" between the" ".

!/usr/bin/python

import serial import sys

serial = serial.Serial("com5", 115200) while True: mystring = "HELLO TUG,//HOW'S IT GOING TODAY?///I HOPE YOU ARE WELL//>>>::: n" serial.write('b') for data in mystring: if not data == 'n': serial.write(data) response = serial.read() if not response == 'n': print "Received: " + response #This is a debug line, since I'm a bit fuzzy on some details break

This might keep you going until the second comp arrives.

fjc

October 11, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Looks like your going to half to format it. Look at the above code and copy that format. More unknown in the unknown.

fjc

October 12, 2009
by Tug
Tug's Avatar

You've made me noticed I was using PuTTy wrong. I was trying to connect to COM5, instead of lcoalhost (because in the ledarray tutorial it says to do it that way). BUT when I do localhost (or 127.0.0.1) port 6667. It says it cannot connect. I have my firewall disabled, and my anti-virus turned off. <br />

Do you HAVE to put the ***.py files every time in the python26 folder?

About Linux, this there a version that is not compatible? Or anything is fine? I was thinking about getting a EEE pc, but I've heard they were a light version of Linux, could that be a problem?

Farmerjoecoledge, you are missing a # in the beginning for "!/usr/bin/python" Here is how I formated it, but it is wrong as it keeps crashing..

alt image text

October 12, 2009
by mcai8sh4
mcai8sh4's Avatar

What error are you getting when you try to run your code?

You should be able to put your python files anywhere, to run them you might have to use "python myprog_name.py"

October 12, 2009
by apurcell
apurcell's Avatar

I am not familiar with the LED project, but it seems there is some confusion over connecting to the serial port vs. a network port. None of the code shown in this thread shows me anything about opening a network socket (aka port, in this thread 6667). To check on your PC (Windoze or Linux) run the following from the ole command prompt...

netstat -an

That will list all active connections, in addition their state. Odds are you will see things like IP addresses, ESTABLISHED, CLOSE_WAIT et-al. To see if your computer is set up to 'listen' and accept connections on a particular port look for LISTEN in the last column of the output. Below is a sample from my computer.

tcp        0      0 :::22                   :::*                    LISTEN

The fourth column :::22 (in this case port 22) indicates the port that is listening for connections. Optimally if your program is set up to run as a network service you should see a line like this in your netstat table.

If not, then you are probably only working over the serial port. Make sure you also set the following settings in PuTTY...

Data bits: 8 
Parity: None 
Stop bits: 1
Flow Control: none

Hope this helps.

Regards, Anthony

October 12, 2009
by mcai8sh4
mcai8sh4's Avatar

Hi Anthony,

You're right, we should have pointed this out initially. So here goes...

The python-listener.py script listens on a port for text to display. Unfortunately there are problems getting this to work properly in windows. So the code(s) above remove the port listening part, and just feeds the text across from the script (or argument).

So the additional codes don't need putty at all.

Hope that makes sense.

Steve

October 12, 2009
by rusirius
rusirius's Avatar

mcai8sh4 and Farmerjoecollege,

What do you mean the listener won't run on windows? It runs just fine... I've run it on both XP and just now Vista... I had run it on an XP machine before and when I saw this I thought maybe it was a Vista issue and decided to check it out... (Vista Business on a Fujitsu Lifebook) I fire it up and netstat -a reveals it's listening on 6667... A simple "telnet localhost 6667" connected me and everything was kosher...

I can't imagine what the issue could be... The only thing I can think of is to make sure you downloaded/installed both the PyWin and PySerial add-ons on top of the standard Python install...

I mean don't get me wrong... Linux is great... I run several servers on it, but for some it just isn't practical... I can't IMAGINE the looks on my users (or Local USERS (lusers) as we like to call them) if they walked in one day and fired up their desktops to have X stare at them... ;) Just for spite I'd have to load up FVWM... ;)

Anyway, here's what I would suggest Tug... Make sure both the Pywin and Pyserial are installed... Then right click the listener script and choose "Edit in IDLE"... I had to fix an ident that didn't translate well, just use the "Unident" option under format, then you'll have to tab it over to the correct position (Python is very specific about ident levels). Go to the top of the file and chance the /dev/TTYUSB0 bit to "COM5" or whatever com port the USB to serial adapter is on... Then choose the "Run Module" option from the Run menu... It should fire up and you'll see the "IDLE>>" prompt... Go to a cmd window and type "netstat -a" and you should see a port listed with 6667... Now try connecting putty to localhost port 6667... (Idle should report a connection from localhost) and you should be good to go...

October 12, 2009
by rusirius
rusirius's Avatar

Just to make things even easier, here are the links to Pywin and Pyserial

http://sourceforge.net/projects/pywin32/

http://sourceforge.net/projects/pyserial/files/

October 13, 2009
by mcai8sh4
mcai8sh4's Avatar

rusirius : You, sir, are the dude! I haven't been able to try the listener on windows (Whilst I have xp on a few machines, and vista on one - they are all located around the house and not where I do my 'playing').

I agree with your feelings about OS's, although I prefer linux, I use windows extensively for my business. To me it's all about the 'right tool for the job' (and user preference!)

I'm going to take my marquee to work and set up up there just to prove you're right :)

-Steve

October 13, 2009
by rusirius
rusirius's Avatar

LOL... Yeah, exactly... Linux is fantastic for tinkerers... Hell, I'll never forget many many years ago when I switched from FreeBSD to Slackware... In those days there was no such thing as a "module"... If you wanted the kernel to support something you compiled it in... I musta spent like 8 hours straight trying to get all the libraries and stuff installed correctly just to get gcc working so I could compile the thing... Let alone actually getting it to compile sucessfully... LOL...
Point is, even though the package managers and stuff have taken a lot of the "fun" out of it, for people like us that like to tinker, and get a thrill out of overcoming the challenges, it's great... For servers there is nothing that feels better than stripping a kernel down to it's absolute bare minimums required for the tasks the server will perform... When you fire up top and see the entire system taking about 2MB of 32GB of ram, that's something to grin about... BUT... I'll never forget all those foos preaching the end of the Microsoft empire back when Redhat was first starting to become a little better known and popular... How windows would completely be replaced, etc... Of course most of the people that were preaching hadn't been running it for more than about a week themselves and were just happy they managed to get mozilla running... LOL...

I know ... I rant I rant... ;) forgive me... I still say the little SparcStation IPX (think shoebox) I had running my smtp/pop server rulled the world! ;)

Anywho... If you have any issues let me know... I'd love to figure out what the issue is if there is one...

October 14, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

CORRECTION: Seems i'm wrong AGAIN! It was 6mo's ago i quit trying to get the listener running on windows and switched to linux. You know i don't remember editing the listener.py. I think the IDLE is what made the diffrence. THANK YOU rusirious now i can go about getting windows back on this machine too. Yes my linux days, of pure torment are over, rejoice. No more of some linux os telling me "permission denied." drive me nuts.

October 16, 2009
by Tug
Tug's Avatar

All the codes that don't need the listening part work just fine now.

But every time I launch python-listener it makes the python shell in "unresponsive mode".

I did the cmd netstat -a ; didn't find "LISTEN", but "LISTENING", 6667 is not listed.

alt image text

So I picked port 135.

alt image text

In PuTTY, should I be changing the Serial to COM5 115200 ? or leave it to COM1 9600? aka is the screen shot right?

I did have pywin32 and pyserial installed. But I uninstall every python related software and reinstalled it with no better results.

Does this part of pyton-listener.py need to be changed ? s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 135)) <---- 6667 ? 135? 445? 22? port I am going to be using? Because when I change it, the shell to one of the "LISTENING" port it says that usually only one connection is allowed.

Well I keep looking for the bug, hopefully something stoopid.

October 17, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Did you get it figured? Yeah we talked about it a little bit on the wayward post, but get python-listener running and it just hangs there and then you open a new terminal and enter nc localhost 6667 which will open the port and then your text and enter.The only editing of the listener is the com5. The rest of the script is ok, no indent errors. If you don 't got netcat download it for windows. I never used putty until you mentioned it nc is just another way to send stuff to the array. You need it for a pipe.

October 18, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Hey Tug you still home? Just need you to tell me how you did your pic links. thx

fjc

October 19, 2009
by Tug
Tug's Avatar

I haven't had time to work on this lately, college is keeping me pretty busy. But I am off next week, so plenty of time hopefully to finish this. Anyways, to add pictures to a thread just put the tag

  ![alt image text](http://somepath.com/path/to/img.jpg)

I hosted my pictures on a friend's website. But photobucket will do just fine. I actually messed up my links by forgetting to put "!" in front of the code.

October 20, 2009
by hevans
(NerdKits Staff)

hevans's Avatar

Tug,

When you run pyhton-listener.py, the command line will appear to just sit there because it is just sitting there waiting for a connection. You will not be sure that it is not working until you try to connect to it with something.

You should not be trying to run putty in this situation. In this case, the python-listener script is talking to the chip on the serial port (COM5). You then need to send data over a socket to the python-listener script. We did this with Netcat on our tutorial, but you can use any program that sends data over a socket (or write your own?). Connecting to a socket is easy in Python, or Java, or if you are into it C++ Hope that helps.

Humberto

October 20, 2009
by rusirius
rusirius's Avatar

To clarify what Humberto said, you don't NEED to use putty to connect to the socket once it's listening... But you CAN use Putty to do it.. I think Humberto is erring on the side of caution here to avoid confusion... Putty can connect to a serial port OR to a socket... If you're on vista for some reason it won't be installed by default, I think you have to specify it in the add/remove somewhere, but even the simple telnet client is also an option... just telnet locahost 6667...

Now.. On to your port issue... You can't just choose any old port, especially not 135... Actually, you can choose any port if you modify the script and make sure it doesn't already have something binding to it (i.e. listening), but you can not (in general) use ports below 1024 as they are usually restricted...

I need to point something out from your images... Forgetting the fact that you're trying to connect to 135, you also have SSH selected... (secure shell)... You cannot use that as it tries to establish an encrypted connection to the socket... You need to change it to connect using "Telnet" which will open a straight connection to the port...

The serial connection info in Putty is irrelavent... You're not connecting via serial so those settings won't make any difference...

I'd suggest this... Go back to my post from the 12th and read through the last section... Do those things... By using IDLE you'll be able to get a little more feedback about what's happening... If you get an error when you run it, then let us know what the error is... If nothing happens and you just get the >> prompt, then you should be good to go... Try to connect to port 6667 via TELNET and see what happens...

Also, since you've made some changes to the script I'd unzip a clean python script and start from there...

October 24, 2009
by Tug
Tug's Avatar

First of all, a big thank you to all the people that help because it WORKS !!

Here is how I did it: Modified python-listener (COM 5 115200, and 6667)

Launch the program by clicking on it! not by going into the IDLE and pressing F5, doing that made it go into 'not responding' mode.

I downloaded netcat from : http://joncraton.org/files/nc111nt.zip went into CMD and typed 'nc.exe' -> 'nc localhost 6667'

Then a magical sentence appeared on the python terminal 'Connected by <127.0.0.1 49429>' (did it change the port on me?)

Still in the CMD I can now type letters and numbers (press enter) and they will appear on my led array.

I am saying PuTTy didn't work for me and I will not try to make it work now that I have netcat.

If anybody needs help now, I can give a hand.

Post a Reply

Please log in to post a reply.

Did you know that you can build a circuit to convert the "dit" and "dah" of Morse code back into letters automatically? Learn more...