Data visualization with serial package
Serial communication is a method of transmitting data one bit at a time over a single communication channel or wire. It’s commonly used in electronics and computer systems for communication between devices, such as between a microcontroller and a computer or between two microcontrollers.In serial communication, the bits are sent sequentially, one after the other, over the communication channel. This is different from parallel communication, where multiple bits are transmitted simultaneously over multiple channels.We use serial communication because it simplifies design, reduces costs, works over longer distances, and is flexible for different types of devices and data transfer needs. Its widespread use in embedded systems, computers, and industrial applications underscores its importance in modern technology.
There are two types of serial communication.
Asynchronous: No clock signal is used. Data is transmitted with start and stop bits to frame each byte. An example of this is RS-232 (used for many old computer serial ports).There can be additional bit known as parity bit to detect errors in transmitted data.
Synchronous: A clock signal is used to sync data transmission. Examples include SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit).
While synchronous serial communication is faster and more efficient, asynchronous serial communication is more popular due to its simplicity, lower hardware requirements, and widespread support in general-purpose applications, especially in embedded systems and microcontroller communication.
Further technical details for serial communication can be read here and here.
The serial package in R can be extremely useful in applications where you need to read from or communicate with external hardware via a serial interface. It’s widely used in fields like IoT, embedded systems, scientific research, healthcare, and industrial automation to automate data acquisition, process real-time data, and control devices. The flexibility of integrating serial communication with R’s powerful data analysis and visualization tools makes it a valuable resource for many projects.
My motivation to use the serial package in R came when I was experimenting with a PID controller algorithm for a simple DC motor. I wanted to graph the motor’s RPM over time whenever I requested a change in speed. While there are many software tools available for this task, as a longtime user of R, I wanted to try reading the RPM data for real-time visualization using R’s graphic capabilities and automation.
A quick internet search for “How to use R for serial communication?” didn’t yield useful results. In this post, I will document how I managed to establish a serial communication link between my experimental device and R, opening the gateway to capture and visualize data in real time
Setting up can be broken down into the following steps:
- Load the serial library.
- Check whether the communication port is listed as available.
- Set up the connection parameters.
- Open and establish the communication channel.
- Verify that the channel is available for communication.
- List and review the communication channel parameters.
- Perform data communication.
- Close the communication channel and release the resources.
# Documentation: https://cran.r-project.org/web/packages/serial/index.html
library(serial)
# Lists available COM ports mapped for serial communication
listPorts()
# Configuration of the serial communication
con <- serialConnection(name = "testcon", # User defined name identify the connection
port = "COM5",
mode = "115200,n,8,1",
newline = 1,
buffering = "line",
translation = "auto")
open(con) # Establishes the serial communication with R
isOpen(con = con) # Test whether the communication established
summary(con) # Get the parameter summary of the established serial connection
nBytesInQueue(con = con) # Number of bytes (In and Out) in a named vector
flush(con = con) # Should flush the buffer. But I couldn't get it worked.
# According to serial package documentation this works with certain buffering configurations only
read.serialConnection(con, n = 4) # Read the serial buffer. n defines the number of bytes to be read
write.serialConnection(con,'o') # Write characters to serial buffer to switch on the device
# Infinite loop to read incoming data
while(1){
if(nBytesInQueue(con = con)[1]>0){
print(read.serialConnection(con, n = 1))
}
}
write.serialConnection(con,'f') # Write characters to serial buffer to switch off the device
close(con = con) # Closes the established serial connection and releases resources
isOpen(con = con)Outputs
1
2
3
Note: Here you might get accumulated set of data in the serial buffer as a batch reading.
4
This demonstration shows how easy it is to establish serial communication between R and a data-generating system. It opens up many possibilities for detailed data analysis and visualization, leveraging R’s advanced capabilities for data wrangling and visualization.