0

I have googled and searched alot for my issues but did not get any threat of use. I have the same issue as https://github.com/yaacov/node-modbus-serial/issues/115 but i am using a Windows 10 OS. I tried the suggested solutions but still same issue. I think it should be simple but been stuck for days now

    var path = "COM3";
    var ModbusRTU = require("modbus-serial");
    var client = new ModbusRTU();
    client.connectRTUBuffered(path, { baudRate: 9600, stopbits: 1, databits: 8, parity: 'none' }, false, read());
    client.setID(1);
      
function read() {
    // read the 20 registers starting at address 0
    // on device number 1.
    var data = client.readHoldingRegisters(0, 20)
    console.log(data)
} 

With this code i get no port permission/ permission denied error. PORT INFORMATION via Device manager port properties

  1. BITS PER SECOND: 9600
  2. DATA BITS: 8
  3. PARITY: NONE
  4. STOP BITS: 1
  5. FLOW CONTROL: NONE

OUTPUT 1: (node:10784) UnhandledPromiseRejectionWarning: Error: Opening COM3: Access denied

Tried to open the port manually

var x = setInterval(function () {
if (client.isOpen) {
    client.setID(1);
    //console.log("WORKING WORKING");
    try {
        client.readHoldingRegisters(0, 10, function (err, data) {
            console.log(data);
        });
    } catch (err) {
        console.log("Error Encountered: " + err)
    }
} else {
    console.log("ERROR ERROR ERROR");
    }

}, 1000);

  • Hello, if you want your question to get an answer consider including the details of the device (smart meter?) you are trying to connect. Are you sure you have the right port, baud rate etc.? What about the register number? Also add details of your cable and or wiring to the device. – Marcos G. Feb 03 '21 at 17:04
  • yeah sorry about that...the device is an electric meter, RS 485 protocol. and yeah i do have the right port. I checked it on device manager. I have successfully ran python version of this code but could not do the same via node.Sorry my bad for not using all the parameters. I have update the code with all the parameters i use. Thank you – Sanam Shrestha Feb 03 '21 at 20:24
  • I see. I think it would be a good idea to add that python code that works to your question. Maybe there are differences you are missing? – Marcos G. Feb 04 '21 at 15:52
  • Thank you for your response. Here is the python version `client = ModbusClient(method='rtu', port='/dev/ttyUSB0', timeout=1, stopbits=1, bytesize=8, parity='N', baudrate=9600) print(client.connect()) address = 0 request = client.read_holding_registers(address=0, count=100) print(request.registers) client.close()` – Sanam Shrestha Feb 04 '21 at 16:26
  • I wonder why your port is '/dev/ttyUSB0' in your python code and 'COM3' on Nodejs. Are you switching from Linux to Windows? – Marcos G. Feb 04 '21 at 19:01
  • yeah sorry about that. python was done on UBUNTU and COM3 is the port path for windows – Sanam Shrestha Feb 05 '21 at 13:24
  • I am using node on a windows laptop. I wonder why port access is denied in windows using node but, it works fine in python for both windows and Ubuntu. Do we have to somehow give port access permission to node.js specifically? I tried using terminal as an administrator as well. Still same error – Sanam Shrestha Feb 05 '21 at 14:12
  • I think you might need to open and close your port manually, have you read [this](https://github.com/serialport/node-serialport/issues/1502)? – Marcos G. Feb 05 '21 at 16:10
  • I have tried that as well. I could not find a way to paste the code here in the comment section so i have updated my question and pasted there. Please do take a look at the code. No error and no Output as well. It just keeps on printing Working W – Sanam Shrestha Feb 06 '21 at 13:29
  • the code you wrote above does not seem to try to open the port, you are just checking if the port is already open. I guess it's not, that why you see the message. Did you look at the link on my previous comment? In particular, you should be defining your port with the `autoOpen:false` option and then opening the port manually with `this.port.open(...)` – Marcos G. Feb 06 '21 at 14:44
  • yeah acutally i did so but the code was long so i was unable to put all at once.. https://github.com/snmSthNepal/batti/blob/master/index.js here is the soure code. Please do check it when you can. Thank you in advance. I also tried editing the modbus-serial library file from https://github.com/yaacov/node-modbus-serial/blob/master/apis/connection.js?fbclid=IwAR395gYOjpl09YVYq3VgPH2vsRGMdFpZqOVTiSEAFgb6SBilECg9TwHiHRM and for now stuck trying to edit this import file somehow (hit and trial method). – Sanam Shrestha Feb 06 '21 at 16:20
  • Hello. I just tested your code and it works fine on my setup. When I unplug my port (it's a USB to serial converter) I get `ERROR ERROR ERROR`. With the port connected and just changing your COM3 to my `/dev/ttyUSB0` I get the expected messages (`WORKING WORKING` plus `TESTING TESTING TESTING`) and most importantly I see the LED on my device blinking, meaning it's transmitting frames... If I have to guess you are experiencing issues with your driver or other Windows related problems. Have you tested your port some other way on Windows? maybe you can try with python... – Marcos G. Feb 07 '21 at 07:49
  • Sorry for the delayed reply... Thank you for your help.. I guess something wrong with windows... Tried myself on Ubuntu and it worked fine.. Thank you again for your time and effort @MarcosG. – Sanam Shrestha Mar 22 '21 at 09:44
  • great, good to hear it's working. You are welcome. – Marcos G. Mar 22 '21 at 14:15

0 Answers0