Skip to content

Instantly share code, notes, and snippets.

@sh4869
Last active August 13, 2016 08:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh4869/10296864 to your computer and use it in GitHub Desktop.
Save sh4869/10296864 to your computer and use it in GitHub Desktop.
Rubyでserialportを弄る簡単なスクリプト
char ch = 'R'; //送受信の時に使う文字列。
int i = 1;
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
while(Serial.available()){
ch = Serial.read();
if(ch == 'y'){
digitalWrite(13,HIGH);
}
if(ch == 'n'){
digitalWrite(13,LOW);
}
}
}
require 'rubygems'
require 'serialport'
@serial_port = "/dev/ttyACM0" #シリアルのポートを指定。OSによって違うので確認のこと。
@serial_bps = 9600
sp = SerialPort.new(@serial_port,@serial_bps)
count = 0
loop do
sp.write('y') #serialへの書き込み
sleep(2)
sp.write('n')
sleep(2)
if count == 20
break
end
count += 1
puts "#{count}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment