Take the 2-minute tour ×

I connected wavecom GSM modem on ubantu. I use node.js language to communicate with GSM modem. I send command to modem by Child Process. Here example

var spawn         = require("child_process").spawn,
    exec          = require('child_process').exec;

 // Write dev_ttyUSB15.tmp file
 var child = exec('cat < /dev/ttyUSB15 > /tmp/dev_ttyUSB15.tmp');

 // Read dev_ttyUSB15.tmp file
 var m1 = spawn('tail',['-f','/tmp/dev_ttyUSB15.tmp']);

 // on data event is emitted when dev_ttyUSB15.tmp file has some data 
 m1.stdout.on('data', function (data) {
     console.log("Data : "+data); // this is executed as output

Now When I fire some command on port /dev/ttyUSB15 I do not get output properly.


Suppose my output should be

Data : abcd1234

but instead of it I got

Data : abc
Data : d1234

In short My output is breaked. I can not extrapolate from where my output exactly break. It's random. Can anyone give me any idea?

Thanks in advance.

share|improve this question

2 Answers 2

It's hard to say without knowing what protocol you are speaking with the modem, but if it's e.g. \n delimited, you will have to buffer the data and split on \n:

var buffer = '';
    var received = (buffer + data).split('\n');
    buffer = received.pop().trim();
share|improve this answer
The key is that data will fire whenever it receives "enough". You need to buffer up the response. Take a look at Substack's Stream Handbook for more details. –  Adam Sanderson May 16 '13 at 17:44

As all streams in node.js, the reading of data consists of 2 separate events: data and end.

data event is fired when some data is readable in the stream (in your case, twice).

end event is fired when no more data events will be fired.

var blob = "";
 blob += data;

m1.stdout.on('end', function () {
 console.log("Data : " + blob); // here you have all the data within one variable
share|improve this answer
Sorry but "end" event will fire only when tail process kill externally...And if tail process is killed then I can't listen any data on port /dev/ttyUSB15... –  Paresh Thummar Mar 21 '12 at 10:24

Your Answer


