Rugged Board -A5D2x
Search…
Example Program for Interfaces Using Python 3

UART Programming using Python 3

Locate P4 connector on board by referring to hardware manual and short pin 1 (RS232_TX_1) and pin 2(RS232_RX_1) to echo the transmitted data back to board. Short Type the following code in target board and save it with extension .py .
To test this python code, need to add the pyserial package in root file system. in nor rootfs image, this pyserial package was not included. This will be included in sdcard image.
1
vi uart4.py
Copied!
1
'''
2
Copy and Paste the python code on to your vi editor
3
and save and exit the editor.
4
You can also use any other editor from your host PC
5
to write the code with .py extension and transfer the
6
python code file to the RuggedBoard. To transfer files
7
from host PC to RB will be show in the later upcoming
8
section named as "Transfering files from host PC to RB"
9
'''
10
11
import time
12
import serial
13
14
ser = serial.Serial(
15
port='/dev/ttyS4',
16
baudrate=9600,
17
parity=serial.PARITY_ODD,
18
stopbits=serial.STOPBITS_TWO,
19
bytesize=serial.SEVENBITS
20
)
21
22
ser.isOpen()
23
24
print ('Enter your commands below.\r\nInsert "exit" to leave the application.')
25
26
input=1
27
while 1 :
28
input = raw_input(">> ")
29
if input == 'exit':
30
ser.close()
31
exit()
32
else:
33
ser.write(input + '\r\n')
34
out = ''
35
time.sleep(1)
36
37
while ser.inWaiting() > 0:
38
out += ser.read(1)
39
40
if out != '':
41
print ">>" + out
Copied!
To execute the code use the below command and enter anything for loopback test.
1
python uart4.py
Copied!
===========================================================

On Board LED blinking Programming using Python 3

In rugged board having 3 LED's, here blinking the one LED(PC13) using below python example code.following code in target board and save it with extension .py .
1
vi led_blinky.py
Copied!
1
#!/usr/bin/python
2
3
import sys,time
4
5
gpio_name = "PC13"
6
gpio_number = "77"
7
8
OUT = "out"
9
IN = "in"
10
11
HIGH = "1"
12
LOW = "0"
13
14
sys_path = "/sys/class/gpio/"
15
16
def gpExport(gpio_number):
17
f = open(sys_path+"export", "w")
18
f.write(gpio_number)
19
f.close()
20
21
def gpUnexport(gpio_number):
22
f = open(sys_path+"unexport","w")
23
f.write(gpio_number)
24
f.close()
25
26
def setDir(gpio_name, dir):
27
f = open(sys_path+gpio_name+"/direction","w")
28
f.write(dir)
29
f.close()
30
31
def setValue(gpio_name, val):
32
f = open(sys_path+gpio_name+"/value","w")
33
f.write(val)
34
f.close()
35
36
def initLed(gpio_name, gpio_number):
37
gpExport(gpio_number)
38
setDir(gpio_name,OUT)
39
setValue(gpio_name, HIGH)
40
41
def exit(gpio_name, gpio_number):
42
setValue(gpio_name, HIGH)
43
gpUnexport(gpio_number)
44
45
def ledOff(gpio_name):
46
setValue(gpio_name, HIGH)
47
48
def ledOn(gpio_name):
49
setValue(gpio_name, LOW)
50
51
initLed(gpio_name, gpio_number)
52
try:
53
while True:
54
ledOn(gpio_name)
55
print("LED1 is ON")
56
time.sleep(1)
57
ledOff(gpio_name)
58
print("LED1 is OFF")
59
time.sleep(1)
60
61
except KeyboardInterrupt:
62
exit(gpio_name, gpio_number)
Copied!
To execute the code use the below command then on board yellow LED(near to ethernet port) starts blinking.
1
python3 led_blinky.py
Copied!
Below code is for blinking the 3 LED's (PC13, PC17, PC19).
1
vi blinky_leds.py
Copied!
1
#!/usr/bin/python
2
3
import sys,time
4
5
gpio1_name = "PC13"
6
gpio1_number = "77"
7
gpio2_name = "PC17"
8
gpio2_number = "81"
9
gpio3_name = "PC19"
10
gpio3_number = "83"
11
12
OUT = "out"
13
IN = "in"
14
15
HIGH = "1"
16
LOW = "0"
17
18
sys_path = "/sys/class/gpio/"
19
20
def gpExport(gpio_number):
21
f = open(sys_path+"export", "w")
22
f.write(gpio_number)
23
f.close()
24
25
def gpUnexport(gpio_number):
26
f = open(sys_path+"unexport","w")
27
f.write(gpio_number)
28
f.close()
29
30
def setDir(gpio_name, dir):
31
f = open(sys_path+gpio_name+"/direction","w")
32
f.write(dir)
33
f.close()
34
35
def setValue(gpio_name, val):
36
f = open(sys_path+gpio_name+"/value","w")
37
f.write(val)
38
f.close()
39
40
def initLed(gpio_name, gpio_number):
41
gpExport(gpio_number)
42
setDir(gpio_name,OUT)
43
setValue(gpio_name, HIGH)
44
45
def exit(gpio_name, gpio_number):
46
setValue(gpio_name, HIGH)
47
gpUnexport(gpio_number)
48
49
def ledOff(gpio_name):
50
setValue(gpio_name, HIGH)
51
52
def ledOn(gpio_name):
53
setValue(gpio_name, LOW)
54
55
initLed(gpio1_name, gpio1_number)
56
initLed(gpio2_name, gpio2_number)
57
initLed(gpio3_name, gpio3_number)
58
try:
59
while True:
60
ledOn(gpio1_name)
61
print("LED1 is ON")
62
ledOff(gpio2_name)
63
ledOff(gpio3_name)
64
time.sleep(0.2)
65
66
ledOff(gpio1_name)
67
ledOn(gpio2_name)
68
print("LED2 is ON")
69
ledOff(gpio3_name)
70
time.sleep(0.2)
71
72
ledOff(gpio1_name)
73
ledOff(gpio2_name)
74
ledOn(gpio3_name)
75
print("LED3 is ON")
76
time.sleep(0.2)
77
78
except KeyboardInterrupt:
79
exit(gpio1_name, gpio1_number)
80
exit(gpio2_name, gpio2_number)
81
exit(gpio3_name, gpio3_number)
Copied!
To execute the code use the below command then on board, all 3LED's(near to ethernet port) starts blinking.
1
python3 blinky_leds.py
Copied!