# Node Red TCP connection gives error (index a nil value) on MG400 Dobot

**URL:** https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857
**Category:** General
**Tags:** function-node
**Created:** [23 January 2025 23:13 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857 "2025-01-23T23:13:59Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)
#### Post date: [23 January 2025 23:13 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/1 "2025-01-23T23:13:59Z")

</div>

I have a dobot Mg400 with the following code:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef742cd061b56d172a15dce487c19db1c598708e.png)

And then a simple flow in node red to inject strings using TCP connection (MG400 is the server):

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13ac9764ad2178a7c46f3497ed09ead0f9f00d32.png)

The code is executed as I would expect, robot does commands and I get the reply back (ex. "ScannerIsEmpty!!"), but then dobot studio throws this error:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9a9e30b39aae49c1d3ee3a72bf30df56a696ae5a.png)  
src0.lua:18: attempt to index a nil value (global 'tcp1')

So why do you post it on the Node Red forum and not in Dobot forum? Because it does not do it when i inject from an app on my phone or the debugging tool in dobot studio, only when done from Node Red. It also does not do it, if i use the TCP out node... but then i wont get a reply and i need that. So what does the TCP request node in Node red do differently to provoke this error?

```auto
[
    {
        "id": "c4396e2906bd2811",
        "type": "tab",
        "label": "Server",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "fa6a975f06c0d443",
        "type": "inject",
        "z": "c4396e2906bd2811",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "GetFoto",
        "payloadType": "str",
        "x": 120,
        "y": 200,
        "wires": [
            [
                "3710ca17baf02557"
            ]
        ]
    },
    {
        "id": "dddbf63d43621b34",
        "type": "inject",
        "z": "c4396e2906bd2811",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "EmSc",
        "payloadType": "str",
        "x": 110,
        "y": 280,
        "wires": [
            [
                "3710ca17baf02557"
            ]
        ]
    },
    {
        "id": "3710ca17baf02557",
        "type": "tcp request",
        "z": "c4396e2906bd2811",
        "name": "",
        "server": "192.168.1.6",
        "port": "6601",
        "out": "time",
        "ret": "string",
        "splitc": "0",
        "newline": "",
        "trim": false,
        "tls": "",
        "x": 400,
        "y": 280,
        "wires": [
            [
                "602439f8493fcfd9"
            ]
        ]
    },
    {
        "id": "602439f8493fcfd9",
        "type": "debug",
        "z": "c4396e2906bd2811",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 660,
        "y": 280,
        "wires": []
    }
]

```

---

<div class="post-metadata">

### Author: ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)
#### Post date: [24 January 2025 06:59 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/2 "2025-01-24T06:59:25Z")

</div>

Just tried with python on my node red pc, that yielded the same error as the node red. Is this some ip config that I have wrong on my pc? Will try to swap pc tomorrow (need to set it up).

---

<div class="post-metadata">

### Author: ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)
#### Post date: [25 January 2025 08:10 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/3 "2025-01-25T08:10:12Z")

</div>

So it's not hardware specific, but software. Tried another pc: Node red, same error, random tcp client program no problem (same as tcp app on my phone). Somehow the format of the message is different.

Tried adding \n to the string, did not help.

Is it because it gets passed as an object and not a clean String?

Any ideas?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 January 2025 08:43 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/4 "2025-01-25T08:43:59Z")

</div>

you could use wireshark or tcpdump to capture the traffic for node-red vs your working python script and compare them so see what is different.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 January 2025 08:56 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/5 "2025-01-25T08:56:26Z")

</div>

Does the dobot software not have a well known protocol like MQTT or HTTP or something? That would be far easier and far more reliable than rolling your own. For example, your raw TCP scratch routine doesn't check for any start markers or end markers or use any kind of CRC for error checking so will be flakey and susceptible to buffered coms etc.

---

<div class="post-metadata">

### Author: ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)
#### Post date: [25 January 2025 11:45 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/6 "2025-01-25T11:45:55Z")

</div>

I figured it out. It was because Python and Node Red pr. default closes down the TCP connection after end message, which my TCP clients on my phone and computer did not. So its Dobots way of saying "I lost connection", which in my opinion is not the most user-friendly way... but I am by no way a coder by profession, so maybe this was obvious to a more experienced coder.

Fix:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aaa580de429128f8598749d8e90807987e84fae2.png)

---

<div class="post-metadata">

### Author: ![Palmhoej](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Palmhoej](https://discourse.nodered.org/u/Palmhoej)
#### Post date: [25 January 2025 11:51 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/7 "2025-01-25T11:51:06Z")

</div>

Hi Steve

Thanks for your replies. No unfortunatly it only commes with TCP or Modbus. It has its small quirks, but for the price i am quit impressed. Its silent, precise and fast and it costs around 2k $

I am making an application for taking old polaroid fotos in and out of a scanner, so i do not have to stand and wait for 2min pr. scan:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf665ea47c5de1d3ba0850247c8827af88353df4.jpeg)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 January 2025 13:14 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/8 "2025-01-25T13:14:40Z")

</div>

That looks cool.

Modbus might be a good bet (there are modbus nodes)

---

<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: [8 February 2025 13:14 UTC](https://discourse.nodered.org/t/node-red-tcp-connection-gives-error-index-a-nil-value-on-mg400-dobot/94857/9 "2025-02-08T13:14:57Z")

</div>

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