473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pyserial vs Win32 CreateFile()

9
Hi,

I am new to Python. Very interested in controlling things (microcontrollers) via the serial port (or usb, parallel). I thought I would start by learning about pyserial. I have seen some examples here and there on the web and the sourceforge examples.

I'm using windows xp.

Wondering if anyone has any very simple examples of using pyserial. And if anyone can help explain each line of code in a very simple way?

Would pyserial be the easiest way to go?

(Also, are there any books out there that explain how to use access PC ports to send data to microcontrollers?)

Sincerely appreciate any info, advice anyone can offer.

Thanks in advance,
Tony
Apr 11 '07 #1
12 6765
dshimer
136 Expert 100+
I'm almost afraid this may be too simple. I never even really learned how to use this module because just playing around, it worked so easily and I haven't looked at it for quite some time. Just created an instance of a serial object that apparently works just like a file.
My device has some control codes for moving to specific positions.

Expand|Select|Wrap|Line Numbers
  1. import serial
  2.  
  3. def main():
  4.     ser=serial.Serial(0)
  5.     #Create the serial port instance for port 1
  6.     ser.baudrate=19200
  7.     #Set the baud rate and write away
  8.     ser.writelines('MM1;\r\n')
  9.     ser.writelines('LA140.00,0.00;\r\n')
  10.     ser.writelines('MM2;\r\n')
  11.     ser.writelines('RA100.00,0.00;\r\n')
  12.     ser.close()
  13.  
  14. main()
Apr 11 '07 #2
TonyAm
9
That's great. Thank you.

I need to see some extremely simple code, so I can start to understand it. I have only done some programming (if you can call it programming) of Pic microcontrollers using PicBasic Pro, which is extremely easy to use.

Been looking into other languages like VB 2005 and Java, actually started to learn them. But realized these languages are probably better suited for someone who wants to program as a career, working for companies, etc.

I make MIDI controllers that use the Pic18F452. For awhile now I have been exploring ways of interfacing the Pic with the pc. I came across info about Python being easy for beginners to pick up, described as having very "clean" code, and being fun. With VB and Java, although I did learn the basic concepts of OOP, the code of both just looks like a convoluted mess to me. A lot of code to do something simple.

