WindowsでVagrant + VirtualboxのVMとArduinoをつなぐ時のメモ

多分超ニッチなので需要ないと思うけど、自分用メモ。

Arduinoに確認用のプログラムを書き込む

通信速度9600で1秒間に一度.を出力し続けるプログラムを書いておきます。この作業はホスト側でやります。VMにシリアルポートを取られちゃうので、これはVMを立ち上げる前にやらないといけません。

void setup() { 
        Serial.begin(9600);
}

void loop() {
        Serial.print('.');
        delay(1000);
}

VagrantfileにVirtualboxのオプションを書く

(ここはVirtualbox特有の設定です。他のproviderの場合はまた別のオプションになるはずです)

このへんを読んでいると、modifyvm--uartNオプションをつけるとVM側にシリアルポートができて、--uartmodeNオプションをつけるとそれとホストOSのシリアルポートをつなげられるみたいです。

--uart1

In either case, you can configure up to two virtual serial ports per virtual machine. For each such device, you will need to determine
1. what kind of serial port the virtual machine should see by selecting an I/O base address and interrupt (IRQ). For these, we recommend to use the traditional values[18], which are: a. COM1: I/O base 0x3F8, IRQ 4
b. COM2: I/O base 0x2F8, IRQ 3
c. COM3: I/O base 0x3E8, IRQ 4
d. COM4: I/O base 0x2E8, IRQ 3
(http://www.virtualbox.org/manual/ch03.html#serialports)

って書いてあります。これはVM側の方の話で、COM1というのは/dev/ttyS0マッピングされるもののようです。なので、引数は0x3F84になります。

--uartmode1

--uartmode<1-N> : This setting controls how VirtualBox connects a given virtual serial port (previously configured with the --uartX setting, see above) to the host on which the virtual machine is running. As described in detail in Section 3.9, “Serial ports”, for each such port, you can specify as one of the following options:
..snip...
client <pipename>: This operates just like server ..., except that the pipe (or local domain socket) is not created by VirtualBox, but assumed to exist already. (https://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvm-other)

とあって、すでにあるシリアルポートをマッピングしたいわけなのでclientにします。pipenameはCOM3とかCOM4になります。ここが違うとvagrant upしようとしても無言で落ちます。(Virtualboxで立ち上げようとすると「そんなポートないっす!」みたいなエラーが出て気付いた)

上のふたつは、Vagrantfileに実際書くときはこんな感じになります。(シリアルポートがCOM4の場合。Macだと/dev/...になるはず)

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--uart1", "0x3f8", "4"]
    vb.customize ["modifyvm", :id, "--uartmode1", "client", "COM4"]
  end

VMを立ち上げる

9600でシリアルポートをscreenします。

screen /dev/ttyS0 9600

で、.が返ってきたらOKなわけですが、、今のところうまく動きません。

...................................................................

とりあえずメモ