如何在 jUART 中将 pyserial 发送的 RGB 值复制到 Arduino? [英] How to replicate pyserial sending RGB values to Arduino in jUART?

查看:19
本文介绍了如何在 jUART 中将 pyserial 发送的 RGB 值复制到 Arduino?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在本地运行的 Python 应用程序中使用 pyserial 将 RGB 值发送到 Arduino,其中:

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 似乎就是为此目的而设计的,即:

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

用于串口通信的跨平台浏览器插件JavaScript

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.

我已尽我所能按照他们的主 GitHub 页面 上的说明进行操作,但我没有并没有真正了解参数来定义它们的值,而且我之前没有制作过浏览器插件,所以不确定我是否遗漏了什么.

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.

我正在使用 Ubuntu 16.04Firefox 48 for Ubuntu 并已复制:

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

bin/Linux/npjUART.so

到:

~/.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)

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);

问题

01) 上面 Javascript 中的空值应该是什么?

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

02) 我是否还需要遵循 To Build 说明?

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

03) 我还需要做些什么才能让 jUART 复制上面显示的原始 pyserial 程序?

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

根据用户建议查看 pyserial 默认值然后复制它们,它们似乎在这里:

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)

因此 jUART 变量可能是:

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;

当我使用 js 文件中的上述值打开 html 文件时,我在 Firebug 中得到以下内容:

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

ReferenceError: plugin is not defined

Arduino Sketch:(供参考)

// 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);
}

推荐答案

查看文档,您唯一遗漏的是将插件添加到 PLUGINS 目录.将已经编译好的插件从 git 复制到 ~/.mozilla/plugins/而不是 ~/.mozilla/extensions

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

这篇关于如何在 jUART 中将 pyserial 发送的 RGB 值复制到 Arduino?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