ruggedBOARD
Tutorial Using Python
Tutorial Using Python
  • Overview
  • Quick Start with RB
    • Pre-Requisite
      • Required Hardware and Software Components
      • Host Setup
      • Linux Commands
    • Power Up your RB
    • Fiddle with RB Command-line
  • Lib-Mraa & UPM Sensor Libraries (Python)
    • First Python Program
    • Know Your Libraries
      • LibMRAA
        • Python Programming using MRAA (SPI)
        • Python Programming using MRAA (PWM)
        • Python Programming using MRAA (gpio_blink)
        • Python Programming using MRAA (Aio)
      • LibUPM
    • Example Programs for Sensors
      • Procedure to interface phyNODE with RB
      • Accelerometer
      • Temperature Sensor
      • Pressure Sensor
      • Humidity Sensor
      • Magnetometer
      • PIR Sensor
      • Ultrasonic Sensor
      • Gas Sensor (MQ5)
    • mikroBUS UPM Sensor
    • Example programs for Communication Protocols
      • GPIO
      • I2C
      • UART
  • Hello World in C language
    • Setting up Environment
    • Transferring Files from Host to Board
    • Write your first C program
  • IoT Implementation
    • What is IoT?
    • IoT Protocols
    • MQTT Setup
  • Peripherals Functionality Testing using C Language
    • Interfaces
      • GPIO(General purpose Input/Output):
      • UART(Universal Asynchronous Receiver-Transmitter)
      • RS232
      • RS485
      • CAN
      • MikroBUS
      • mPCIe
  • Advance RB
    • Preparing SD card in case of OS failure
Powered by GitBook
On this page

Was this helpful?

  1. Lib-Mraa & UPM Sensor Libraries (Python)
  2. Example programs for Communication Protocols

UART

A simple UART sender and receiver program

Sender:

import mraa
import sys

sys.stdout.write("Initializing UART...")
u=mraa.Uart(0)
print("...done")

print("Setting UART parameters: baudrate 115200, 8N1, no flow control")
u.setBaudRate(115200)
u.setMode(8, mraa.UART_PARITY_NONE, 1)
u.setFlowcontrol(False, False)

msg_b = bytearray("Hello, mraa byte array!", "ascii")
print("Sending message as a byte array: '{0}'".format(msg_b))
u.write(msg_b)
# Make sure the message gets out to the line.
# It's generally unnecessary (and performance-degrading) to do this explicitly,
# UART driver normally takes care of that, but it may be useful with specific
# half-duplex endpoints, like Dynamixel servos.
u.flush()

msg_s = "Hello, mraa string!"
print("Sending message as a string: '{0}'".format(msg_s))
u.writeStr(msg_s)

sys.stdout.write("Two-way, half-duplex communication, sending a flag...")
u.writeStr("X")
print("...sent, awaiting response...")
# Checking for data in the RX buffer, giving it a 100ms timeout
if u.dataAvailable(100):
  print("We've got a response: '{0}', says the other side".format(u.readStr(20)))
else:
  print("No data received, do you have anything at the other end?")

Receiver:

import mraa

# Initialize UART
u=mraa.Uart(0)

# Set UART parameters
u.setBaudRate(115200)
u.setMode(8, mraa.UART_PARITY_NONE, 1)
u.setFlowcontrol(False, False)

# Start a neverending loop waiting for data to arrive.
# Press Ctrl+C to get out of it.
while True:
  if u.dataAvailable():
    # We are doing 1-byte reads here
    data_byte = u.readStr(1)
    print(data_byte)
    # Just a two-way half-duplex communication example, "X" is a flag
    if data_byte == "X":
      u.writeStr("Yes, master!")

PreviousI2CNextSetting up Environment

Last updated 6 years ago

Was this helpful?