[portland] Python Frontend to Microcontoller demo

Ron Jackson Ron at FascinatingElectronics.com
Tue Jul 17 07:12:04 CEST 2007


Jeff,

Thanks for your comments! Glad to hear the code looks Pythonic!

The Serial method is peculiar in that you can specify a port by a number from 0 
to 255 (corresponding to COM1 to COM256), or by name 'COM1' to 'COM256'. Though 
apparently the name required for COM10 and above is of the form '\\\\.\\COM10'. 
  Method makeDeviceName(9) produces the string above. What the story with all 
the '\' is, I'm not fully sure. Some Windows thing, I think.

If the Serial method requires all those '\' for ports over 9, then I'll just 
take the user input and convert it to an integer, subtract one, then pass that 
to open the instance of Serial.

The fallback code does work as presented, in that it prints out the names of all 
of the available commports.

I would like to support both the new USB-based modules and the existing RS-232 
based product (the Experimenter) with the same extension of PySerial. I'd like 
to keep it reasonably simple for these examples. I'll look into Twisted when I 
get further along. It sounds interesting.

   -- Ron


Jeff Rush wrote:
> Ron Jackson wrote:
> 
>>Thanks for taking a look at my demo of a Python program communicating with a 
>>USB-enabled microcontroller! I've attached the code I used.
> 
> 
> Cool!
> 
> 
>>I'd appreciate feedback on this code from more experienced Python programmers, 
>>particularly to use the favored Python programming style.
> 
> 
> You have a tiny bug in the fallback code where a COM number is not entered. 
> You need to change serial.Serial(i) to serial.Serial('COM' + i), as you did in 
> the try clause agove that.
> 
>              for i in range(256):
>                  try:
>                      s = serial.Serial(i)
>                      print s.portstr,
>                      s.close()
>                  except serial.SerialException:
>                      pass
> 
> Overall the code looks fine, very Pythonic in style so far.  Although I'm not 
> in Portland, I look forward to seeing where you go with this.
> 
> Looking over your website, FascinatingElectronics.com, I see you do really 
> neat controllers.  For more complex serial protocols, you might want to look 
> into the Twisted Framework for Python (http://twistedmatrix.com/).  It 
> provides some nice asynchronous event processing facilities, where you can 
> easily design your own serial protocol and interact with it cleanly.  It takes 
> some work to understand the async way of programming but once you do, it is 
> very powerful.
> 
> -Jeff
> _______________________________________________
> Portland mailing list
> Portland at python.org
> http://mail.python.org/mailman/listinfo/portland
> 
> 

-- 
Website:  http://www.FascinatingElectronics.com
Fascinating Electronics Inc, 925 SW 83 Avenue, Portland, OR 97225-6307
Toll Free: 800.683.5487    Direct Dial: 503.296.8579


More information about the Portland mailing list