Controlling your MCU
The SCADA interface in Flowcode is powerful - but is limited to Windows PC and has limited graphics abilities (though v10 might change this)
I wanted to control an MCU from a different language - this would allow me to use Linux, Mac or Android devices.
To test the idea - I used an Arduino UNO attached to the PC via USB (COM3 on my setup)
I installed the Arduino SCADA firmware (from Component: SCADA (Arduino Uno) (SCADA Slaves) )
This allows commands to be passed to the Arduino via USB at 115200 baud.
I used Python on a PC - and tested the simplest command - flash the inbuilt LED with a 1s flasher program
import serial ## Serial port routines
import time ## for sleep (delay)
state = True ## led state - true = on
## Data to set a pin - in this case 13 (LED builtin)
pin13 = [58, 128, 13, state, 59, 10] ## Output to pin 13
data = bytearray(pin13)
## Open com port - my SCADA device attached to COM3
with serial.Serial(port = "COM3", baudrate = 115200) as port:
while(1):
data[3] = state ## insert 'on' or 'off' into the command bytes
port.write(data) ## send the command to the serial port
state = not state ## toggle the led state
time.sleep(1) ## pause 1s
Save this code as flasher.py and run using python flasher.py
(Python can be downloaded from Download Python | Python.org)
This 'flashes' the inbuilt LED at 1s intervals.
Then - for fun - I added a 'graphic' display of the LED state on the PC.
import serial ## Serial port routines
import time ## for sleep (delay)
state = True ## led state - true = on
import pygame
## Pygame initialisation
BLACK = (0, 0, 0)
RED = (255, 0, 0)
clock = pygame.time.Clock()
pygame.init()
pygame.display.set_caption('Flasher 1s')
screen = pygame.display.set_mode((240,240))
## Data to set a pin - in this case 13 (LED builtin)
pin13 = [58, 128, 13, state, 59, 10] ## Output to pin 13
data = bytearray(pin13)
## Open com port - my SCADA device attached to COM3
with serial.Serial(port = "COM3", baudrate = 115200) as port:
while(1):
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
data[3] = state ## insert 'on' or 'off' into the command bytes
port.write(data) ## send the command to the serial port
if state:
screen.fill(RED)
else:
screen.fill(BLACK)
pygame.display.update()
state = not state ## toggle the led state
time.sleep(1) ## pause 1s
This simply displays a 240 x 240 window - which is red when the LED is on and black when it is off.
However - this technique allows many extra benefits - the user could use Python's extensive libraries to process data from a sensor for instance - or post to a website...
The technique also works for other languages (although note that opening a COM port in C++ is more involved :-( ) - for example MATLAB.
Here I've only shown one command (set a pin) - there are many others available - for outputting data to i2c or SPI etc.