How to replicate pyserial sending RGB values to Arduino in jUART?

送分小仙女□ 提交于 2019-12-11 04:36:51

问题


I have been using pyserial in a locally running Python application to send RGB values to an Arduino with:

import serial
import struct

ser = serial.Serial('/dev/ttyACM0', 9600)

ser.write(struct.pack('>3B', red_value, green_value, blue_value))

# where the rgb values are ints like 77,75, 0

I'd like to now achieve this functionality from a non-local website.

jUART seems to be designed for this purpose, ie a:

Cross platform browser plugin for serial port communication from JavaScript

It requires the user to create a plugin for the system and browser they are using.

I've followed the instructions on their main GitHub page as far as I could, but I don't really understand the parameters enough to define their values, and I haven't made a browser plugin before so not sure if I am missing something.

I'm using Ubuntu 16.04 and Firefox 48 for Ubuntu and have copied:

bin/Linux/npjUART.so

to:

~/.mozilla/extensions

HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Serial Test</title>
<script src="serial.js"></script>
</head>
<body>

</body>
</html>

Javascript (serial.js)

//Get a Serial object
var ser = plugin().Serial;

// Open a port
ser.open("/dev/ttyACM0");

// Set port options
/*
baud: Baud rate

parity: 0->none, 1->odd, 2->even

csize: 5 6 7 8

flow: 0->none, 1->software, 2->hardware

stop: 0->one, 1->onepointfive, 2->two
*/

var baud = "";
var parity = "";
var csize = "";
var flow = "";
var stop = "";

ser.set_option(baud, parity, csize, flow, stop);

// Send a byte to serial port
char = "";

ser.send(char);

Questions

01) What should the empty values in the above Javascript be?

02) Do I additionally need to follow the To Build instructions?

03) Is there anything else I need to do to get jUART to replicate the original pyserial program shown above?

Edit:

Based on user suggestion to look at pyserial defaults and then replicate them, they seem to be here:

https://pythonhosted.org/pyserial/pyserial_api.html#serial.Serial

__init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None)

So perhaps jUART variables would therefore be:

var baud = 9600;
var parity = 0;
var csize = 8; // based on user comment
var flow = 0; // based on user comment
var stop = 0;

When I open the html file with the above values in the js file, I get the following in Firebug:

ReferenceError: plugin is not defined

Arduino Sketch: (for reference)

// digital output pin numbers
const int digitalOutputPinForRedLED = 9;
const int digitalOutputPinForGreenLED = 10;
const int digitalOutputPinForBlueLED = 11;

// global variables
int valueOfRed = 0;
int valueOfGreen = 0;
int valueOfBlue = 0;
int x = 1;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

  // set digital pin modes
  pinMode(digitalOutputPinForRedLED,OUTPUT);
  pinMode(digitalOutputPinForGreenLED,OUTPUT);
  pinMode(digitalOutputPinForBlueLED,OUTPUT);
}

void loop() {

  if (Serial.available()) {
    if (x==1) {
     valueOfRed = Serial.read();
     Serial.print("Red: ");
     Serial.println(valueOfRed);
     analogWrite(digitalOutputPinForRedLED, valueOfRed);
   }
    else if (x==2) {
     valueOfGreen = Serial.read();
     Serial.print("Green: ");
     Serial.println(valueOfGreen);
     analogWrite(digitalOutputPinForGreenLED, valueOfGreen);
   }
    else if (x==3) {
     valueOfBlue = Serial.read();
     Serial.print("Blue: ");
     Serial.println(valueOfBlue);
     analogWrite(digitalOutputPinForBlueLED, valueOfBlue);
   }
   x++;
 }
    else {
      x = 1; 
     }
delay(1);
}

回答1:


Looking at the documentation, the only thing you missed was adding the plugin to the PLUGINS directory. Copy the already compiled plugin from the git to ~/.mozilla/plugins/ instead of ~/.mozilla/extensions



来源:https://stackoverflow.com/questions/38841959/how-to-replicate-pyserial-sending-rgb-values-to-arduino-in-juart

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