Hyper-V: Connecting VMs through named pipe loses data - serial-port

We are trying to connect two Hyper-V VMs through a serial port. Hyper-V exposes the serial port as a named pipe to the host system, and implements the server end of the named pipe. Consequentially, to connect them, we need to write a named-pipe client which connects to both VMs, and copies the data back and forth.
We have written such an application. Unfortunately, this application loses data.
If we connect two hyperterms, and have them exchange data, the transmission sometimes succeeds, but in many cases, the receiving end reports errors, or the transmission just deadlocks. Likewise, if we use the link to run a kernel debugger, it also seems to hang often.
What could be the cause of the data loss? What precautions must be taken when connecting named pipes in such a manner?
Edit: We have worked around the problem, using kdsrv.exe. The COM port of the debuggee continues to be exposed through a named pipe, however, the debugger end talks to kdserv via TCP.

The data loss is not due to the named pipes. It is infact the COM ports (emulated and physical) that may lose data since they operate with a small buffer in the UART.
The named pipe receives all the data that is written to the COM port. Your program reads data from the named pipe and writes it to another named pipe. This is where data loss can originate if you write too fast the receiveing COM port's UART can overflow leading to data loss.
You may need to add some delay to avoid exceeding the baud rate expected by the receiving side.
In addition, you are missing ResetEvent() calls in your program.
For your KD issues, you may need to add resets=0 to the connection string.

I did not try to connect VM via Serial but I connected VM and Host via usb (through network)
and it works.
If it is required for your software to establish serial connection try to test via serial emulators with work through tcp\ip.

I think John's suggestion is correct - if u are using a slow CPU to emulate TWO VM, then the guest OS's drivers for serial port is highly drifted away from the high speed version. So John's suggestion is to set the input/output side of the serial link to the slowest possible speed. Ie, you cannot use high baud rate for the inter-VM serial communication. Instead u have to use the slowest possible speed, and so that the VM guest driver will take that cue and use the slower version of the driver. But your physical machine must have sufficient CPU speed to run two VM concurrently, to avoid the "emulation drift" of the the serial driver.
Well, just my guess, but there is a VirtualBox version of your problem, seemingly no issues running it:
http://bodocsi.net/2011/02/how-setup-serial-port-link-in-virtualbox-between-two-guest-virtual-machine-in-linux/
But the following bug ticket for VirtualBox does describe many similarities to your problem:
https://www.virtualbox.org/ticket/1548
And reading the end seemingly indicate the solution has to do with VirtualBox's internal source code. Perhaps it is Hyper-V's problem?

Related

How can I manually change DSR/CTS signals?

I am using WinAPI to tinker with RS232 ports. Using SetCommState function I've set fRtsControl: RTS_CONTROL_HANDSHAKE and fDtrControl: DTR_CONTROL_HANDSHAKE, so from my understanding both DTR/RTS handshakes are now enabled, and now additionaly I need to set fOutxDsrFlow and fOutxCtsFlow to true, to make this flow work, right?
Having it all set up how can I manually change DSR/CTS to high/low, so I can see what happens?
Or perhaps are RTS/DTR handshakes and DSR/CTS signals completely separate things and I am understanding it incorrectly?
The DSR/CTS signal has only a passive function of acquiring the state or notifying the change as an event.
If you want to actively control it, you need to prepare another hardware or software for it and use another interface independent of the target serial port.
It's relatively easy to have two virtual COM port device drivers with loopback or port-to-port connectivity.
For example, in the case of software, there is something like this.
Connecting to serial port (com port) over network
The same thing is to prepare two USB serial port dongles in terms of hardware and connect them with a cable that crosses DTR/DSR and RTS/CTS.
Both need to be programmed to test the target port/software and run on the opposing port.

Serial communication crashes in LabVIEW

I am controlling a device over serial connection using LabVIEW (version 7.0). It is connected using USB, and is installed as a virtual serial port on the computer (running Windows XP). Every now and then my device crashes when my program sends a command, and it is unable to accept any more input (the device itself also stops working) until it has timed out.
I've looked at the serial port traffic using Portmon. Whenever the device crashes the serial driver sends the command I send using my program four times instead of just once, with an IOCTL_SERIAL_GET_COMMSTATUS command in between. I cannot see what this last command returns, but I assume something happens in the communication earlier on. I'm thinking my configuration of the port is not entirely right, but I have no idea how or why. I open and close the connection to my device every time I want to write something to it.
For completeness' sake: it has a baud rate of 9600, 8 bits, no parity, 1 stop bit, and no flow control. I'm aware that the correct settings of these parameters depend on the device, but the manufacturer has not supplied any recommended settings.
The driver is a DLL of some sort? If so, this is the most likely source of your problem, and you likely will need to contact the author of the driver. LabVIEW does have crashing bugs, but by far the most common source of crashes in simple communications apps is a buggy third-party DLL.
In other words, I doubt this is a LabVIEW problem at all and that you would have the same difficulty if you wrote a C program to talk to this driver. I only know what you've posted here about your system, but after many years of chasing down such issues, I would start with the device manufacturer/driver author.
If you have evidence to the contrary, please share.

Programming Arduino's from a centralized location

