嘿,我正在尝试使用以下 RS232 命令打开和关闭 A/V 接收器:
@MAIN:VOL=Down & Chr$(13) & Chr$(10)
这在我的 VB6 应用程序中运行得很好:
MSCommAV.CommPort = 4
MSCommAV.RThreshold = 1
MSCommAV.Settings = "9600,N,8,1"
MSCommAV.RTSEnable = True
MSCommAV.PortOpen = True
MSCommAV.Output = "@MAIN:VOL=Down" & Chr$(13) & Chr$(10)
但是我似乎无法让它在我的 C# 应用程序中工作:
PCComm.CommunicationManager commAV = new PCComm.CommunicationManager();
commAV.Parity = "None";
commAV.StopBits = "One";
commAV.DataBits = "8";
commAV.BaudRate = "9600";
commAV.PortName = "COM4";
commAV.CurrentTransmissionType = PCComm.CommunicationManager.TransmissionType.Text; //.Hex
commAV.OpenPort();
commAV.WriteData("@MAIN:VOL=Down" + "\r" + "\n"); //Vol DOWN
我认为它不起作用的原因是“\r”和“\n”替换了 vb6 Chr$(13) 和 Chr$(10)。
CommunicationManager.cs:http://snipt.org/xmklh
最佳答案
我不确定 PCComm.CommunicationManager
是什么。然而,通过串行通信相当简单,无需任何特殊的 API。此 C# 代码相当于 VB6 代码:
var port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
port.RtsEnable = true;
port.Open();
port.Write("@MAIN:VOL=Down\r\n");
port.Close();
编辑:
您的 CommunicationManager
可能会失败,因为它未将 RtsEnable
属性设置为 true
。您的 VB6 代码在第 4 行执行此操作。
关于c# - C# 和通信端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005141/