# Understand RS232 port configuration



## cynwulf (Nov 16, 2020)

I have working serial ports, via an installed PCI-e card and USB to serial adapters (FTDI and Prolific chips) which are for use with serial connected printers.  Comms are established on all devices and works as expected - however.

When issuing the following in an attempt to configure the ports:


```
% stty -f /dev/cuaU0 raw pass8 -echo -hup clocal 115200
% stty -f /dev/cuaU0.init raw pass8 -echo -hup clocal 115200
stty: tcsetattr: Operation not permitted
```
The first succeeds, but attempting the same on the *.init device node will not allow configuration (except as root).  This would not be an issue, but the change to the *.init device is not persistent.


```
% ls -al /dev/cua*
crw-rw----  1 uucp  dialer  0x2a1 Nov 16 15:21 /dev/cuaU0
crw-rw---- 1 uucp dialer 0x2a2 Nov 16 15:19 /dev/cuaU0.init
crw-rw---- 1 uucp dialer 0x2a3 Nov 16 15:19 /dev/cuaU0.lock
crw-rw---- 1 uucp dialer 0x3e Nov 16 11:05 /dev/cuau0
crw-rw---- 1 uucp dialer 0x3f Nov 16 11:05 /dev/cuau0.init
crw-rw---- 1 uucp dialer 0x40 Nov 16 11:05 /dev/cuau0.lock
```


```
% cat /etc/devfs.rules
[system=10]
add path 'unlpt*' mode 0660 group operator
add path 'ulpt*' mode 0660 group operator
add path 'lpt*' mode 0660 group operator
add path 'usb/X.Y.Z' mode 0660 group operator
add path 'dri/*' mode 0666 group operator
add path 'drm/*' mode 0666 group operator
add path 'cuau*' mode 0660 group dialer
add path 'cuaU*' mode 0660 group dialer
```

After hotplugging:


```
% stty -f /dev/cuaU0
speed 9600 baud;
lflags: echoe echoke echoctl
oflags: tab0
cflags: cs8 -parenb clocal
```
It reverts to defaults.

I'm sure I'm missing something obvious here, or should I be configuring the lock device?

Thanks


----------



## SirDice (Nov 16, 2020)

cynwulf said:


> which are for use with serial connected printers


Do those still exist? Last serial printer I saw was well over 25 years ago. And even back then printers tended to use "Centronics" parallel connections. And those have pretty much disappeared too. It's all USB or network connected nowadays.


----------



## cynwulf (Nov 16, 2020)

Yes, these are thermal barcode printers manufactured by e.g. TSC, Toshiba, Datamax, Citizen, Zebra, etc.  Some of the machines are very old, but well made and still serviceable and rather than throw them away I connect them via USB to serial adaptors and put them to good use.  Even the newer machines often still come with RS232 ports.

Some of the machines also have parallel ports but those ports have vanished from modern PCs and some of the newer printers - and the adaptors are not the most robust or reliable (although they actually behave far better under FreeBSD or Linux than when used in MS Windows).


----------



## ralphbsz (Nov 16, 2020)

The devfs.rules files says that the devices should be owned by group operator. They are owned by group dialer. Something is wrong here. What does the line [system=10] in your devfs.rules file mean? I don't know; maybe check whether these rules are even being applied.

Read the handbook chapter about serial ports, it explains the difference between .init and .lock devices, and points at the correct man pages. I don't know why the .init device is refusing certain settings. But it makes sense that after hotplugging, the settings vanish, since unplugging a USB device causes it to vanish, and when you replug it, a new one (with the same name) will be created. Where would the old settings be stored?


----------



## Martin Paredes (Nov 16, 2020)

Reading above the section 26.2.2. Serial Port Configuration of the handbook, it says " The initialization devices (.init) are used to initialize communications port parameters *each time a port is opened*", also read the section 26.2.2

The configuration is applied until the port is opened


----------



## cynwulf (Nov 16, 2020)

ralphbsz, "system" is the name of the ruleset. It's loaded via rc.conf. I followed the man page for devfs.rules(5) but called it "system". I can't recall why its set to 10, but again the man page suggests this. The call out devices are defined as dialer group in devfs.rules (right at the bottom). I can configure those as a normal user, but not the *.init devices.

Martin, yes I've seen that and I think ralphbz is correct that hotplugging clears any parameters as the device is recreated. That makes sense to me, but I'll have find another solution. Thank to all for the advice.


----------



## SirDice (Nov 16, 2020)

cynwulf said:


> Martin, yes I've seen that and I think ralphbz is correct that hotplugging clears any parameters as the device is recreated. That makes sense to me, but I'll have find another solution. Thank to all for the advice.


You could create a few custom devd.conf(5) rules for these devices. That could apply the settings as soon as the device is plugged in.


----------



## Martin Paredes (Nov 17, 2020)

What is happening is that every time you insert the USB device the files are created (devices, .init .lock), when you remove it the files are deleted and therefore the initial configuration is lost


----------



## cynwulf (Nov 19, 2020)

Created two devd.conf(5) rules for both types of devices and works perfectly:


```
# FTDI
attach 100 {

device-name "uftdi0";
action "stty -f /dev/cuaU0.init raw pass8 -echo -hup clocal 115200";
};

# Prolific
attach 100 {

device-name "uplcom0";
action "stty -f /dev/cuaU0.init raw pass8 -echo -hup clocal 115200";
};
```

Thanks to all.

(note: I will only install one of either device, so catering for sequential device nodes, or both being plugged in at the same time, but only cuaU0 being configured, is not so important in my case)


----------



## cynwulf (Nov 20, 2020)

Ok so it bugged me and I decided to redo it so that at least two USB to serial adapters are configured when plugged in and don't have to be matched to any particular vendor/chip:

This is in my /usr/local/etc/devd/some_rules.conf

```
notify 0 {
        match "cdev" "cuaU0";
        action "stty -f /dev/cuaU0.init raw pass8 -echo -hup clocal 115200";
};

notify 0 {
        match "cdev" "cuaU1";
        action "stty -f /dev/cuaU1.init raw pass8 -echo -hup clocal 115200";
};
```


----------

