c# - C# 和通信端口

标签 c# vb6 serial-port

嘿,我正在尝试使用以下 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/

相关文章:

c# - 如何比较两个忽略 C# 中某些键的 JSON?

c# - 将结构传递给非托管代码,从 C# DLL 到 VB6

winapi - 更改使用资源文件的VB6项目的exe图标

linux - “启用控制台 [ttyS0]”是什么意思?

java - 无法发送消息,无法从我的串行端口连接设备中读取响应

c# - .net 有样板端口吗?

C#:在 Winforms 中实现许可

c# - 是否可以在 Lambda 表达式中包含 SqlFunctions.StringConvert?

forms - VB6 .frm 文件格式属性无故更改!

c# - 串行端口 - 如何设置字符?