简体   繁体   中英

Connecting C# SerialPort to Hyper-V named pipe (Just Like PuTTy can)

I am working on an Application to Connect to virtual machines(using Hyper-V). (I am using Virtual Machines now for Development, but the end-product will be physical Serial Connected Machines. not VM's) I have created a named pipe with the COM adaptor like this.

在 Hyper-V 中创建命名管道

I am able to connect with PuTTy Like so.

PuTTy 的连接信息

Running a CentOS Build on the Hyper-V

自定义 CentOS 终端

I then wrote a simple Application to connect to it with C#

my_Serial_Port = new SerialPort();
my_Serial_Port.PortName = @"\\.\pipe\ttyS0";
my_Serial_Port.BaudRate = m_BaudRate;
my_Serial_Port.DataBits = 8;
my_Serial_Port.Parity = Parity.None;
my_Serial_Port.StopBits = StopBits.One;
my_Serial_Port.Handshake = Handshake.None;
my_Serial_Port.DtrEnable = true;
my_Serial_Port.RtsEnable = true;
my_Serial_Port.WriteTimeout = 15000;
my_Serial_Port.ReadTimeout = 15000;

my_Serial_Port.ErrorReceived += my_Serial_Port_ErrorReceived;

The thing is when I open the connection, I get this Exception thrown.

参数端口名不能以 / 开头

A little searching, and I found this information but no work-around. msdn.microsoft.com/en-us/library/system.io.ports.serialport.portname I have also Tried running my application as Administrator (Putty Needed this too) And it did not work.

With Oystein's Suggestion I tried:

string[] ports = SerialPort.GetPortNames();

string result = "The following serial ports were found:" + Environment.NewLine;

// Display each port name to the console.
foreach (string port in ports)
{
    result += port + Environment.NewLine;
}

MessageBox.Show(result);

Unfortunately COM1 is on the Physical Machine.

只找到 COM1

Does any one know how I can get this to work? Thanks!

You can Automate PuTTy Connections with PLink.

I read on another website this Answer. USEPlink

There is a plus side to this solution. I will be able to automate Serial and SSH connections with the same tool/wrapper. Using functions like

"Send_Command(string command);" 

and

"Wait_For_Prompt(string prompt, int timeout);"

I found this on Stack Overflow, which helped me to complete the project. Answer

Additionnally, PLink comes with the full install of Putty, and more usefull applications if you look where you installed it.

  • pageant.exe
  • plink.exe
  • pscp.exe (windows to linux File transfer)
  • psftp.exe

Hyper-V connects virtual COM port on guest to Win32 named pipe on Host. On host you should connect to named pipe ( NamedPipeClientStream ), not serial port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM