var _blue = mywindow.findChild("_blue"); // QSlider var _close = mywindow.findChild("_close"); // QPushButton var _color = mywindow.findChild("_color"); // QLabel var _green = mywindow.findChild("_green"); // QSlider var _input = mywindow.findChild("_input"); // XLineEdit var _output = mywindow.findChild("_output"); // XTextEdit var _red = mywindow.findChild("_red"); // QSlider var _send = mywindow.findChild("_send"); // QPushButton // set our command constants var ON = 0x42; var OFF = 0x46; var SETCURSOR = 0x47; var HOME = 0x48; var CURSOR_ON = 0x53; var CURSOR_OFF = 0x54; var LEFT = 0x4C; var RIGHT = 0x4D; var BACKSPACE = 0x08; var NEWLINE = 0x0A; var CLEAR = 0x58; var SETCONTRAST = 0x50; var SETBACKLIGHTBRIGHTNESS = 0x99; var SETRGBCOLOR = 0xD0; var LOADCUSTOMCHARACTER = 0xC0; var COMMAND = 0xFE; var seriallcd = {}; var sp; //define the initialization function seriallcd.init = function(parent, serialport, baudrate) { sp = new QSerialPort(parent); sp.setPortName(serialport); sp.setBaudRate(baudrate); if (!sp.open(QIODevice.ReadWrite)) { QMessageBox.critical(mywindow, "Error", "Could not open serial port, error code returned: "+ sp.error); return; } seriallcd.port = sp; seriallcd.clear(); } // our two command functions seriallcd.sendCommand = function(input) { var t = QByteArray(); t.append(COMMAND); sp.write(t); t.clear(); t.append(input); sp.write(t); sp.flush(); } seriallcd.sendCommandWithParam = function(input, param) { var t = QByteArray(); t.append(COMMAND); sp.write(t); t.clear(); t.append(input); sp.write(t); t.clear(); t.append(param); sp.write(t); t.clear(); sp.flush(); } seriallcd.print = function(text) { sp.write(QByteArray(text)); } seriallcd.write = function(bytes) { sp.write(bytes); } // the group of helper functions seriallcd.setLCDContrast = function(contrast) { contrast = parseInt(contrast); if (contrast == "" || contrast < 1 || contrast > 50) { QMessageBox.critical(mywindow, "Invalid Data", "Contrast level can only be between 1 and 50"); } seriallcd.sendCommandWithParam(SETCONTRAST, contrast); } seriallcd.setLCDBrightness = function(brightness) { brightness = parseInt(brightness); if (brightness == "" || brightness < 1 || brightness > 8) { QMessageBox.critical(mywindow, "Invalid Data", "Brightness level can only be between 1 and 8"); } seriallcd.sendCommandWithParam(SETBACKLIGHTBRIGHTNESS, brightness); } seriallcd.clear = function(){ seriallcd.sendCommand(CLEAR); } //send the Home Cursor command to the LCD seriallcd.home = function() { seriallcd.sendCommand(HOME); } //Turn the LCD ON seriallcd.on = function() { seriallcd.sendCommand(ON); } // Turn the LCD OFF seriallcd.off = function() { seriallcd.sendCommand(OFF); } //Turn the Underline Cursor ON seriallcd.cursorOn = function() { seriallcd.sendCommand(CURSOR_ON); } //Turn the Underline Cursor OFF seriallcd.cursorOff = function() { seriallcd.sendCommand(CURSOR_OFF); } //Move the cursor left 1 space seriallcd.left = function() { seriallcd.sendCommand(LEFT); } //Move the cursor right 1 space seriallcd.right = function() { seriallcd.sendCommand(RIGHT); } //Backspace seriallcd.backspace = function() { seriallcd.sendCommand(BACKSPACE); } //Backspace seriallcd.setRGBColor = function(threebytes) { seriallcd.sendCommandWithParam(SETRGBCOLOR, threebytes); } // bytes representing the boundaries of our screen var g_index = [0x00, 0x40, 0x14, 0x54]; // move cursor position seriallcd.setCursor = function(line, pos){ seriallcd.sendCommandWithParam(SETCURSOR, g_index[line-1] + pos - 1); } function sSend() { seriallcd.print(_input.text); _output.plainText += _input.text + "\n"; } function setColor(newcolor) { // newcolor ignored seriallcd.setRGBColor(QByteArray([_red.value, _green.value, _blue.value])); _color.setStyleSheet("background-color: rgb(" + _red.value + ", " + _green.value + ", " + _blue.value + ");"); } // update for your serial port seriallcd.init(mywindow, "/dev/ttyACM0", 115200); _send.clicked.connect(sSend); _close.clicked.connect(mywindow, "close()"); _red["valueChanged(int)"].connect(setColor); _green["valueChanged(int)"].connect(setColor); _blue["valueChanged(int)"].connect(setColor);