串行端口不工作
Serial port not working?
我制作了一个程序,将数据发送到我的arduino,该Arduino检测发送的内容,然后根据按下的键打开正确的引脚。
当我从我的Windows计算机使用arduino软件时,arduino草图工作正常,我可以通过发送W A S或D来打开和关闭每个引脚。
当通过节点发送时,arduino上的RX灯闪烁,但没有其他反应。
谁能帮忙?
节点.js程序:
var httpServer = require('http').createServer(function(req, response){ /* Serve your static files */ })
httpServer.listen(8080);
var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);
everyone.now.logStuff = function(msg){
console.log(msg);
}
var SerialPort = require('serialport2').SerialPort;
var assert = require('assert');
var portName;
if (process.platform == 'win32') {
portName = 'COM4';
} else if (process.platform == 'darwin') {
portName = '/dev/cu.usbserial-A800eFN5';
} else {
portName = '/dev/ttyUSB0';
}
var readData = '';
var sp = new SerialPort();
sp.on('close', function (err) {
console.log('port closed');
});
sp.on('error', function (err) {
console.error("error", err);
});
sp.on('open', function () {
console.log('port opened... Press reset on the Arduino.');
});
sp.open(portName, {
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
everyone.now.forward = function() {
sp.write("w");
}
everyone.now.back = function() {
sp.write("s");
}
everyone.now.left = function() {
sp.write("a");
}
everyone.now.right = function() {
sp.write("d");
}
sp.on('data', function(data) {
console.log(data.toString());
});
Arduino 计划:
void setup(){
Serial.begin(9600);
Serial.write("READY");
//Set all the pins we need to output pins
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop (){
if (Serial.available() > 0) {
//read serial as a character
char ser = Serial.read();
Serial.write(ser);
//NOTE because the serial is read as "char" and not "int", the read value must be compared to character numbers
//hence the quotes around the numbers in the case statement
switch (ser) {
case 'w':
move(8);
break;
case 's':
move(9);
break;
case 'a':
move(10);
break;
case 'q':
move(10);
move(8);
break;
case 'd':
move(11);
break;
case 'e':
move(11);
move(8);
break;
}
}
}
void move(int pin){
Serial.print(pin);
digitalWrite(pin, HIGH);
delay(1);
digitalWrite(pin, LOW);
}
我最近涉足了这个。 Arduino在收到来自Arduino IDE以外的大多数事物的串行通信时会自动重置。这就是为什么您可以从 IDE 发送,但不能从 node.js 发送。
我有一个Uno,并在复位和接地之间放置了一个电容器。这是一个页面,其中包含有关该主题的一些好信息。
祝你好运。 http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection
我每天都使用node通过USB或bt向我的Arduino发送操作,并且在这两种情况下都很好用。我认为你的问题来自寄信。您应该发送一个缓冲区,其中包含字母的 ascii 值,如下所示:
myPort.write(Buffer([myValueToBeSent]));
另外,为此,我认为使用一些"逻辑"接口,数据标头,操作数量之类的东西会更好。它不是必需的,但它会使您的代码更健壮,将来更容易修改。
这是我如何做到这一点的一个例子。首先,节点:
var dataHeader = 0x0f, //beginning of the data stream, very useful if you intend to send a batch of actions
myFirstAction = 0x01,
mySecondAction = 0x02,
myThirdAction = 0x03;
然后你像以前一样打电话给他们:
everyone.now.MyBatchOfActions = function() {
sp.write(Buffer([dataHeader]));
sp.write(Buffer([0x03])); // this is the number of actions for the Arduino code
sp.write(Buffer([myFirstAction]));
sp.write(Buffer([mySecondAction]));
sp.write(Buffer([myThirdAction]));
}
这样,Arduino 很容易在 Serial.read() 上读取数据:(请注意,您需要在某处定义数据标头和数据页脚)
void readCommands(){
while(Serial.available() > 0){
// Read first byte of stream.
uint8_t numberOfActions;
uint8_t recievedByte = Serial.read();
// If first byte is equal to dataHeader, lets do
if(recievedByte == DATA_HEADER){
delay(10);
// Get the number of actions to execute
numberOfActions = Serial.read();
delay(10);
// Execute each actions
for (uint8_t i = 0 ; i < numberOfActions ; i++){
// Get action type
actionType = Serial.read();
if(actionType == 0x01){
// do you first action
}
else if(actionType == 0x02{
// do your second action
}
else if(actionType == 0x03){
// do your third action
}
}
}
}
}
我希望我清楚,希望它有所帮助!干杯!
关于电容器和复位问题...在后来的型号中,其中一个串行控制线和 Arduino 上的复位之间有一个小电容器。该电容器会导致 Arduino 在端口打开时复位,但不会干扰正常的串行操作。
此重置技巧允许代码上传在上传过程中重置Arduino。当 Arduino 启动时,代码引导加载程序首先运行一小段时间,然后加载的代码运行。
上传过程为:重置启动引导加载程序的Arduino,在Arduino IDE中启动上传过程,建立通信,上传,然后运行加载的代码。当Arduino启动时,它会等待一小段时间的上传,如果没有收到,它将继续运行代码。
我发现这非常有用,因为它允许我们仅通过关闭和打开端口来有效地重置Arduino。在旧的Arduino中,如果没有这个电容器,您必须在正确的时间按下重置按钮才能上传代码。而且时机如此之长,以至于Arduino在开始使用上传的代码之前花费了更多的时间等待。
在这里描述的问题中,我不相信他会因为使用的重置技巧而遇到任何问题。当他打开串行端口时,它应该只具有重置Arduino的效果,从他的信息来看,这是一个理想的副作用。
就我而言,问题是重置,但串行端口已打开 - 但在重置完成之前不可用于写入。 在写入端口之前放置 3s 延迟解决了该问题。 编写 ASCII 不是问题。
- 保存串行端口列表与流星
- 如何从JavaScript中读取串行端口数据
- 如何使用javascript在客户端通过串行端口进行通信
- 我已经完成了私有到公共 IP 的端口转发,但在公共 IP 中它不会渲染 geoip.js但它在本地地址上可以正常工作
- 使用 javascript 和 jssc.jar 将数据发送到串行端口
- 尝试使用Beaglebone Black上的串行端口时无法打开文件“/dev/ttyO2”
- 我的网络应用程序如何与串行端口通信
- 串行端口不工作
- 谷歌浏览器 - 如何使用javascript通过客户端的串行端口进行通信
- Node.js+Node Webkit+基于Node串行端口的应用程序-这可能吗
- node.js串行端口事件在重新连接到端口后停止工作
- 附加组件生成器:使用端口的多个工作程序
- 无法使用节点预处理程序重建串行端口:404状态代码下载64位nw.lib
- Web应用程序需要使用串行端口进行通信
- 节点WebKit和串行端口
- 从node.js中的arduino serialport和arduino串行端口接收值
- 通过串行端口进行通信的Web应用程序
- 从模拟端口读取时从Arduino串行接收到的错误数据
- 串行端口没有方法“列表”
- 用JavaScript字符串发送CR LF到node.js串行端口服务器