首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#和Comm端口

C#和Comm端口
EN

Stack Overflow用户
提问于 2011-08-10 10:07:17
回答 1查看 879关注 0票数 2

嘿,我正在试着用下面的RS232命令打开和关闭音频/视频接收器:

代码语言:javascript
复制
 @MAIN:VOL=Down & Chr$(13) & Chr$(10)

这在我的VB6应用中运行得很好:

代码语言:javascript
复制
 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#应用程序中使用它:

代码语言:javascript
复制
 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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-10 10:20:28

我不确定PCComm.CommunicationManager是什么。但是,通过Serial进行通信相当简单,不需要任何特殊的API。此C#代码等同于VB6代码:

代码语言:javascript
复制
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行就是这样做的。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7005141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档