简体   繁体   English

将 C# SerialPort 连接到 Hyper-V 命名管道(就像 PuTTy 可以)

[英]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).我正在开发一个连接到虚拟机的应用程序(使用 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. (我现在使用虚拟机进行开发,但最终产品将是物理串行连接机器。不是虚拟机)我已经创建了一个带有 COM 适配器的命名管道,如下所示。

在 Hyper-V 中创建命名管道

I am able to connect with PuTTy Like so.我可以像这样与 PuTTy 连接。

PuTTy 的连接信息

Running a CentOS Build on the Hyper-V在 Hyper-V 上运行 CentOS 构建

自定义 CentOS 终端

I then wrote a simple Application to connect to it with C#然后我写了一个简单的应用程序来用 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. msdn.microsoft.com/en-us/library/system.io.ports.serialport.portname我也试过以管理员身份运行我的应用程序(Putty 也需要这个)但它没有用。

With Oystein's Suggestion I tried:根据 Oystein 的建议,我尝试了:

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 在物理机上。

只找到 COM1

Does any one know how I can get this to work?有谁知道我怎样才能让它发挥作用? Thanks!谢谢!

You can Automate PuTTy Connections with PLink.您可以使用 PLink 自动化 PuTTy 连接。

I read on another website this Answer.我在另一个网站上阅读了这个答案。 USEPlink使用Plink

There is a plus side to this solution.这个解决方案有一个优点。 I will be able to automate Serial and SSH connections with the same tool/wrapper.我将能够使用相同的工具/包装器自动进行串行和 SSH 连接。 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.我在 Stack Overflow 上找到了这个,它帮助我完成了这个项目。 Answer 回答

Additionnally, PLink comes with the full install of Putty, and more usefull applications if you look where you installed it.此外,PLink 带有 Putty 的完整安装,以及更多有用的应用程序(如果您查看安装位置)。

  • pageant.exe选美程序
  • plink.exe链接程序
  • pscp.exe (windows to linux File transfer) pscp.exe (windows to linux 文件传输)
  • psftp.exe psftp.exe

Hyper-V connects virtual COM port on guest to Win32 named pipe on Host. Hyper-V 将来宾上的虚拟 COM 端口连接到主机上的 Win32 命名管道。 On host you should connect to named pipe ( NamedPipeClientStream ), not serial port.在主机上,您应该连接到命名管道( NamedPipeClientStream ),而不是串行端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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