c# 和通信端口 [英] c # and Comm Ports
问题描述
嘿,我正在尝试使用以下 RS232 命令打开和关闭 A/V 接收器:
Hey all i am trying to turn an A/V reciever on and off this the following RS232 command:
@MAIN:VOL=Down & Chr$(13) & Chr$(10)
这在我的 VB6 应用中运行良好:
This works just fine in my VB6 app:
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# 应用程序中运行它:
However i can not seem to get it working in my C# app:
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).
I think the reason why its not working is the "\r" and "\n" replacing the vb6 Chr$(13) & Chr$(10).
CommunicationManager.cs:http://snipt.org/xmklh
CommunicationManager.cs: http://snipt.org/xmklh
推荐答案
我不确定 PCComm.CommunicationManager
是什么.但是,通过 Serial 进行通信非常简单,无需任何特殊 API.此 C# 代码等效于 VB6 代码:
I'm not sure what PCComm.CommunicationManager
is. However, it's fairly simple to communicate via Serial without any special APIs. This C# code is the equivalent of the VB6 code:
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 行执行此操作.
It's possible that your CommunicationManager
is failing because it is not setting the RtsEnable
property to true
. Your VB6 code is doing that on line 4.
这篇关于c# 和通信端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!