# Trouble with serialport. Error code 31

**URL:** <https://discourse.nodered.org/t/trouble-with-serialport-error-code-31/84784>\
**Category:** General\
**Tags:** node-red-node-serialport\
**Created:** [22 January 2024 06:45 UTC](https://discourse.nodered.org/t/trouble-with-serialport-error-code-31/84784 "2024-01-22T06:45:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nightmareside15](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nightmareside15/32/56176_2.png) [@nightmareside15](https://discourse.nodered.org/u/nightmareside15)\
**Post date:** [22 January 2024 06:45 UTC](https://discourse.nodered.org/t/trouble-with-serialport-error-code-31/84784/1 "2024-01-22T06:45:44Z")

</div>

Hello. I'm having trouble to use serialport in node red to connect with my GPT-9803. The connect I was using is rs232C ( using usb to rs232C with CH340 CHIP). I'm successfully connect it via tera-term and getting feedback from the machine, but when I tried to use node red serialport it always having this error:

"[serialconfig:58065d3ff9e25096] serial port COM3 error: Error: Open (SetCommState): Unknown error code 31"

This is the output from tera term, if I tried to send "idn?" command to the machine:  
 ![teraterm out](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9da1374325d00df406e69f406ee77371a78efe2d.png)

And this is node-red when I tried to connect it:

 ![nr-result](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8d3cc4c5b4cc39611f3fdd2438bb55b8975468c8.png)

```auto
[
    {
        "id": "52d7084d2efd8fbc",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "ea5031698f6e236f",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "1631f6c1af7f2963",
        "type": "ui_base",
        "theme": {
            "name": "theme-light",
            "lightTheme": {
                "default": "#0094CE",
                "baseColor": "#0094CE",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
                "edited": true,
                "reset": false
            },
            "darkTheme": {
                "default": "#097479",
                "baseColor": "#097479",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif",
                "edited": false
            },
            "customTheme": {
                "name": "Untitled Theme 1",
                "default": "#4B7930",
                "baseColor": "#4B7930",
                "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
            },
            "themeState": {
                "base-color": {
                    "default": "#0094CE",
                    "value": "#0094CE",
                    "edited": false
                },
                "page-titlebar-backgroundColor": {
                    "value": "#0094CE",
                    "edited": false
                },
                "page-backgroundColor": {
                    "value": "#fafafa",
                    "edited": false
                },
                "page-sidebar-backgroundColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "group-textColor": {
                    "value": "#1bbfff",
                    "edited": false
                },
                "group-borderColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "group-backgroundColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "widget-textColor": {
                    "value": "#111111",
                    "edited": false
                },
                "widget-backgroundColor": {
                    "value": "#0094ce",
                    "edited": false
                },
                "widget-borderColor": {
                    "value": "#ffffff",
                    "edited": false
                },
                "base-font": {
                    "value": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"
                }
            },
            "angularTheme": {
                "primary": "indigo",
                "accents": "blue",
                "warn": "red",
                "background": "grey",
                "palette": "light"
            }
        },
        "site": {
            "name": "Node-RED Dashboard",
            "hideToolbar": "false",
            "allowSwipe": "false",
            "lockMenu": "false",
            "allowTempTheme": "true",
            "dateFormat": "DD/MM/YYYY",
            "sizes": {
                "sx": 48,
                "sy": 48,
                "gx": 6,
                "gy": 6,
                "cx": 6,
                "cy": 6,
                "px": 0,
                "py": 0
            }
        }
    },
    {
        "id": "5ad7a3c2f4cb1f09",
        "type": "ui_group",
        "name": "Default",
        "tab": "ea5031698f6e236f",
        "order": 1,
        "disp": false,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "a6e358b.672ffa8",
        "type": "ui_group",
        "name": "Demo",
        "tab": "4e528085.a1bfa",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "4e528085.a1bfa",
        "type": "ui_tab",
        "name": "Demo",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    },
    {
        "id": "58065d3ff9e25096",
        "type": "serial-port",
        "name": "",
        "serialport": "COM3",
        "serialbaud": "9600",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "\\n",
        "bin": "false",
        "out": "char",
        "addchar": "",
        "responsetimeout": "10000"
    },
    {
        "id": "f57557155a8ddb6f",
        "type": "serial in",
        "z": "52d7084d2efd8fbc",
        "name": "",
        "serial": "58065d3ff9e25096",
        "x": 490,
        "y": 280,
        "wires": [
            [
                "8fc2ed2bd72f2b24"
            ]
        ]
    },
    {
        "id": "8fc2ed2bd72f2b24",
        "type": "debug",
        "z": "52d7084d2efd8fbc",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 320,
        "wires": []
    },
    {
        "id": "c15a8906993c6221",
        "type": "inject",
        "z": "52d7084d2efd8fbc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "*idn?",
        "payloadType": "str",
        "x": 190,
        "y": 400,
        "wires": [
            [
                "34fcfe862721a4f3"
            ]
        ]
    },
    {
        "id": "34fcfe862721a4f3",
        "type": "serial out",
        "z": "52d7084d2efd8fbc",
        "name": "",
        "serial": "58065d3ff9e25096",
        "x": 350,
        "y": 400,
        "wires": []
    }
]

```

I wonder what cause it? Using windows 10 pro. Thank you very much!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [22 January 2024 07:27 UTC](https://discourse.nodered.org/t/trouble-with-serialport-error-code-31/84784/2 "2024-01-22T07:27:30Z")

</div>

I don’t know too much about how windows handles its drivers.

But given its working in tera term - we should rule out drivers.

Next thing(s) to check

- The serial port is not in use at the same time
- check the settings (baudrate, stop bits) etc  
In tera term and match them in Node RED
- NodeJS runtime architecture (32, 64 bit)

---

<div class="post-metadata">

**Author:** ![nightmareside15](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nightmareside15/32/56176_2.png) [@nightmareside15](https://discourse.nodered.org/u/nightmareside15)\
**Post date:** [22 January 2024 07:49 UTC](https://discourse.nodered.org/t/trouble-with-serialport-error-code-31/84784/3 "2024-01-22T07:49:21Z")

</div>

Thanks for the reply!

1. Checking the serial port to make sure it doesn't connect to terra term before using node red. If the serial is being used, I've seen a new error that saying  
"[serialconfig:58065d3ff9e25096] serial port COM3 error: Error: Opening COM3: Access denied"  
So I think it safe as I didn't have that error.

2. Here is the setting, both in tera-term and node-red:  
 ![teraterm serial setting](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4deeeb7e3ddad02e6e217cbcaa3dd32faa7b1ee.png)  

3. It is 64 bit. Im using

```auto
node -p "process.arch"

```

Oh yeah. Is using different OS will have a different output? I haven't tried it on ubuntu as I need to install the ch340 driver first on the ubuntu

UPDATE: It seems re-installing the CH340 did the trick. Now node red indicate that the connection is connected. The interesting thing is I can inject "\*idn?" to the machine just fine but somehow didn't receive anything on the output side of the node red. However, when I open tera term once more and use "idn?" command, it seems the previous node-red inject end up printed here.I inject like 10 times, and it also printed extra 10 times when I send "idn?" from the tera -term. Does it has anything to do with CR or LF?

![tera-term terminal](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/443b68e0d21ded3a8eefa96a2cf35e3726117032.png)

UPDATE 3: It is working! It seems I need to add \r everytime I finished the input!  
Thank you for the answer! The settings is a bit different in the input side!

Here is the updated flow for anyone that might need it.

```auto
[
    {
        "id": "d5a2884de61808a4",
        "type": "serial in",
        "z": "52d7084d2efd8fbc",
        "name": "",
        "serial": "58065d3ff9e25096",
        "x": 530,
        "y": 260,
        "wires": [
            [
                "4b8992af7b45d485"
            ]
        ]
    },
    {
        "id": "a5c8ce124ab72cb3",
        "type": "serial out",
        "z": "52d7084d2efd8fbc",
        "name": "",
        "serial": "58065d3ff9e25096",
        "x": 430,
        "y": 320,
        "wires": []
    },
    {
        "id": "83ebbb84e030d34b",
        "type": "inject",
        "z": "52d7084d2efd8fbc",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "*idn?",
        "payloadType": "str",
        "x": 130,
        "y": 320,
        "wires": [
            [
                "81799e70251bfbe4"
            ]
        ]
    },
    {
        "id": "4b8992af7b45d485",
        "type": "debug",
        "z": "52d7084d2efd8fbc",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 740,
        "y": 260,
        "wires": []
    },
    {
        "id": "81799e70251bfbe4",
        "type": "function",
        "z": "52d7084d2efd8fbc",
        "name": "function 1",
        "func": "msg.payload = msg.payload+\"\\r\"; \nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 320,
        "wires": [
            [
                "a5c8ce124ab72cb3"
            ]
        ]
    },
    {
        "id": "58065d3ff9e25096",
        "type": "serial-port",
        "name": "",
        "serialport": "COM3",
        "serialbaud": "9600",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "\\n",
        "bin": "false",
        "out": "char",
        "addchar": "",
        "responsetimeout": "10000"
    }
]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 February 2024 07:49 UTC](https://discourse.nodered.org/t/trouble-with-serialport-error-code-31/84784/4 "2024-02-05T07:49:48Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
