# Using Serial Ports with Ruby

DevFeed: [Using Serial Ports with Ruby](<https://devfeed.tech/articles/using-serial-ports-with-ruby-38998.md>)

Original publisher: [Read original article](<https://tenderlovemaking.com/2024/02/16/using-serial-ports-with-ruby/>)

Published: 2024-02-16T19:56:28Z

Content type: tutorial

Language: en

Sources: [Aaron Patterson](<https://devfeed.tech/sources/aaron-patterson.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Embedded Systems](<https://devfeed.tech/topics/embedded-systems.md>), [Hardware](<https://devfeed.tech/topics/hardware.md>)

Tags: [embedded](<https://devfeed.tech/tags/embedded.md>), [embedded-systems](<https://devfeed.tech/tags/embedded-systems.md>), [firmware](<https://devfeed.tech/tags/firmware.md>), [hardware](<https://devfeed.tech/tags/hardware.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [streaming](<https://devfeed.tech/tags/streaming.md>)

## AI overview

A tutorial on using the UART gem with Ruby to communicate with a GMC-320 Geiger counter over a serial port. It demonstrates configuring the connection, retrieving the device firmware version, and preparing to process live updates with streaming data and timeouts.

## Source excerpt

Lets mess around with serial ports today! I love doing hardware hacking, and dealing with serial ports is a common thing you have to do when working with embedded systems. Of course I want to do everything with Ruby, and I had found Ruby serial port libraries to be either lacking, or too complex, so I decided to write my own. I feel like I've not done a good enough job promoting the library, so today we're going to mess with serial ports using the UART gem. Don't let the last commit date on the repo fool you, despite being over 6 years ago, this library is actively maintained (and I use it every day!). I've got a GMC-320 Geiger counter. Not only is the price pretty reasonable, but it also has a serial port interface! You can log data, then download the logged data via serial port. Today we're just going to write a very simple program that gets the firmware version via serial port, and then gets live updates from the device. This will allow us to start with UART basics in Ruby, and then work with streaming data and timeouts. The company that makes the Geiger counter published a spec for the UART commands that the device supports, so all we need to do is send the commands and read the results. According to the spec, the default UART config for my Geiger counter is 115200 BPS, Data bit: 8, no parity, Stop bit: 1, and no control. This is pretty easy to configure with the UART gem, all of these values are default except for the baud rate. The UART gem defaults to 9600 for the baud rate, so that's the only thing we'll have to configure. Getting the hardware version To get the hardware model and version, we just have to send <GETVER>> over the serial port and then read the response. Let's write a small program that will fetch the hardware model and version and print them out. require "uart" UART.open ARGV[0], 115200 do |serial| # Send the "get version" command serial.write "<GETVER>>" # read and print the result puts serial.read end The first thing we do is require the uar