The TRNG data can be received by anything that can interact with the USB CDC serial port that gets created. In the previous section, we used screen to simply open and view the output, without doing any parsing or processing. Here we go a little further and show some simple examples of how to do this using Python.

Install pySerial

The pySerial module is used to open and read from the serial port in Python. If this module is not already installed on your setup, go here:

Read Raw Bytes

This is the simplest thing to read. The out is just a continuous stream of random bytes. You can read in as many of these as you wish.

Here is how to read in 4 bytes.

import serial

# how many bytes to read?
TRNG_SIZE = 4

# open serial port
ss = serial.Serial("/dev/ttyACM0")

# read raw bytes
raw_bytes = ss.read(TRNG_SIZE)

# print them
print(raw_bytes)

Read CSV String

Reading and parsing a comma separated (CSV) formatted string is also easy, thanks to Python's excellent string handling capabilities. Once parsed, the data can be further processed for whatever end application.

Here we just turn it into a list (array) of integers.

import serial

# open serial port
ss = serial.Serial("/dev/ttyACM0")

# read string
_ = ss.readline() # first read may be incomplete, just toss it
raw_string = ss.readline().strip().decode()

# create list of integers
rnd_ints = [int(x) for x in raw_string.split(',')]

# print them
print(rnd_ints)

Read JSON String

JavaScript Object Notation, aka JSON, is another common text based data format. It allows for more complexity than CSV and thanks to Python's JSON module, it is also easy to process. Once loaded in, that data can be accessed via the trng entry.

Here is a simple example.

import json
import serial

# open serial port
ss = serial.Serial("/dev/ttyACM0")

# read string
_ = ss.readline() # first read may be incomplete, just toss it
raw_string = ss.readline().strip().decode()

# load JSON
json_data = json.loads(raw_string)

# print data
print(json_data['trng'])

This guide was first published on Dec 09, 2021. It was last updated on Dec 09, 2021.

This page (Receiving TRNG) was last updated on Jan 19, 2022.

Text editor powered by tinymce.