Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to read the data from serial port #1668

Closed
ghost opened this issue Sep 20, 2018 · 9 comments
Closed

Not able to read the data from serial port #1668

ghost opened this issue Sep 20, 2018 · 9 comments

Comments

@ghost
Copy link

ghost commented Sep 20, 2018

Hi,

My usecase is I am running the NodeJS code to fetch the data from the custom made shield which uses the Modbus protocol for communication. I am using serial port to send a request to the shield and get back the response from the shield. I have written the code in NODE-RED, which has a serial port and it is able to send request and receive response. I have to connect to port /dev/ttyS0. My board in Intel Galelio Board and the board is connected to the shield via arduino pins. I have written the following code to establish the communication but the message (request) is written but I do not get any response. Please help me with this.

var serialport = require('serialport');
var portName = '/dev/ttyS0';

var port = new serialport(portName, {
    baudRate: 115200,
	dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false
});

port.on('open', onOpen);

function onOpen(){
    console.log('Open connections!');
}

function onData(data){
    console.log('on Data ' + data);
}

port.on('readable', function () {
	console.log('Read Data:', port.read());
});

var buffer = new Buffer(9);
buffer[0] = 0x01;
buffer[1] = 0x30;
buffer[2] = 0x02;
buffer[3] = 0x07;
buffer[4] = 0x00;
buffer[5] = 0x00;
buffer[6] = 0x02;
buffer[7] = 0x00;
buffer[8] = 0x00;

port.write(buffer, function(err) {
  if (err) {
    return console.log('Error on write: ', err.message);
  }
  console.log('message written');
});

port.on('data', onData);
@reconbot
Copy link
Member

reconbot commented Sep 20, 2018

You don't want to mix port.read() with post.on('data', ...) use one or the other. I also recommended turning on debug mode https://serialport.io/docs/guide-debugging#debugging-output to see what it's doing under the hood

@ghost
Copy link
Author

ghost commented Sep 21, 2018

I get the following output when I use the debug mode.

$ DEBUG=* node flow.js
@serialport/bindings loading LinuxBinding +0ms
@serialport/stream opening path: /dev/ttyS0 +0ms
@serialport/binding-abstract open +0ms
@serialport/stream _read queueing _read for after open +189ms
@serialport/bindings:poller Creating poller +0ms
@serialport/stream opened path: /dev/ttyS0 +74ms
Open connections!
@serialport/stream _write 9 bytes of data +130ms
@serialport/binding-abstract write 9 bytes +331ms
@serialport/stream _read reading +40ms
@serialport/binding-abstract read +40ms
@serialport/bindings:unixWrite Starting write 9 bytes offset 0 bytesToWrite 9 +0ms
@serialport/bindings:unixRead Starting read +0ms
@serialport/bindings:unixWrite write returned null 9 +50ms
@serialport/bindings:unixWrite wrote 9 bytes +27ms
@serialport/bindings:unixWrite Finished writing 9 bytes +2ms
@serialport/stream binding.write write finished +105ms
message written
@serialport/bindings:unixRead waiting for readable because of code: EAGAIN +76ms
@serialport/bindings:poller Polling for "readable" +311ms

I am not getting any data. What do you suggest ?

@reconbot
Copy link
Member

reconbot commented Sep 21, 2018 via email

@ghost
Copy link
Author

ghost commented Sep 24, 2018

Yes the baud rate is correct. It is working perfectly in Node RED framework but not in Node JS. So, What do you suggest ?

@ghost
Copy link
Author

ghost commented Sep 25, 2018

Is it any problem in the code or the library ?
I am trying all possible mechanisms to read data but it is not possible. Could anyone provide an insight inot the solution ?

@HipsterBrown
Copy link
Contributor

@Vishal000 What version of serialport are you using?

@ghost
Copy link
Author

ghost commented Sep 26, 2018

Node: 6.12.3
Serialport: 6.2.2

@ghost
Copy link
Author

ghost commented Sep 28, 2018

Can anyone please help with this situation ?

@stale
Copy link

stale bot commented Nov 27, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week no further activity occurs. Feel free continue the discussion or ask for a never-stale label to keep it open. If this is a support issue, consider sharing it on stack overflow to get more eyes on your problem.

@stale stale bot added the stale-issue label Nov 27, 2018
@stale stale bot closed this as completed Dec 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants