Clear Pole Display text using chrome serial

痞子三分冷 提交于 2019-12-14 03:11:53

问题


I have created a chrome application to connect to a Pole Display through serial port, I have succeeded with writing to it, but every time I send a message, it concatenates it with the previous one, I can't find a way to clear the screen each time I send a new message !

Here's my code:

var connectionId = -1;

openPort("COM3");

function openPort(port){
  var onOpen = function(connectionInfo) {
    if (!connectionInfo || connectionInfo.connectionId == -1) {
      return;
    }
    connectionId = connectionInfo.connectionId;
  }
  chrome.serial.connect(port, {bitrate: 9600}, onOpen);
}

function closePort() {
  if (connectionId == -1) {
    return;
  }

  var onDisconnect = function(connectionInfo) {
    connectionId = -1;
  }
  chrome.serial.disconnect(connectionId, onDisconnect);
}

function sendData(str){
  chrome.serial.flush(connectionId, function(){});
  chrome.serial.send(connectionId, str2ab(str), function(){});
}

function str2ab(str) {
  var buf      = new ArrayBuffer(str.length);
  var bufView  = new Uint8Array(buf);
  for (var i   = 0; i < str.length; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}

sendData("dsadsads");

回答1:


You should be looking into your Pole Display manual.

From my experience working with one, you need to send a special command to clear the display and / or reposition the caret at the beginning.


You named your display model, and a search for "BIRCH DSP-800 manual" gives first link to this document that contains all the command codes in section 4.1.7.

To overwrite a string, you just need to send exactly 40 characters that will "roll over" the old data. This is a universal solution by the way: only write text in chunks equal to display size. Pad with spaces as needed.

You can also execute commands by sending a string of characters corresponding to a command. Let's do it with the example of "clear range" command.

"Clear range from 1 position to 40 position and move cursor to 1 position"

Hex string is 04 01 43 n m 17, take note: m and n are in range [0x31;0x58], so position 1 would be n = 0x31 and position 40 would be (hex arithmetic!) m = 0x31 + 39 = 49 + 39 = 88 = 0x58 (no surprise). Therefore, the correct command (hex-encoded) is 04 01 43 31 58 17.

The corresponding string would be String.fromCharCode(0x04, 0x01, 0x43, 0x31, 0x58, 0x17), you send it as text and you're done.

You can convert other commands like that and control your pole display fully.



来源:https://stackoverflow.com/questions/23033585/clear-pole-display-text-using-chrome-serial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!