mmwaseem
Posts: 1
Joined: Sun Jun 24, 2018 5:32 am

NodeJS: How to send code string (command) to projector from NodeJS?

Sun Jun 24, 2018 6:41 am

Hi there,

I'm new on this forum and this is my first post.

I have a projector from Sony. I need to understand what will be the right way / right command to send to the projector.

Sample found in technical document for simple commands:

Power On
A9 17 2E 00 00 00 3F 9A
0xA9,0x17,0x2E,0x00,0x00,0x00,0x3F,0x9A

Power OFF
A9 17 2F 00 00 00 3F 9A
0xA9,0x17,0x2F,0x00,0x00,0x00,0x3F,0x9A

Port setting is 38400, Parity even, stop bits 1, Hardware Flow control none, send Hex bits.

I'm using NodeJS and serialport in order to send commands to the projector.

var SerialPort = require('serialport');
var port = new SerialPort('/dev/ttyUSB0', { //Edited from ttyAMA0 to ttyUSB0 as I have USB to RS232 connector
baudRate: 9600,
databits: 8,
parity: 'none'
}, false);

var cmd = "A9 17 2E 00 00 00 3F 9A" // I tried this as a command

//var cmd = "0xA9,0x17,0x2E,0x00,0x00,0x00,0x3F,0x9A" // I also tried this as command


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

Console log says "message written", but I'm unsure the command is reaching projector or if it's reaching whether it's right format as it should be.

I'm connecting Raspberry Pi 3 with USB 2.0 to RS232 converter (DB9). Thank you in advance.
https://ae.linkedin.com/in/mmwaseem

Heater
Posts: 9832
Joined: Tue Jul 17, 2012 3:02 pm

Re: NodeJS: How to send code string (command) to projector from NodeJS?

Sun Jun 24, 2018 11:47 pm

You need to send binary data not strings. The node-serialport documentation says:
You can write to the serial port by sending a string or buffer to the write method:

port.write('Hi Mom!');
port.write(Buffer.from('Hi Mom!'));
From the node.js documentation we find:
Prior to the introduction of TypedArray, the JavaScript language had no mechanism for reading or manipulating streams of binary data. The Buffer class was introduced as part of the Node.js API to enable interaction with octet streams in TCP streams, file system operations, and other contexts.
Putting two and two together, the way to send your binary bytes is as follows:

Code: Select all

let cmd = Buffer.from([0xA9, 0x17, 0x2F, 0x00, 0x00, 0x00, 0x3F, 0x9A]))

port.write(cmd, function(err) {
    if (err) {
        return console.log('Error on write: ', err.message);
    }
    console.log('message written');
});
We also read from the node.js buffer documentation that:
With TypedArray now available, the Buffer class implements the Uint8Array API in a manner that is more optimized and suitable for Node.js.
So perhaps one could use that instead:

Code: Select all

let cmd = new Uint8Array([0xA9,0x17,0x2F,0x00,0x00,0x00,0x3F,0x9A])
https://github.com/node-serialport/node-serialport
https://nodejs.org/api/buffer.html

Return to “Other programming languages”

Who is online

Users browsing this forum: No registered users and 4 guests