Communication Protocols

How do two computers communicate?

Communication Protocol

We can make a parallel with human communication.

A protocol is a set of rules that ensure information is well understood. We can highlight two extremes:

  • structured human conversation
  • incoherent messages, hubbub
We will here realize the construction of a basic communication protocol, as well as a program that will allow us to use it to create a chat system:
  • sending global messages
  • sending personal messages
  • creating a discussion group

Using micro:bit cards

We will use micro:bit cards to practice a communication network simulation. They allow us to approach the question with useful simplifications:

  • no network to configure
  • radio communication, so wireless
  • using Python

micro:bit editor v3

After Capytale notebooks, Thonny, Mu, a new editor!

Editor source code

The editor is open-source.

The radio module

The radio module of micro:bit cards is very simple to use. A code example:

from microbit import *
import radio

radio.on()  # Activate radio
radio.config(channel=10)  # Set a channel (10 here)

while True:
    if button_a.was_pressed():
        radio.send("Hello!")  # Send a message
    message = radio.receive() # Check regularly for message reception
    if message:
        print(message)  # Display received message in console

Explanations:

  • the radio module contains functions to interact with the micro:bit card's radio
  • the radio.config() function allows configuring the radio
  • the radio.on() function allows activating the radio
  • the radio.send() function allows sending data by radio

Communications without protocol

In this first exercise, we will get familiar with the editor interface, ensure proper functioning of sending a program to a card, and set up our first chat discussion system.

Instructions

The objective is simple:

  • create a program to join channel 10
  • the microbit card detects button A press - if button A is pressed, it asks the user for the message to send (input() function) - once the result of input() has been stored in a variable, we send the content of this variable with radio.send() - to avoid multiple sends, we can use immediately after radio.send() the sleep(100) function which will delay the program - with radio.receive(), we detect message reception, which we'll display with print() in the console
This system quickly shows its limits: a set of messages whose sender and recipient we don't know succeed each other, it's impossible to follow a conversation. The whole thing corresponds to a sort of unintelligible background noise.

Setting up a protocol

A communication protocol will easily allow us to improve our messages to reach our objective: a large forum where you can communicate with whoever you want, knowing precisely who is speaking to you and to whom you are addressing.

We therefore need to find a way to add information to the simple message content, notably the sender and recipient. We must therefore, together, agree on a format that will be common to all our messages, and that our programs will be able to process: a protocol.

We remind some useful functions:

  • the split() function allows dividing a string into a list of strings. The argument is the separator.
message = "Hello! How are you?"
message.split('.') # Will give a list: ["Hello ", " How are you?"]
  • the + operator allows concatenating strings:
author = "Mathieu"
message = "Hello"
frame = author+message
# frame will equal "MathieuHello"

Improving our program

Continue on page 2