从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:在block in <main>': undefined method
start_with?'中为nil:NilClass(NoMethodError)
来自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
答案 0 :(得分:0)
似乎与Arduino或我的虚拟Debian机器有关。重新启动VM并关闭Arduino电源后我就开始了。