TOPLLab / WARDuino

📟 A dynamic WebAssembly VM for embedded systems
https://topllab.github.io/WARDuino/
Mozilla Public License 2.0
80 stars 7 forks source link

reading and writing to serial from local VM does not work #125

Closed carllocos closed 1 year ago

carllocos commented 1 year ago

When event-based out-of-place debugging, the local VM connects to the MCU via serial so as to pull primitives and receive generated events. However, the local VM does not receive any generated event nor can it perform remote function calls.

I connected to the MCU via arduino-cli monitor and manually created events as well as rfc interrupts. And in both cases, I receive the expected output:

arduino-cli monitor -p /dev/cu.usbserial-8952FFEE8B  -c baudrate=115200
Monitor port settings:
baudrate=115200
Connected to /dev/cu.usbserial-8952FFEE8B! Press CTRL-C to exit.
{"topic":"interrupt_37","payload":""}{"topic":"interrupt_37","payload":""}

64030000000A00000000000000
received interrupt 64
Interrupt: 64
{"success":true}

so I believe there is something wrong with read and/or write to the file descriptor associated with the serial connection when performed from within the local VM.

I doubled checked the number of bytes written when calling write. It corresponds with the expected number. https://github.com/TOPLLab/WARDuino/blob/3981b3d0e7abb0aa55bfb9e27ba2effdc732a186/src/Edward/proxy_supervisor.cpp#L129

carllocos commented 1 year ago

A potential cause is that when you write to the file descriptor. The content buffers, thus it does not get flushed by default. Since there is no standard function that flushes based on a file descriptor, I rewrote thus the FileDescriptorChannel::write method to use the write standard function, which by default does not buffer:

int FileDescriptorChannel::write(const char *fmt, ...) const {
    char buffer[256]{};
    va_list args;
    va_start(args, fmt);
    int size = vsnprintf(buffer, 256, fmt, args);
    int written = 0;
    if( size > 0 && size < 256){
        written = ::write(this->fd, buffer, size);
    }
    va_end(args);
    return written;
}

Unfortunately, this also does not solve the problem

carllocos commented 1 year ago

The problem comes from not correctly configuring the serial connection (e.g., the baudrate needs to be specified). Commit 76439b76a8ae20bae7a0e8bf7d1b7b8683c6d52c should fix the issue.

Now it is also possible to specify which baudrate to use when connecting the local VM to a MCU via serial. This is actually mandatory when using a serial connection. To provide this, start the local VM with an extra argument of the form --baudrate 115200