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.

vi uart4.py
'''
Copy and Paste the python code on to your vi editor
and save and exit the editor.
You can also use any other editor from your host PC
to write the code with .py extension and transfer the
python code file to the RuggedBoard. To transfer files
from host PC to RB will be show in the later upcoming
section named as "Transfering files from host PC to RB"
'''
 
import time
import serial
 
def main():
    # Replace '/dev/ttymxc0' with the correct UART device file
    uart_port = '/dev/ttymxc0'
    baud_rate = 115200
 
    # Open the UART port
    try:
        ser = serial.Serial(uart_port, baud_rate, timeout=1)
        print(f"Connected to {uart_port} at {baud_rate} bps")
    except serial.SerialException as e:
        print(f"Error: {e}")
        return
 
    try:
        # Send a message
        message = "Hello, UART!\n"
        ser.write(message.encode())
        print(f"Sent: {message}")
 
        # Wait for a moment to receive data
        time.sleep(1)
 
        # Read and print received data
        received_data = ser.read_all().decode()
        print(f"Received: {received_data}")
 
    except Exception as e:
        print(f"Error: {e}")
 
    finally:
        # Close the UART port
        ser.close()
        print("UART port closed")
 
if __name__ == "__main__":
    main()

To execute the code use the below command and enter anything for loopback test.

python uart4.py

OUT PUT: With and Without loop back :

=========================================================================

On Board LED blinking Programming using Python 3

In rugged board having 3 LED's

vi led_blinky.py
#!/usr/bin/python
import sys,time
gpio_name = "gpio43"  #you can try with 44 and 45 also
gpio_number = "43"
OUT = "out"
IN = "in"
HIGH = "1"
LOW = "0"
sys_path = "/sys/class/gpio/"
def gpExport(gpio_number):
        f = open(sys_path+"export", "w")
        f.write(gpio_number)
        f.close()
def gpUnexport(gpio_number):
        f = open(sys_path+"unexport","w")
        f.write(gpio_number)
        f.close()                       
def setDir(gpio_name, dir):             
        f = open(sys_path+gpio_name+"/direction","w")
        f.write(dir)                                
        f.close()                                   
def setValue(gpio_name, val):                       
        f = open(sys_path+gpio_name+"/value","w")   
        f.write(val)                                
        f.close()                                   
def initLed(gpio_name, gpio_number):                
        gpExport(gpio_number)                       
        setDir(gpio_name,OUT)                       
        setValue(gpio_name, HIGH)
def exit(gpio_name, gpio_number):                   
        setValue(gpio_name, HIGH)                   
        gpUnexport(gpio_number)                     
def ledOff(gpio_name):                              
        setValue(gpio_name, HIGH)                   
def ledOn(gpio_name):                               
        setValue(gpio_name, LOW)                    
initLed(gpio_name, gpio_number)                     
try:                                                
        while True:                                 
                ledOn(gpio_name)                    
                print("LED1 is ON")                 
                time.sleep(1)                       
                ledOff(gpio_name)                   
                print("LED1 is OFF")                
                time.sleep(1)                       
except KeyboardInterrupt:                           
        exit(gpio_name, gpio_number)
  • To execute the code use the below command then on board yellow LED(near to ethernet port) starts blinking.

python3 led_blinky.py

=========================================================================

  • Below code is for blinking the 3 LED's (gpio2_11, gpio2_12, gpio2_13) :

vi blinky_leds.py
#!/usr/bin/python
 
import sys,time
 
gpio1_name = "gpio43"
gpio1_number = "43"
gpio2_name = "gpio44"
gpio2_number = "44"
gpio3_name = "gpio45"
gpio3_number = "45"
 
OUT = "out"
IN = "in"
 
HIGH = "1"
LOW = "0"
 
sys_path = "/sys/class/gpio/"
 
def gpExport(gpio_number):
        f = open(sys_path+"export", "w")
        f.write(gpio_number)
        f.close()
 
def gpUnexport(gpio_number):
        f = open(sys_path+"unexport","w")
        f.write(gpio_number)
        f.close()
 
def setDir(gpio_name, dir):
        f = open(sys_path+gpio_name+"/direction","w")
        f.write(dir)
        f.close()
 
def setValue(gpio_name, val):
        f = open(sys_path+gpio_name+"/value","w")
        f.write(val)                                 
        f.close()                                    
def initLed(gpio_name, gpio_number):                 
        gpExport(gpio_number)                        
        setDir(gpio_name,OUT)                        
        setValue(gpio_name, HIGH)                    
def exit(gpio_name, gpio_number):                    
        setValue(gpio_name, HIGH)                    
        gpUnexport(gpio_number)                      
def ledOff(gpio_name):                               
        setValue(gpio_name, HIGH)                    
def ledOn(gpio_name):                                
        setValue(gpio_name, LOW)                     
initLed(gpio1_name, gpio1_number)                    
initLed(gpio2_name, gpio2_number)                    
initLed(gpio3_name, gpio3_number)                    
try:                                                 
        while True:                                  
                ledOn(gpio1_name)                    
                print("LED1 is ON")                  
                ledOff(gpio2_name)                   
                ledOff(gpio3_name)                   
                time.sleep(0.2)                  
                ledOff(gpio1_name)               
                ledOn(gpio2_name)                
                print("LED2 is ON")              
                ledOff(gpio3_name)                   
                time.sleep(0.2)                      
                ledOff(gpio1_name)                   
                ledOff(gpio2_name)                   
                ledOn(gpio3_name)                    
                print("LED3 is ON")                  
                time.sleep(0.2)                      
except KeyboardInterrupt:                        
        exit(gpio1_name, gpio1_number)           
        exit(gpio2_name, gpio2_number)           
        exit(gpio3_name, gpio3_number)    
  • To execute the code use the below command then on board, all 3LED's(near to ethernet port) starts blinking.

python3 blinky_leds.py

NOTE:- If it will show the below error like :-

root@ruggedboard-imx6ul:~# python3 3leds_blink.py Error exporting GPIO 43: [Errno 16] Device or resource busy

  • We have to use the below command :

# echo <GPIO_NUMBER> > /sys/class/gpio/unexport

Last updated