Pyserial vs Win32 CreateFile()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TonyAm
    New Member
    • Apr 2007
    • 9

    Pyserial vs Win32 CreateFile()

    Hi,

    I am new to Python. Very interested in controlling things (microcontrolle rs) 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 microcontroller s?)

    Sincerely appreciate any info, advice anyone can offer.

    Thanks in advance,
    Tony
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    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.

    Code:
    import serial
    
    def main():
    	ser=serial.Serial(0)
    	#Create the serial port instance for port 1
    	ser.baudrate=19200
    	#Set the baud rate and write away
    	ser.writelines('MM1;\r\n')
    	ser.writelines('LA140.00,0.00;\r\n')
    	ser.writelines('MM2;\r\n')
    	ser.writelines('RA100.00,0.00;\r\n')
    	ser.close()
    
    main()

    Comment

    • TonyAm
      New Member
      • Apr 2007
      • 9

      #3
      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 microcontroller s 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; "simplifica tion 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

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by TonyAm
        Hi,

        I am new to Python. Very interested in controlling things (microcontrolle rs) 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 microcontroller s?)

        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.

        Comment

        • dshimer
          Recognized Expert New Member
          • Dec 2006
          • 136

          #5
          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.

          Comment

          • TonyAm
            New Member
            • Apr 2007
            • 9

            #6
            Originally posted by bartonc
            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

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by TonyAm
              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.

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by bartonc
                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

                Comment

                • TonyAm
                  New Member
                  • Apr 2007
                  • 9

                  #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("hell o") #write a string to com port 3
                  ser.close() #close port

                  print "\nThe com port is open"
                  raw_input("\n\n Press 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.

                  Comment

                  • TonyAm
                    New Member
                    • Apr 2007
                    • 9

                    #10
                    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("hell o") #write a string to com port 3
                    ser.close() #close port

                    print "\nThe com port is open"
                    raw_input("\n\n Press 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.

                    Comment

                    • modal
                      New Member
                      • May 2007
                      • 2

                      #11
                      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

                      Comment

                      • modal
                        New Member
                        • May 2007
                        • 2

                        #12
                        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

                        Comment

                        • bartonc
                          Recognized Expert Expert
                          • Sep 2006
                          • 6478

                          #13
                          Originally posted by modal
                          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.

                          Comment

                          Working...