Auto connected serial port interface


At work, we’ve developed several applications that need to run on different linux distros and while generally possible, there are times when installing,updating and running these applications become an issue for those that don’t have intimate knowledge of how the application works.

Snaps seem like a good way to alleviate or even eliminate these issues.

I am new to snaps and I’m loving the whole idea. I’ve spent a couple of days reading and testing. I’ve packaged several test applications and I’ve even uploaded one.

We work with lots of devices that use serial ports(mostly RS-232) as the main way of interfacing to the world so we use standard terminal programs like minicom, gtkterm and putty to configure them.

I’m still wrapping my head around the slot/plug concept and how to properly define and
use them so it is possible that I’m missing something.
I read somewhere that a snap can’t auto connect but rather one has to manually connect the snap to the serial-port interface because of the potential to modify devices connected via a serial port.

This I can understand and I find that manually connecting to the serial-port interface once after installation is reasonable so along as the connection persists after updates and reboots.

Today I packaged minicom in order to understand how snaps work with serial ports.
Knowing that the serial-port interface was not auto connected, I was not at all surprised that minicom could not access any of the available serial ports.

I attempted to manually connect to the serial-port interface but to my dismay, I was not able to. It’s as if the interface skeleton is there to be implemented by someone.

After some more searching, I found examples of people creating snaps called gadgets that expose serial devices by name like "/dev/ttyUSB0 " and “/dev/ttyS4” but I didn’t find any that listed all of the serial ports recognized by the OS. This is rather frustrating because the number and type of serial ports I use change on a daily basis.

Today I am using “/dev/ttyUSB3” and “/dev/ttyS0” But there are times
I need to access “/dev/ttyACM0” .

I can make it work with a gadget but this seems rather clunky and it just reinforces my sense that I am missing something obvious.

In order to get minicom to mostly work I had to:

  1. Add current user to the dialout group, log out and log in.(I’m o.k. with this step)
  2. install the minicom snap I created.(I’m o.k. with this step)
  3. install a gadget snap that exposes the serial ports for minicom to access.(Not sure I’m o.k. with this step)
  4. manually connect the minicom serial-port plug to the gadget’s serial-port socket.(I’m o.k. with this step as long it’s part of the core)

Aside from the standard terminal programs and the like, we have many custom programs that use serial ports and would benefit from an implemented serial-port interface that supports auto connect.

Here are the snapcraft.yaml files I created for testing minicom and my gadget.

name: minicom
version: ‘1.0’
summary: minicom test
description: |
grade: stable
confinement: strict

- home
- serial-port
command: minicom

plugin: nil
stage-packages: [minicom]

name: serialports
version: ‘0.1’
summary: Gadget to expose serial ports to snaps
This gadget will expose serial ports to a snap. The serial port
list seems to be generated using file globbing instead of regex.
The serial ports listed will be similar to the list generated by
issuing the command "ls /dev/tty[A-U]".
This should list serial ports that fit the patterns
,ttyUSB* and ttyACM*
To use serial ports on your system make sure that the user is in the
dialout group. To add a user to the dialout group you can issue the
following command. “sudo usermod -a -G dialout USERNAME”
Afterwards logout and log back in.

confinement: strict
grade: stable

plugin: nil

interface: serial-port
path: /dev/tty[A-U]*

I also read that I could modify,compile and install my own version of snapd but this seems like more work than it should be.

If it is in fact true that snapd doesn’t have a clean way of using serial ports without gadgets, could this be implemented in the core?
I think many programs would benefit from this, not just the obvious terminal ones.

There are lots of people that have no idea that they are using a serial port.

  1. I believe Arduino boards show up as “/dev/ttyUSB*”
  2. Many USB modems show up as “/dev/ttyACM*”
  3. Serial ports are used in robotics as well, I think I saw a turtlebot-snap somewhere that
    had to write a gadget to access a serial port.

Thanks and keep up the good work.

Yes, if your snap is strictly confined it can only access serial ports on Ubuntu Core with a gadget that exposes them as required. You cannot access them from classic Ubuntu, or probably any other distro that doesn’t have a gadget.

Definitely. You’re talking about this:

As of now, I don’t believe further interface work on this (e.g. hotplugging) in planned. @niemeyer can tell you more. See this bug, as well as these topics for more details.

1 Like

Thanks for the quick response.

I’ll try and learn more about how I can release these apps in a convenient way for others to use.
I guess changing confinement to classic would grant access to the serial ports but
I really like the idea of sandboxing these applications as much as possible.


1 Like

I have the exact issue with tio:

The way to make it easier for end users it to request that the serial-port interface gets autoconnected so that users don’t need to manually connect it. Such a request requires snap review and voting - takes about a week or so.

@lundmar this doesn’t really have anything to do with autoconnection, it’s the fact that a custom gadget is required.

Nice terminal app, I’ll try it out later.

I actually don’t mind manually connecting to the serial port interface, I can live with that as long as it’s only done once per installation.

I do believe that serial ports should be made available without having to use a gadget. I’m not sure I understand why it can’t/won’t be implemented but then again I’m still confused about how it all works.
I own a really old Microsoft serial mouse I like to use… Not really, I rarely use it but my point is that I believe that applications that are strictly confined should be able to use serial ports with a little less effort.

I did however notice that what I’m asking for is similar to the camera interface in that it’s available
but one has to manually connect to it. Curiously only the first camera is available which leads me to believe that
there is a more fundamental issue when implementing these sorts of things.

Maybe determining which is the first serial port isn’t trivial?

Doh, obviously I scanned too quickly through your original post and misread the issue.

Actually, I haven’t tested my tio snap for various reasons. I simply assumed that the serial-port plug interface would just work, else there is little point in having it. For serial applications like minicom, putty, tio, etc. we need this ‘serial-port’ interface to be functional - maybe some more implementation is needed. Recent Linux kernels actually support ways to classify tty devices as “serial” devices - one can start by looking in /dev/serial/* . Regardless, the user will have to be part of whatever group provides access to serial devices, typically “dialout” - not all distributions adds users to this group so that is sometimes a pain point.

Yes, don’t hesitate to try out tio - it’s supposed to be a simpler/modern alternative to minicom, etc… Oh, maybe don’t use the tio snap ha ha. Luckily tio is packaged for various distributions already but I would surely like to see the tio snap work seamlessly out of the box with all serial devices presented by the kernel - I mean, thats the point of a serial tty tool.

Oh, by the way. Until the serial-port gadget/interface is improved to give access to all system serial devices you can get your snap working by simply installing it non-confined like so:

$ snap install tio --classic

It’s better than nothing but it surely would be best to run it confined with allowed access to serial tty devices.

For future readers of this topic, there is now the hotplug serial-port interface. See After enabling hotplug, you will now see individual serial-port interfaces show up for all matching serial ports (i.e. ones which define udev attributes) that you can connect your snap to.