Python and Mikro C

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Dear all,
I am currently learning Python. I wanted to start a project that allows me to build a GUI in Python which allows me to set LEDs on a PIC microcontroller (PIC18F4520). I am using Mikro C.
I am thinking of using FDTI from PC USB to RX/TX (UART) of the micro.
The GUI will be very simple ( I think Tinkert) is used for Python. It will be a couple of buttons to light LEDs from PORTD of the micro. I think I can use the pyserial function in Python but not sure how to extract the Python script from the micro end.
Has anyone done this before? Any help would be appreciated.
Thanks in advance.
Best regards,
Rajinder
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
I think I can use the pyserial function in Python but not sure how to extract the Python script from the micro end.
You don't have to. The PIC doesn't have to know anything about Python. In fact, it doesn't need to know where the instructions to turn the LEDs ona and off come from at all.
Your project requires 3 tasks to be done:
  1. Define a protocol which will transfer the information from the PC to the PIC via the serial link (or find a suitable protocol). A very simple protocol could involve the PC sending commands to the PIC as an ASCII string.
    For example "Lxy" where
    - L stands for "LED command"
    - x stands for "LED number", a number between 0...9, or use "xx" for up to 100 LEDs
    - y stands for "on" or "off", e.g. y=0 -> turn LED off, y=1 turn LED on
    Full example L20 -> turn LED 2 off
    It may be a good idea to reserve the number x=0 for "select all LEDs" to be able to turn all LEDs on or off with one command
  2. Write a Python program on the PC that converts the actions on the user interface (GUI) to such commands and sends these via the pxserial library routines to the PIC.
  3. Write a PIC program that reads the commands from the serial port and converts them to the equivalent LED on/off actions.
Regards,
Harald
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
You don't have to. The PIC doesn't have to know anything about Python. In fact, it doesn't need to know where the instructions to turn the LEDs ona and off come from at all.
Your project requires 3 tasks to be done:
  1. Define a protocol which will transfer the information from the PC to the PIC via the serial link (or find a suitable protocol). A very simple protocol could involve the PC sending commands to the PIC as an ASCII string.
    For example "Lxy" where
    - L stands for "LED command"
    - x stands for "LED number", a number between 0...9, or use "xx" for up to 100 LEDs
    - y stands for "on" or "off", e.g. y=0 -> turn LED off, y=1 turn LED on
    Full example L20 -> turn LED 2 off
    It may be a good idea to reserve the number x=0 for "select all LEDs" to be able to turn all LEDs on or off with one command
  2. Write a Python program on the PC that converts the actions on the user interface (GUI) to such commands and sends these via the pxserial library routines to the PIC.
  3. Write a PIC program that reads the commands from the serial port and converts them to the equivalent LED on/off actions.
Regards,
Harald
Hi Harald,
Thanks for your help. You make it sound easy. I have never done any work with serial comms and this may be tricky without further reading. I should be able to generate a GUI and get the PIC to read the ASCII text.
My PC gas no serial port (9 way). Hence I am having to use USB to rs232 ie FDTI. This is where I might get some problems. Any advice around this area. Is it a simple case if setting the baudrate etc from the Python program and similarly in the PIC UART?
Best regards,
Rajinder
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
The FDI chip and driver will create a virtual COM port on the PC. You ca use this COM port as usual.
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
The FDI chip and driver will create a virtual COM port on the PC. You ca use this COM port as usual.
Thanks for the advice.
I want to break this project up into smaller manageable projects.
I want to check that my serial comms works. Would it be a good idea to write a Python program to send characters and use a terminal program to see if they are sent successfully? Something like RealTerm etc
Then write a C program to recognise dummy ASCII values, decipher the information and switch on the LEDs.
Finally combine both together.
Any suggestions would be appreciated.
Thanks in advance,
Rajinder
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
I want to check that my serial comms works. Would it be a good idea to write a Python program to send characters and use a terminal program to see if they are sent successfully? Something like RealTerm etc
A good start. You will need a second PC, however, as the receiver of the serial data sent by your python program..
 
Top