串口不工作? [英] Serial port not working?

查看:18
本文介绍了串口不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个程序,将数据发送到我的 arduino,它检测发送的内容,然后根据按下的键打开正确的引脚.

I made a program that sends out data to my arduino which detects what was sent and then turns on the correct pin according to what key is pressed.

在我的 windows 计算机上使用 arduino 软件时,arduino 草图工作正常,我可以通过发送 W A S 或 D 来打开和关闭每个引脚.

When using the arduino software from my windows computer the arduino sketch works fine, I can make each pin turn on and off by sending either W A S Or D.

通过节点发送时,arduino 上的 RX 灯闪烁,但没有其他反应.

When sending via node the RX light on the arduino flashes but nothing else happens.

有人可以帮忙吗?

Node.js 程序:

Node.js program:

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 程序:

Arduino Program:

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 发送的原因.

I recently dabbled into this. The Arduino automatically resets when it receives serial communication from most things other than the Arduino IDE. This is why you can send from the IDE but not node.js.

我有一个 Uno 并在复位和接地之间放置了一个电容器.这是一个页面,其中包含有关该主题的一些很好的信息.
祝你好运.http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

I have an Uno and put a capacitor between Reset and Ground.Here's a page with some good info on the subject.
Good luck. http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

这篇关于串口不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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