Skip to content

Instantly share code, notes, and snippets.

@mipsparc
Last active May 10, 2020 02:58
Show Gist options
  • Save mipsparc/290b64b68e94d309964b2c0479762843 to your computer and use it in GitHub Desktop.
Save mipsparc/290b64b68e94d309964b2c0479762843 to your computer and use it in GitHub Desktop.
DSair2 鉄道模型コントローラでアナログ鉄道模型をPythonから制御する、あくまでサンプルコード
#coding:utf-8
import serial
import time
def send(s, value):
s.write(value.encode('ascii') + b'\n')
s.flush()
def init_dsair2(s):
time.sleep(0.3)
send(s, 'setPower(0)')
time.sleep(0.3)
send(s, 'setPower(0)')
time.sleep(0.3)
s.reset_input_buffer()
ser = serial.Serial('/dev/ttyUSB0', baudrate=115200, timeout=0.1, write_timeout=0.1, inter_byte_timeout=0.1)
# DSair2を再起動
send(ser, 'reset()')
time.sleep(1)
# 1回目の初期化
init_dsair2(ser)
send(ser, 'setPing()')
if (not ser.read(50).decode('ascii').endswith('200 Ok\r\n')):
print('DSair2を正常に認識できませんでした。終了します')
exit()
else:
print('DSair2を正常に認識しました。')
# 2回目の初期化
init_dsair2(ser)
print('DSair2 起動完了')
for speed in [0, 400, 350, 350, 350, 350, 350, 350, 280, 280, 280, 250, 0]:
way = 1
command = f'DC({speed},{way})\n'
send(ser, command)
time.sleep(0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment