Serial port connection between host and guest with Virtualbox

I am trying to learn how to write C code that will be read from a serial port on Linux. I found what seems like a good tutorial here .

I want to check this code, so I think I need either a serial port or a way to write to a serial port while the code is reading from above.

I am running Ubuntu 10.04 as a virtual machine on my Mac using a virtual box. My idea was to set up a virtual serial connection and write from host to guest. Hopefully something is as simple as cat "Hello World" > /tmp/fake_serial in the host terminal, and in order to read the program from the link above.

Is it possible? I tried to add a serial port using a virtual window, and when I try to execute the above command, I get an error message that I cannot write to the socket.

The second option that I was thinking about was using something like minicom in the guest OS to connect to say /dev/ttyS1 and write messages for my code to be read at the same time. Again, assuming the baud rate and other settings are OK, is this possible?

I do not have much experience with serial ports, so I will be grateful for any suggestions on how to do this. Thank you in advance.

+4
source share
3 answers

So, to get this working, I added another Ubuntu virtual machine to VirtualBox and connected it together via the virtual serial port. My main, original VM, which I use for a big development, will be called VM1. A new virtual machine with a small hard drive that will only be used to send messages to VM1 will be called VM2. This is the Ubuntu 10.04 VM.

In VirtualBox, go to "Settings for VM1", go to the ports and change the settings as follows: VM1 Settings

Now go to VM2 and select the settings, ports, then change as follows:

VM2 Settings

Now first you need to start VM1. When this boots up, boot up VM2. Now you can open the terminal in VM1 and type screen /dev/ttyS0 38400 (you may need to run sudo apt-get install screen before this works). Then go to VM2, open a terminal and enter echo "Hello" > /dev/ttyS0 .

You should see that Hello appears in the terminal open in VM1. When you finish running the screen, press ctrl-a k to kill it, otherwise, if you try to do other things using the serial port, you may get an error message saying that the port is busy.

+5
source

When I had to go through the serial port test from my real virtual machine, I finished the loop back test. I took two USB-serial converters and an RS232 FF adapter and connected my car to myself. Then in the VirtualBox, under "Settings-> USB", you can route one of two USB-serial converters that will "belong" to your VirtualBox.

As soon as you connect the converters, you register on a Mac and one with a Ubuntu computer, then you can perform serial communication as usual between the two machines.

You can also emulate a virtual serial port using pty (pseudo-teletype device), but I am not sure about this, since I believe that the ability to do this was blocked in the new kernels.

+1
source

I came across a similar situation with guest QNX using VirtualBox 5.0.10 on an Ubuntu 14.04 host.

My solution seems common enough to apply to the above case.

I configured the guest virtual machine in the same way that Kells1986 installed its VM1:

On the Serial Ports / Port1 tab:

  • check the box next to "Enable serial port"
  • set "Port Number" to "COM1"
  • set "IRQ" to "4"
  • set "I / O Port" to "0x3F8"
  • set "Port Mode" to "Host Pipe".
  • uncheck "Connect to an existing handset / outlet"
  • set "Path / Address" to an accessible path to the file system (for example, "/ home / safayet / vmSerialPipe")

According to the VirtualBox manual :

You can tell VirtualBox to connect the virtual serial port to the software handset on the host .... On a Mac, Linux, or Solaris computer, a local domain socket is used ... On Linux, there are various tools that can connect to a local domain juice or create it in server mode . The most flexible tool is socat and is available as part of many distributions.

A domain socket is an IPC mechanism on UNIX systems similar to a channel.

I connected to the end of the "pipe" of the virtual serial port on the Ubuntu host using the socat command:

 socat - UNIX-CONNECT:/home/safayet/vmSerialPipe 
+1
source

All Articles