Tutorial: Read Serial data in Windows 10 using Python 3

Written as part of week16 - interface and application programming

  1. Download the latest version of python and install it.

  2. Open a command prompt, you do this either by pressing Win key + R and running the command cmd. Or use the GIT BASH shell from week1.
  3. If you are lucky Python managed to add itself to the windows PATH environment, meaning that you can run it from the command line from anywhere in your system. Test it in your command prompt by running the command python If it is installed correctly it should respond like this:

    We need pip for adding libraries. Test if it is properly installed by running the command pip

    If you can run both python and pip witout error then you can skip the next step.
    EDIT: IF YOU HAVE THIS PROBLEM, TRY RESTARTING YOUR PC AND TESTING AGAIN

  4. If you couldn't run python or pip in the previous step we can add them manually.

    We do this by add the folder containing python.exe it AND the scripts folder containing pip.exe to the PATH environment so that you can run them from the command line anywhere in your system. Press the windows-key and type path:

    Close any command prompts that you have open so that it can refresh.

  5. To run the serial tutorial (backup of the original code) from Fab Academany you need to first install the Serial library.

    Extended tutorial for installing python libraries

  6. Connect your PC to something that can send serial data, for example use an FTDI cable and your Hello button board from Electronics design week6.

    Optional:
    Check that we have serial data incoming. I used the Arduino IDE and the Serial Monitor for this since I know that it is working.

  7. Test which serial port you are connected to by opening a command promt (Win key + R, type cmd) and running

    python -m serial.tools.list_ports

    This will list connected serial ports like this:

  8. Test recieving the serial data through the Python termial. In the command promt, start the python miniterminal by running:

    python -m serial.tools.miniterm

    Select the correct port, in my case i choose Nr 1, the USB Serial port (COM4) that is my FDTI cable.

    The text "$J=X10F100" is serial data coming from my jogging card I made in input devices week13.

    For scandinavian keyboards the Quit command is Ctrl+^ instead of Ctrl+]

  9. Using a Python program to capture the serial data

    The serial tutorial code did not work straight away for me. I suspect that the syntax (the way you write the code) has changed since it was written. Nor did I get the rx.py code that Neil provided in the lecture notes for Input devices to work.

    Here is my modified version of the serial tutorial code.

    Download it, right click and select Edit with IDLE to open in the Python code editor.

  10. It is setup to work with a board connected with FTDI cable which Windows sees as 'COM4'. Replace 'COM4' with whatever Step 7. told you that your connection is called.

  11. In IDLE, press Run->Run Module or just press the F5 key. This should open a Python Shell window and you should be able to recieve serial data like this:

  12. You can now add code to the program to make use of the incoming serial data!
  13. Don't forget to document! :)

Further reading: Great PySerial reference!

Written as part of week16 - interface and application programming