I have 16 Arduinos that are in very tight spaces and hard to get to when I need to reprogram them with my FTDI cable. I would like to have or create some sort of centralized place where I can connect my FTDI cable, make some sort of selection (switch of some sort), which will then connect the pins my cable is on to the selected arduino.
Does anyone have any thoughts on how this can be accomplished? I've toyed with transistors, but that takes a lot of them and didn't quite work.
Are there any premade solutions that are out there that I have yet to find?
Thanks very much!
Here is a thought! TCP to Serial.
Sound complicated.
Not really.
Note from AVRDUDE's manual the following
For programmers that attach to a serial port using some kind of higher
level protocol (as opposed to bit-bang style programmers), port can be
specified as net:host:port. In this case, instead of trying to open a
local device, a TCP network connection to (TCP) port on host is
established. The remote endpoint is assumed to be a terminal or
console server that connects the network stream to a local serial port
where the actual programmer has been attached to. The port is assumed
to be properly configured, for example using a transparent 8-bit data
connection without parity at 115200 Baud for a STK500.
With this, One could place your Arduino's behind your choice of TCP to Serial Server. Which is available in several forms. Cisco has a gang TS (but that is expensive, unless used). lantronix (and others have single end point devices. But then for Linux, there is "Net2Ser" which can serve up all your ttyS (aka Serial/COM ports).
With the later you could use a raspberry or TP-Link TL-WR703N (
In the latest IDE 1.5.6r2 add one entry for each TCP-to-Serial Port to ./Arduino/hardware/arduino/avr/programmers.txt file. While replacing the IP and Port with corresponding values.
TCP2001.name=TCP 2001
TCP2001.communication=serial
TCP2001.protocol=stk500v1
TCP2001.program.protocol=stk500v1
TCP2001.program.tool=avrdude
TCP2001.program.extra_params=-Pnet:192.168.1.100:2001
TCP2002.name=TCP 2002
TCP2002.communication=serial
TCP2002.protocol=stk500v1
TCP2002.program.protocol=stk500v1
TCP2002.program.tool=avrdude
TCP2002.program.extra_params=-Pnet:192.168.1.100:2002
...
Additionally change "protocol" to "upload.protocol" the following line in ./Arduino/hardware/arduino/avr/platform.txt
tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} -p{build.mcu} -c{protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i"
to
tools.avrdude.program.pattern="{cmd.path}" "-C{config.path}" {program.verbose} -p{build.mcu} -c{upload.protocol} {program.extra_params} "-Uflash:w:{build.path}/{build.project_name}.hex:i"
With this you can use "Upload Using Programmer" with the selected programmer to send over TCP, rather then use the Upload.
That all said, it will only work on Linux.
avrdude: ser_open(): network connects are currently notimplemented for Win32 environments

Using socat to relay one TTY stream to multiple TCP/IP destinations, plus to one 'sniffer' program

Using an embedded Linux development board, I need to put together a widget that does the following:
Reads packets in via physical serial port, and relays those packets to a number of IP addresses (up to 20 of them; with IP destinations read from a configuration file).
Also 'sniff' those serial packets using a custom program, perhaps written in c.
As someone with a programming background, the most obvious solution (to me) would be to create a c program from scratch to achieve the above. However, as this is something I need to throw together quickly, and because I need an excuse to learn more about existing Linux command-line programs and script writing (which I'm not so good at), I'm wondering if much of this could be achieved with existing command-line programs and a shell script. Then, the only part I write from scratch is my packet sniffer (call it sniffer.c).
I understand that netcat and socat can be used for relaying between devices and addresses, and I have started experimenting with both. The thought occurs to me that I could avoid having to develop and test TCP/IP software by running multiple instances of socat to relay serial data from the TTY port to remote IP addresses. Each instance of socat could handle a particular remote IP address.
Does this sound feasible, and if so, how could I effectively 'multiplex' a stream from /dev/ttyS0 (say) as the source for multiple instances of socat plus one instance of sniffer.c? Could one way be to relay data read from /dev/ttyS0 to a cache file, and then have my socat instances and sniffer.c all have a read-only access to that file?

linux serial ports -- mulithread program

I am working on a smartcard reader project here i will have to read/write data from the smartcard reader.
Also i will have to read/write data from PC application.
There are two serial port on my microcontroller one connected to smartcard reader other to PC.
Smartcard reader <------> Microcontroller <-----> PC
I have ported linux & using /ttys0 & /ttys1 driver for this.
1> My question is if application have to find that some data is available to be read from the port than will i have to always check it with read() system call ?
2> Does ttys0 driver have internal buffer to store received data ? Or data is lost if application do not read data immediately ?
3> Here using seprate threads for rx/tx from each port, is it right approach ?
Please guide me i am new to Embedded linux.
//John
Yes, there's a fair amount of buffering on linux tty's.
You have a few choices for how to interact with them.
you can make them non-blocking, and frequently poll to see if you can read data from them (but this may result in uselessly spinning CPU cycles, slowing other tasks)
you can use select() to yield to the scheduler until one of your devices has data for you to act on
you can use blocking I/O, however since you have multiple ports that may also require multiple threads
TTY programming is similar to socket programming in Linux. So basically you can set the socket to be a asynchronous and receive a signal once data is available. Regarding buffering, yes it's buffered using two flipping buffers. You can check chapter 18 in Linux device drivers 3rd edition regarding TTY implementation in the kernel.

Resources