Getting Started

Estimated reading time: 4 minutes

The following describes how to send the RS-232C command with serial_control.js and Node.js.

Requirements

  • BRAVIA Professional Display
  • PC
  • Serial cable (such as USB to 3.5 mm Mini Plug TTL Serial Cable)

Preparation

Set up BRAVIA Professional Display

  • Connect BRAVIA Professional Display and the PC with the serial cable
  • Set the RS232C control to Via serial port
      1. [HOME] Settings
      2. RS232C control
      3. Via serial port
  • Switch to Normal mode or Pro mode by using the Pro settings tool

Set up PC

  • Install Node.js (v6.x and later) and npm on the PC
  • Install node-serialport
    • C:\>npm install serialport -g
  • Download serial_control.zip
  • Extract the zip file to a specific folder (ex: C:\serial_control>)

Execution

  1. You can get a list of serial port names by running ‘node serial_control.js’ with no argument.
    C:\serial_control>node serial_control.js
    Usage: node serial_control.js [PORTNAME] [CMDNAME]
    .
    Commands:
      auto_wide_off
      auto_wide_on
      auto_wide_toggle
      ...
      (*snip*)
      ...
    .
    Serial Ports:
      [PORTNAME]                    [VENDOR]
      COM1                          (*snip*)
      COM2                          (*snip*)
      COM3                          (*snip*)
    
  2. Send the power_off command, and then BRAVIA Professional Display will power off
    C:\serial_control>node serial_control.js COM1 power_off
    Port 'COM1' opened.
      Request[0]=0x8C
      Request[1]=0x00
      Request[2]=0x00
      Request[3]=0x02
      Request[4]=0x00
      Request[5]=0x8E
    Command 'power_off' written.
      Response[0]=0x70: ANSWER
      Response[1]=0x00: Completed (Normal)
      Response[2]=0x70: Checksum matched
    
  3. Send the power_get command, and then 0x00 (Power Off) will be returned in Return Data[0]
    C:\serial_control>node serial_control.js COM1 power_get
    Port 'COM1' opened.
      Request[0]=0x83
      Request[1]=0x00
      Request[2]=0x00
      Request[3]=0xFF
      Request[4]=0xFF
      Request[5]=0x81
    Command 'power_get' written.
      Response[0]=0x70: ANSWER
      Response[1]=0x00: Completed (Normal)
      Response[2]=0x02: Return Data Size
      Response[3]=0x00: Return Data[0]
      Response[4]=0x72: Checksum matched
    
  4. Send the power_on command, and then BRAVIA Professional Display will power on
    C:\serial_control>node serial_control.js COM1 power_on
    Port 'COM1' opened.
      Request[0]=0x8C
      Request[1]=0x00
      Request[2]=0x00
      Request[3]=0x02
      Request[4]=0x01
      Request[5]=0x8F
    Command 'power_on' written.
      Response[0]=0x70: ANSWER
      Response[1]=0x00: Completed (Normal)
      Response[2]=0x70: Checksum matched
    
  5. Send the power_get command again, and then 0x01 (Power On) will be returned in Return Data[0]
    C:\serial_control>node serial_control.js COM1 power_get
    Port 'COM1' opened.
      Request[0]=0x83
      Request[1]=0x00
      Request[2]=0x00
      Request[3]=0xFF
      Request[4]=0xFF
      Request[5]=0x81
    Command 'power_get' written.
      Response[0]=0x70: ANSWER
      Response[1]=0x00: Completed (Normal)
      Response[2]=0x02: Return Data Size
      Response[3]=0x01: Return Data[0]
      Response[4]=0x73: Checksum matched
    

Sample Code

  • Excerpt from serial_control.js
    const serialport = require("serialport");
    
    const commands = {
      "power_on":                        [0x8C, 0x00, 0x00, 0x02, 0x01],
      "power_off":                       [0x8C, 0x00, 0x00, 0x02, 0x00],
      "power_get":                       [0x83, 0x00, 0x00, 0xFF, 0xFF],
    ...
    (*snip*)
    ...
    
    const sp = new serialport(portname, {
      baudRate: 9600,
      dataBits: 8,
      parity: 'none',
      stopBits: 1,
      flowControl: false,
      parser: new serialport.parsers.Readline("\n")
    });
    
    sp.on("data", function(data) {
      for (var i = 0; i < data.length; i++) {
        if (resp_index == 0) { // HEADER
          if (data[i] == 0x70) {
            resp_log(resp_index, data[i], 'ANSWER');
          } else {
            sp.close();
            errorlevel = EL_HEADER_UNKNOWN;
            return resp_log(resp_index, data[i], 'Error: Header is not ANSWER(0x70), ' + toHex(data[i]));
          }
        } else if (resp_index == 1) { // ANSWER
          resp_answer = data[i];
          var answer = answers[data[i]];
          if (answer !== undefined) {
            resp_log(resp_index, data[i], answer);
            if (resp_answer !== 0x00) {
              errorlevel = EL_ANSWER_ERROR;
            }
          } else {
            sp.close();
            errorlevel = EL_ANSWER_UNKNOWN;
            return resp_log(resp_index, data[i], 'Error: Unknown answer, ' + toHex(data[i]));
          }
        } else {
          if (cmd_type == 0x8C) { // Response to Control Request
            if (resp_index == 2) {
              return checkChecksum(sp, data[i], resp_checksum, resp_index);
            }
          } else if (cmd_type == 0x83) {  // Response to Query Request
            if (resp_index == 2) {
              if (resp_answer == 0x00) {
                resp_return_data_size = data[i];
                resp_return_data_size -= 1;
                resp_log(resp_index, data[i], 'Return Data Size');
              } else {
                return checkChecksum(sp, data[i], resp_checksum, resp_index);
              }
            } else if (resp_index >= 3 && resp_index < (2 + resp_return_data_size)) {
              resp_log(resp_index, data[i], 'Return Data[' + (resp_index - 3) + ']');
            } else if (resp_index == (2 + resp_return_data_size)) {
              return checkChecksum(sp, data[i], resp_checksum, resp_index);
            }
          } else {
            sp.close();
            errorlevel = EL_CMD_TYPE_UNKNOWN;
            return resp_log(resp_index, data[i], 'Error: Unknown command type');
          }
        }
        resp_index += 1;
        resp_checksum += data[i];
      }
    });
    
    sp.on("open", function(err) {
      if (err) {
        errorlevel = EL_OPEN_ERROR;
        sp.close();
        return console.log("Error on open: " + err.message);
      } else {
        console.log("Port '" + portname + "' opened.");
        var buf = commands[cmdname];
        buf.push(0); // for checksum
        var sum = 0;
        for (var i = 0; i < buf.length; i++) {
          if (i == buf.length - 1) {
            buf[i] = sum;
          } else {
            sum += buf[i];
          }
          console.log("   Request[" + i + "]=" + toHex(buf[i]));
        }
        resp_index = 0
        cmd_type = buf[0];
        sp.write(buf, function(err) {
          if (err) {
            errorlevel = EL_WRITE_ERROR;
            sp.close();
            return console.log('Error on write: ', err.message);
          }
          console.log("Command '" + cmdname + "' written.");
        });
      }
    });
    
Last modified: 1 Mar 2019