1

从 sp.getc 更改为 sp.gets 后,我的以下脚本工作正常。

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:in block in <main>': undefined methodstart_with?'for nil:NilClass (NoMethodError) from serial_arduino.rb:14:in loop' from serial_arduino.rb:14:in' 如果我删除 start_with? 方法,然后一切都很好。使用 irb 测试显示方法是可以的

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

1 回答 1

0

似乎与 Arduino 或我的虚拟化 Debian 机器有关。重新启动 VM 并关闭 Arduino 电源后,我已经完成了。

于 2013-07-07T06:15:23.263 回答