2014-04-10 91 views
0

我正在创建一个获取串行输入的程序,将其转换为int,然后将引脚设置为HIGH,但输入时间长,例如,如果我向Arduino打印“9000”串口将引脚13设置为高电平9000毫秒。但是,它在连接开始时将引脚设置为高电平以进行初始“握手”。有什么方法可以消除这种情况,或者反击它?消除Arduino串行噪声

这里是我当前的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"); 
      } 
     } 
    } 
} 
+0

你应该能够公正'digitalWrite(继电器,LOW);'你的设置()。在将它设置为输出之前,甚至可能会这样做。通过主机操作系统配置(空闲时控制线的状态),通过禁用串行命令复位,更改引导加载程序立即将信号驱动为低电平,或者甚至通过禁用复位线保险丝(尽管这会使你的程序变得很难并且很难解开)。如果信号的错误设置具有负面现实世界的后果,您可能需要更安全的设计。 –

回答

0

您还没有检查的有效性(时间= Serial.parseInt())的输入,所以生产线上的任何噪音都将作为有效的输入值。我用命令协议将我的呼叫包裹到Arduino,以便我可以确定Arduino获得了发送的值而不是噪音。即首先为GetReadyToReceive发送16,然后是值。这样,输入错误的可能性就会降低。

见我在http://playground.arduino.cc/Csharp/SerialCommsCSharp代码为例

+0

嗨,我刚刚尝试过,它仍然在开始时做同样的事情,我会尽快上传它的视频。这里是我的代码,有什么不对吗? (OP中的新代码) – cheese5505

+0

代码对我来说还行 – Richard210363

+0

另外,我可能在程序的另一端做错了,因为我没有使用C#,但是我正在使用Java。如果有人知道我会如何在Java中做到这一点,它将非常感激。我将把Java代码放入OP中。 – cheese5505