# A problem of executing bin file generated by C

**URL:** <https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341>\
**Category:** Developing Nodes\
**Created:** [29 January 2023 09:07 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341 "2023-01-29T09:07:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![magicfire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magicfire/32/93420_2.png) [@magicfire](https://discourse.nodered.org/u/magicfire)\
**Post date:** [29 January 2023 09:07 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/1 "2023-01-29T09:07:43Z")

</div>

We recently attempt to use tuya cloud in Node-red, and we have found out that tuya's SDK is base on C--[tuya SDK](https://github.com/tuya/tuya-iot-core-sdk).  
After following the instructions, we do compliled and got the executable bin file "subdevice\_basic\_demo". It dose work in command line like this, and links to the tuya cloud well.

```auto
croot@PN6400:~# cd /home/tuya/tuya-iot-core-sdk-main/build/bin
root@PN6400:/home/tuya/tuya-iot-core-sdk-main/build/bin# ./subdevice_basic_demo 

```

But when we call the bin file in js file like this:

```auto
var fs = require("fs");
var exec = require('child_process').exec;

module.exports = function(RED) {
    function PN6400_tuyaInit(config) {
					RED.nodes.createNode(this,config);
					var node = this;
					this.key = RED.nodes.getNode(config.key);

					node.on('input', function(msg) {
						//run ('export PATH="/home/tuya/tuya-iot-core-sdk-main/build/bin/:$PATH"') ahead of time 
						exec('subdevice_basic_demo'+' ',function(error,stdout,stderr){
							if(error) {
								console.info('stderr : '+stderr);
							}
						});
					});
    }
    RED.nodes.registerType("PN6400_tuyaInit",PN6400_tuyaInit);
}

```

Then nothing happened, aka the executable file "subdevice\_basic\_demo" never executed.  
So anyone can help us?

---

<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:** [29 January 2023 09:22 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/2 "2023-01-29T09:22:46Z")

</div>

Is the problem that it is not calling your code or that the code does not work when called like that? You can check by adding some logging to your C code so that it, for example, logs what is happening to file.

---

<div class="post-metadata">

**Author:** ![magicfire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magicfire/32/93420_2.png) [@magicfire](https://discourse.nodered.org/u/magicfire)\
**Post date:** [29 January 2023 09:37 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/3 "2023-01-29T09:37:18Z")

</div>

The code does not work when called.  
And I have added some logging codes in the C file, but still nothing happended.

FYI  
If I replace the bin file with a simple python file instead, the python file works.

```auto
exec('#a python file instead #'+' ',function(error,stdout,stderr){
							if(error) {
								console.info('stderr : '+stderr);
							}
						});

```

So the problem should not be the JS calling part itself.

---

<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:** [29 January 2023 09:43 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/4 "2023-01-29T09:43:36Z")

</div>

If it does work outside of Node-RED from the command line, then you proabably either need the full path to the command when you call it - or the environment that you are running Node-RED is not the same as the user you are using to run from command line.

---

<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:** [29 January 2023 09:44 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/5 "2023-01-29T09:44:01Z")

</div>

1. Try using `./subdevice_basic_demo`
2. Is the executable available in the cwd? Node-red current working directory will NOT be `/home/tuya/tuya-iot-core-sdk-main/build/bin` (i.e. try using full path) and check the account running node-red can access and execute the binary)

Ps. Instead of calling exec, you might want to investigate adding `c` bindings to call the program/functions directly: [How to call C/C++ code from Node.js | by Konstantin Tarkus | Medium](https://koistya.medium.com/how-to-call-c-c-code-from-node-js-86a773033892)

---

<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:** [29 January 2023 12:06 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/6 "2023-01-29T12:06:56Z")

</div>

Why not use one of the existing Node.js wrappers for Tuya?

[tuya - npm search (npmjs.com)](https://www.npmjs.com/search?q=tuya)

---

<div class="post-metadata">

**Author:** ![magicfire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magicfire/32/93420_2.png) [@magicfire](https://discourse.nodered.org/u/magicfire)\
**Post date:** [30 January 2023 05:43 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/7 "2023-01-30T05:43:01Z")

</div>

> [@magicfire](#):
>
> `/home/tuya/tuya-iot-core-sdk-main/build/bin/`

Thanks for your advice.  
The problem is solved.  
As I put the bin file under the path of /usr/bin, like this:

 ![1675057218487](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/715318a2f4a281413756a64fa2b5efcb0664c789.png)  
Then the JS code can call the bin file.  
PS: In both cmdline and Node-RED mode, the logged user is root.

---

<div class="post-metadata">

**Author:** ![magicfire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magicfire/32/93420_2.png) [@magicfire](https://discourse.nodered.org/u/magicfire)\
**Post date:** [30 January 2023 05:45 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/8 "2023-01-30T05:45:21Z")

</div>

Thanks.Right, the path is the key point.

---

<div class="post-metadata">

**Author:** ![magicfire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/magicfire/32/93420_2.png) [@magicfire](https://discourse.nodered.org/u/magicfire)\
**Post date:** [30 January 2023 05:47 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/9 "2023-01-30T05:47:24Z")

</div>

Surely this is a bright idea.  
I have planned to create some different functions from the existing libraries.

---

<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:** [31 March 2023 05:47 UTC](https://discourse.nodered.org/t/a-problem-of-executing-bin-file-generated-by-c/74341/10 "2023-03-31T05:47:45Z")

</div>

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