2014-04-10 92 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