# How to manage serial port disconnection

**URL:** https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592
**Category:** General
**Created:** [21 August 2019 09:34 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592 "2019-08-21T09:34:32Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)
#### Post date: [21 August 2019 09:34 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/1 "2019-08-21T09:34:32Z")

</div>

Hello,

I'm using Node-red on a Raspberry Pi 3B+ with Raspbian Stretch.  
I'm also using [UUgear 7-port USB hub](http://www.uugear.com/product/7-port-usb-hub-for-raspberry-pi/).

I connected RS232 devices with node-red-node-serialport, using the serial request node.

For easier device management I gave names to my USB ports by adding udev rules using [this](https://askubuntu.com/questions/49910/how-to-distinguish-between-identical-usb-to-serial-adapters) and [this](http://hintshop.ludvig.co.nz/show/persistent-names-usb-serial-devices/) tutorials.

This is working very well.

But sometimes my USB ports disconnect, often when I plug a new device or when I touch the hardware.  
They reconnect immediatly and can be found using the cmd console, but Node-red lost them and I have to restart Node-red to get them back.

Do you know how I can manage this so Node-red can fin them back ?

Thanks for your help

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 August 2019 10:33 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/2 "2019-08-21T10:33:51Z")

</div>

How do you know they are lost to node-red?

---

<div class="post-metadata">

### Author: ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)
#### Post date: [21 August 2019 11:11 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/3 "2019-08-21T11:11:16Z")

</div>

Before disconnection it is written "connected" with a green dot below the node.  
After it is written "disconnected" with a red dot.  
And I have a message in the debug console :

```auto
"serial port /dev/port_usb_2 closed unexpectedly"

```

port\_usb\_2 is the name I gave to my USB port, instead of ttyUSBx

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 August 2019 11:14 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/4 "2019-08-21T11:14:43Z")

</div>

Is this using the standard serial port node (node-red-node-serialport) or have you installed another serial port node?

---

<div class="post-metadata">

### Author: ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)
#### Post date: [21 August 2019 11:21 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/5 "2019-08-21T11:21:17Z")

</div>

Yes I'm using the standard one.  
The disconnection is due to the hardware however what I need is when I detect a disconnection (I can with status or catch), wait a bit and ask nodered to reconnect. But I have no idea how to do that.  
Sometimes it reconnect by itself but most of the time it doesn't.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 August 2019 11:25 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/6 "2019-08-21T11:25:49Z")

</div>

> [@Charlotte](#):
>
> Sometimes it reconnect by itself but most of the time it doesn't.

I suggest you raise an issue against the node, it should reliably reconnect without you doing anything.  
I don't know whether there has been any work on that node that might have improved it recently, are you using the latest version?

---

<div class="post-metadata">

### Author: ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)
#### Post date: [21 August 2019 11:53 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/7 "2019-08-21T11:53:44Z")

</div>

OK thanks  
I installed nodered and serial port a few weeks ago so it is probably the latest version.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [21 August 2019 18:26 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/8 "2019-08-21T18:26:15Z")

</div>

It should reconnect automatically on a disconnect... but sometimes we get reports that once it disconnects it then comes back as another port so then of course it won't reconnect.... is that the case here ? if/when it does it again can you list he serial ports you now have available (I would say `ls /dev/ttyU*` but you have renamed them, so I'll let you figure that out...)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [21 August 2019 20:56 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/9 "2019-08-21T20:56:25Z")

</div>

I believe that this wouldn't be the case here as she has given the ports fixed names already.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [22 August 2019 01:24 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/10 "2019-08-22T01:24:12Z")

</div>

Depends how they were (re)named.

---

<div class="post-metadata">

### Author: ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)
#### Post date: [22 August 2019 06:55 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/11 "2019-08-22T06:55:08Z")

</div>

Yes I renamed them also because I observed they changed names (ttyUSB\*) when reconnecting.

To rename them I created a file 10-local.rules in /etc/udev/rules.d like this :

```auto
KERNEL=="ttyUSB*", KERNELS=="1-1.1.3.4", SYMLINK+="port_usb_1"
KERNEL=="ttyUSB*", KERNELS=="1-1.1.3.7", SYMLINK+="port_usb_2"
KERNEL=="ttyUSB*", KERNELS=="1-1.1.3.6", SYMLINK+="port_usb_3"
KERNEL=="ttyUSB*", KERNELS=="1-1.1.3.5", SYMLINK+="port_usb_4"
KERNEL=="ttyUSB*", KERNELS=="1-1.1.3.1", SYMLINK+="port_usb_5"
KERNEL=="ttyUSB*", KERNELS=="1-1.1.3.2", SYMLINK+="port_usb_6"
KERNEL=="ttyUSB*", KERNELS=="1-1.1.3.3", SYMLINK+="port_usb_7"
KERNEL=="ttyUSB*", KERNELS=="1-1.1.2", SYMLINK+="port_usb_8"
KERNEL=="ttyUSB*", KERNELS=="1-1.3", SYMLINK+="port_usb_9"
KERNEL=="ttyUSB*", KERNELS=="1-1.2", SYMLINK+="port_usb_10"

```

After disconnection if I check my ports on the cmd console with

