2013-07-06 100 views
1

從sp.getc更改爲sp.gets後,我有以下腳本正常工作。Ruby 1.9.3 Serialport未定義的方法`start_with?

require "rubygems" 
require "serialport" 
require "data_mapper" 

#params for serial port 
port_str = "/dev/ttyACM0" 
baud_rate = 19200 
data_bits = 8 
stop_bits = 1 
parity = SerialPort::NONE 

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity) 

loop do 
    sp_char = sp.gets 
    if sp_char.start_with?("Time") 
    printf("%s", sp_char) 
    end 
end 

我再重新加載Arduino的有以下

//SMS Send and Receive program 
//Using Mega 2560 & Elecrow SIM900 GSM Shield 
//4 July 2013 

int count = 0; 
int sendCount=0; 
int n_sms,x,sms_start; 
char data[300]; 

void setup() 
{ 
Serial1.begin(19200);    // the GPRS/GSM baud rate 
Serial.begin(19200);     // the USB baud rate 

Serial1.println("AT\r"); 
Serial1.flush(); 
delay(1000); 
Serial1.println("AT+CMGF=1\r"); 
Serial1.flush(); 
delay(1000); 
Serial.println("Text Mode Selected"); 
Serial.flush(); 
//sendSMS(); 
} 

void loop() 
{ 

    if(sendCount>720) //approx 60 minutes between sends 
    { 
    //sendSMS(); 
    sendCount=0; 
    } 
    else 

    Serial1.print("AT+CMGR=1\r"); //Reads the first SMS 
     Serial1.flush(); 
     for (x=0;x < 255;x++){ 
      data[x]='\0'; 
     } 
     x=0; 
     do{ 
      while(Serial1.available()==0); 
      data[x]=Serial1.read(); 
      x++; 
      if(data[x-1]==0x0D&&data[x-2]=='"'){ 
       x=0; 
      } 
      }while(!(data[x-1]=='K'&&data[x-2]=='O')); 

     data[x-3]='\0';  //finish the string before the OK 

     Serial.println(data); //shows the message 
     Serial.flush(); 
     Serial1.print("AT+CMGD=1,4\r"); 
     Serial1.flush(); 

     delay(5000); 
     sendCount++; 

} 

void sendSMS() 
{ 
Serial1.println("AT+CMGS=\"+64xxxxxxxxxx\"\r");  
//Replace this number with the target mobile number. 
Serial1.flush(); 
delay(1000); 
Serial1.println("SA\r"); //The text for the message 
Serial1.flush(); 
delay(1000); 
Serial1.write(0x1A); //Equivalent to sending Ctrl+Z 
} 

現在我得到以下錯誤(我以爲是什麼我呢?): serial_arduino.rb:16:block in <main>': undefined method start_with ?'for nil:NilClass(NoMethodError) from serial_arduino.rb:14:in loop' from serial_arduino.rb:14:in' 如果我刪除了start_with?方法,那麼一切都很好。用irb顯示方法測試OK

irb(main):003:0> 'AT'.start_with? 'Time' 
=> false 
irb(main):004:0> 'Time:0902'.start_with? 'Time' 
=> true 

回答

0

似乎與Arduino或我的虛擬化的Debian機器有關。在重新啓動虛擬機並關閉Arduino電源後,我已經完成了它。