Python, at least initially looks better. Does this make sense? (I've heard a saying that goes something like; "simplification is an essential element to gauge the progress of development..."

These other languages seem to delibrately obscure, or remove components (or make using things unnecessarily difficult in order to get you to buy their junk, or to become completel reliant on their "products".)

I think my next step is to throw the Windows right out the window. Here is a company filled with geniuses, for some reason they keep making their product more and more crappy. How could they have sold millions of copies on XP with all of those security problems? Was there a deliberate reason?

I'm starting to see why Linux and Python would probably be a much better way to go. made by the people, for the people.

I apologize for my rant. Thank you very much for sharing that code, and the comments. I appreciate it very much.

Tony
Apr 11 '07 #3
bartonc
6,596 Expert 4TB
Hi,

I am new to Python. Very interested in controlling things (microcontrollers) via the serial port (or usb, parallel). I thought I would start by learning about pyserial. I have seen some examples here and there on the web and the sourceforge examples.

I'm using windows xp.

Wondering if anyone has any very simple examples of using pyserial. And if anyone can help explain each line of code in a very simple way?

Would pyserial be the easiest way to go?

(Also, are there any books out there that explain how to use access PC ports to send data to microcontrollers?)

Sincerely appreciate any info, advice anyone can offer.

Thanks in advance,
Tony
I wrote an entire IDE for the Motorola 68HC11 using Python. It has a Tkinter UI for talking to the serial port. Back then, I used the Win32 package to get serial port access, but would probably use pyserial, now. I can make the code available, if you're interested.
Apr 11 '07 #4
dshimer
136 Expert 100+
Rant aside, if you want clear, concise, easy to understand, power, python has much to recommend it. Stick around here and you won't lack for help from some of the local geniuses. I "play" with coding to automate tasks and get out of actually having to work, some of the guys in this forum amaze me.
Apr 11 '07 #5
TonyAm
9
I wrote an entire IDE for the Motorola 68HC11 using Python. It has a Tkinter UI for talking to the serial port. Back then, I used the Win32 package to get serial port access, but would probably use pyserial, now. I can make the code available, if you're interested.

Hi,

Thank you for the info. I would love to take a look at your code. I have seen one of the O'Reilly books that describes using the Win32 package.

What are the advantages of using pyserial over Win32?

Any advantages of using Win32 over Pyserial?

Thanks again,

Tony
Apr 11 '07 #6
bartonc
6,596 Expert 4TB
Hi,

Thank you for the info. I would love to take a look at your code. I have seen one of the O'Reilly books that describes using the Win32 package.

What are the advantages of using pyserial over Win32?

Any advantages of using Win32 over Pyserial?

Thanks again,

Tony
Give me a little while to dig up the old files and zip them. I'll then attach them here.

As long as you are thinking "Windows only" Win32 is OK, but you need some "Inside Windows" knowledge. I used it because it's all that was available at the time. I don't know if PySerial is cross platform or not.
Apr 12 '07 #7
bartonc
6,596 Expert 4TB
Give me a little while to dig up the old files and zip them. I'll then attach them here.

As long as you are thinking "Windows only" Win32 is OK, but you need some "Inside Windows" knowledge. I used it because it's all that was available at the time. I don't know if PySerial is cross platform or not.
As I recall, the debugDialog1.py or similar name is the one using background serial com in it's own thread. That thread is polled by the GUI to get data.
Attached Files
File Type: zip bc11asm.zip (86.2 KB, 293 views)
Apr 12 '07 #8
TonyAm
9
I was able to access my serial port using the very simple example from the pyserial sourceforge page:


import serial
ser = serial.Serial(2) #open virtual serial port #3

print ser.portstr #check which port was realy used
ser.write("hello") #write a string to com port 3
ser.close() #close port

print "\nThe com port is open"
raw_input("\n\nPress the enter key to exit.")

Now, I'm wondering how I can receive serial data to the same port?

Thanks for the help/assistance. Ver much appreciated.

Tony
PS. Thank you for posting the code for the Win32 example. Will be going through it soon and comparing.

Thanks again.
Apr 13 '07 #9
TonyAm
9
I was able to access my serial port using the very simple example from the pyserial sourceforge page:


import serial
ser = serial.Serial(2) #open virtual serial port #3

print ser.portstr #check which port was really used
ser.write("hello") #write a string to com port 3
ser.close() #close port

print "\nThe com port is open"
raw_input("\n\nPress the enter key to exit.")


Now, I'm wondering how I can receive serial data to the same port?

Thanks for the help/assistance. Very much appreciated.

Tony
PS. Thank you for posting the code for the Win32 example. Will be going through it soon.

Thanks again.
Apr 13 '07 #10
modal
2
I just started using Python last week to do some website scripting and it worked great.

I thought this pyserial module might be useful in some simple rs232 applications. I typically write programs in C/C++. Reason I (think) like Python is I can quick do simple one off projects to test ideas.

My questions is: How do you send straight binary data with this module? It seems to me that it is set up for ASCII data only
May 1 '07 #11
modal
2
Okay I found how to do it from a post on another website.


buffer = [0x0, 0x33,0x34,0x55]
buffer = [chr(i) for i in buffer] #convert hex to characters
May 1 '07 #12
bartonc
6,596 Expert 4TB
Okay I found how to do it from a post on another website.


buffer = [0x0, 0x33,0x34,0x55]
buffer = [chr(i) for i in buffer] #convert hex to characters
Thanks for the update, modal.
May 2 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and...
13
by: Bob Greschke | last post by:
We have some equipment that communicates at 57600 baud RS232. The path from the PC is USB to a Phillips USB hub, then off of that a TUSB3410 USB/Serial converter. The driver for the 3410 chip...
2
by: Jon | last post by:
Hi, I wrote some code to read in info from a port using pyserial. the code reads info sent by a box that is connected to my computer by an rs232-to usb adapter. When I was writing the code and...
3
by: Ron Jackson | last post by:
I am using Python 2.5 on Windows XP. I have installed Pyserial and win32all extensions. When I try to run the example program scan.py (included below), or any other program using pyserial, as...
0
by: pauland80 | last post by:
<snip> <snip> Late thanks for your both answers! (Please excuse me for that) The problem was a bug in the device firmware. But before finding this, I dugg lightly in the pyserial source...
0
by: [david] | last post by:
http://pyserial.sourceforge.net/ "port numbering starts at zero, no need to know the port name in the user program" But the implementation in SerialWin32 is just (Portnum +1)
3
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
1
by: JAMoore84 | last post by:
Hi Guys, I have a project where I'd like to save GPS data that is streamed to a Sony Vaio over bluetooth. I can monitor the data stream over Hyper Terminal, but I'd like to use python to...
6
by: Joe G \(Home\) | last post by:
Hi All, Background =================== I have installed Python for windows today from the python web site .I also installed pySerial using the Windows installer from the sourceforge web...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.