```auto
ls -l /dev/port_usb_1

```

I can find it, but it has not reconnected in Nodered.

---

<div class="post-metadata">

### Author: ![maxwellhadley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxwellhadley/32/7827_2.png) [@maxwellhadley](https://discourse.nodered.org/u/maxwellhadley)
#### Post date: [23 August 2019 15:00 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/12 "2019-08-23T15:00:20Z")

</div>

I fought and won this battle many moons ago with node-red-contrib-rfxcom, though recent versions of the serialport node did cause some trouble. It is a case of listening to serialport events, and using a timer to attempt reconnection at regular intervals if a disconnection is detected. But this is USB-serial device specific, and may cause problems with actual hardware UARTs. It could be made an option on the serialport node, maybe?

_Edit to add:_ The logic is split between `node-red-contrib-rfxcom` and `node-rfxcom`.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [23 August 2019 16:56 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/13 "2019-08-23T16:56:40Z")

</div>

I have also seen this sometimes. To solve the issue, I use a utility named usbreset (with google you will find it here: [https://github.com/jkulesza/usbreset](https://github.com/jkulesza/usbreset))

I configured NR to use a status node checking the status of rfx node. In case it showed "OK", everything is fine but if it shows "disconnected", the trigger node kicked in and activated the exec node. Shortly after, everything was working again. Today I do not recognize any communication problems with the RFXtrx

The command I use for the exec node is as follows:

```auto
export DISPLAY=:1 && xterm -geometry 96x24-450+450 -e sudo /home/pi/usbreset /dev/bus/usb/001/005

```

but, if you do not want it shown on the monitor screen, I guess you could just use

```auto
sudo /home/pi/usbreset /dev/bus/usb/001/005

```

(you have to find out on which usb device it is connected to, in my case 001/005)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/563a95ab565d42804441b9fef8e65a95d7c65289.png)

---

<div class="post-metadata">

### Author: ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)
#### Post date: [31 October 2019 09:07 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/14 "2019-10-31T09:07:47Z")

</div>

Hi,

Thanks for your advices.

RFXcom doesn't seem to fit my need compare to serialport nodes.

I tried using usbreset however I was not successful.  
I manage to reset the USB port however when it reconnects it is still in error ("waiting").

If I look for the devices with

```auto
ls -l /dev/usb_1

```

I find it properly (usb\_1 is the simlink given to the port)

If I deploy again Nodered app, the port reconnects.

I don't know how to trigger the reset from the dashboard, as a "deploy" does. Do you have any idea ?

Thanks for your help.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [31 October 2019 13:48 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/15 "2019-10-31T13:48:22Z")

</div>

> [@Charlotte](#):
>
> RFXcom doesn't seem to fit my need

RFXcom is a special product for a special purpose, obviously is of no interest if you just wanna do serial comms

Are you talking about "full deploy"? If so, I think it is restarting the flow and it might be that this in itself is releasing some handles to the serial port. In later versions of NR you have the possibility to restart the flow via the "Deploy" button in the editor. Next time you see the problem with the serial, you could try this to see if it is enough to fix it, just to verify

If it seems to help, you could implement a flow restarter in the dashboard using a template node, in this thread there is a possible solution discussed

> [@\[Solved\] Restarting flows programmatically](https://discourse.nodered.org/t/solved-restarting-flows-programmatically/9341):
>
> Since v0.20, the deploy menu has a new Restart Flows option. Would it be possible to provide a way to invoke this programmatically from within a flow? For example, in cases where message properties have been used to override node parameters set when a node was deployed (not encouraged, I realize, but sometimes a good idea), it could be useful to be able to recover the initial state of the flow.

---

<div class="post-metadata">

### Author: ![Charlotte](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@Charlotte](https://discourse.nodered.org/u/Charlotte)
#### Post date: [8 November 2019 14:15 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/16 "2019-11-08T14:15:30Z")

</div>

Thanks a lot, this is working for me.  
The remaining issue I have to solve is that it's reloading all flows, so I have some undesired behaviours.

---

<div class="post-metadata">

### Author: ![ekawahyu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ekawahyu/32/15723_2.png) [@ekawahyu](https://discourse.nodered.org/u/ekawahyu)
#### Post date: [3 January 2020 01:21 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/17 "2020-01-03T01:21:02Z")

</div>

@Charlotte have you found a solution for disconnect/reconnect issue with USB serial device as symlink? I have been struggling to get this right, but restarting the flow is not the solution for me. I have an open session in Node Red that I don't want to loose by restarting the flow.

---

<div class="post-metadata">

### Author: ![ekawahyu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ekawahyu/32/15723_2.png) [@ekawahyu](https://discourse.nodered.org/u/ekawahyu)
#### Post date: [3 January 2020 03:17 UTC](https://discourse.nodered.org/t/how-to-manage-serial-port-disconnection/14592/18 "2020-01-03T03:17:49Z")

</div>

@Charlotte I may have solved it myself with a temporary solution. It seems that something has changed or broken with node-serialport \>=0.9.0? I can make it work by installing earlier version of it. It works just fine on my platform with node-serialport 0.8.8, and symlinks to physical ports work as well in my box.
