【问题标题】:Eliminate line noise on Arduino Serial消除 Arduino 串行上的线路噪声
【发布时间】:2014-04-10 06:16:12
【问题描述】:

我正在创建一个程序,它获取串行输入,将其转换为 int,然后将引脚设置为 HIGH,无论输入显示多长时间,例如,如果我将“9000”打印到 Arduino 串行端口,它将设置引脚13 到 HIGH 持续 9000 毫秒。但是,它在连接开始时将引脚设置为 HIGH 以进行初始“握手”。有什么办法可以消除或对抗它?

这是我当前的 Arduino 代码:

int Relay = 13;
//The pin that the relay is attached to
int time;
//Creates temp variable

void setup() {
    //Initialize the Relay pin as an output:
    pinMode(Relay, OUTPUT);
    //Initialize the serial communication:
    Serial.begin(9600);
}

void loop() {
    while(true) {
        //Check if data has been sent from the computer:
        if (Serial.available()) {
                    //Assign serial value to temp
            time = Serial.parseInt();
            //Output value to relay

                    delay(4000);
                digitalWrite(Relay, HIGH);
                delay(time);
            digitalWrite(Relay, LOW);
        }
    }
}

如果有什么办法可以告诉我怎么做吗?

谢谢:)

编辑 1:

int Relay = 13;
//The pin that the relay is attached to
int time;
byte byte_1;
byte byte_2;
//Creates temp variable

void setup() {
  pinMode(Relay, OUTPUT);
  //Initialize the serial communication:
  Serial.begin(9600);
  digitalWrite(Relay, LOW);
  delay(250);
}

void loop() {
  //Check if data has been sent from the computer:
  if (Serial.available() == 2) {
      byte_1 = Serial.read();
      delay(100);
      byte_2 = Serial.read();
  }                   

  if(byte_1 == 16) {
      digitalWrite(Relay, HIGH);
      delay(byte_2);
      digitalWrite(Relay, LOW); 
  }

  byte_1 = 0;
  byte_2 = 0;
}

编辑 3:

package net.arduino;

import java.io.OutputStream;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

public class Main {
    public static void main(String[] args) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM3");

        if(portIdentifier.isCurrentlyOwned()) {
            System.out.println("ERROR!!! -- Port already in use!");
        }else{
            CommPort commPort = portIdentifier.open("XBell", 0);

            if(commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                OutputStream out = serialPort.getOutputStream();
                byte[] buffer = new byte[2];
                buffer[0] = (byte) 16;
                buffer[1] = (byte) 1000;

                out.write(buffer);
                out.flush();
                Thread.sleep(10000);
                out.close();
            }else{
                System.out.println("ERROR!!! -- This is not normal");
            }
        }
    }
}

【问题讨论】:

  • 您应该可以在 setup() 中使用 digitalWrite(Relay, LOW);。它甚至可以在将其设置为输出之前 这样做。您可以通过主机操作系统配置(空闲时控制线的状态)、通过禁用串行命令复位、更改引导加载程序以立即将信号驱动为低电平,或者甚至通过禁用复位线来解决复位问题。保险丝(尽管这会使更改程序变得困难并且难以撤消)。如果错误的信号设置会对现实世界产生负面影响,您可能需要更安全的设计。

标签: java serial-port arduino


【解决方案1】:

您也没有检查输入的有效性(time = Serial.parseInt()),因此线路上的任何噪音都将作为有效的输入值。我用命令协议包装了对 Arduino 的调用,这样我就可以确定 Arduino 得到了发送的值而不是噪音。即首先为 GetReadyToReceive 发送 16,然后发送值。这样就减少了错误输入的可能性。

http://playground.arduino.cc/Csharp/SerialCommsCSharp 上查看我的代码示例

【讨论】:

  • 嗨,我刚试过,一开始还是一样,我会尽快上传它的视频。这是我的代码,有什么问题吗? (OP中的新代码)
  • 另外,我可能在程序的另一端做错了,因为我没有使用 C#,但我使用的是 Java。如果有人知道我将如何在 Java 中做到这一点,将不胜感激。我会将Java代码放在OP中。
猜你喜欢
  • 2022-06-17
  • 2015-02-24
  • 2016-06-13
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多