串行端口与 MSComm

发布于 2024-10-14 11:47:17 字数 893 浏览 5 评论 0原文

.Net SerialPort 和 VB6 MSComm 的工作方式是否可能不同?

在这两种情况下,我都从缓冲区读取数据,并且都得到了不同的字符串,如果我将 MSComm dll 导入到我的 .Net 项目中,它会完美地工作(显然)。

有人有更深入的信息吗?

如果有帮助,这里是我的简单示例,在这两种情况下我发送相同的字节数组...

VB6:

Dim MSComm1 As Object
Dim ArrToSend() As Byte
Dim IncomeData As String
Set MSComm1 = CreateObject("MSCommLib.MSComm")
With MSComm1
    .CommPort = 1
    .PortOpen = True
End With

ReDim ArrToSend(4)
ArrToSend(0) = 179
ArrToSend(1) = 1
ArrToSend(2) = 92
ArrToSend(3) = 92
MSComm1.Output = ArrToSend
IncomeData = MSComm1.Input

c#

SerialPort _serialPort = new SerialPort();
_serialPort.Open();
Byte[] _bytesToSend = new Byte[4];
_bytesToSend[0] = 179;
_bytesToSend[1] = 1;
_bytesToSend[2] = 92;
_bytesToSend[3] = 92;
_serialPort.Write(_bytesToSend, 0, _bytesToSend.Length);
String ReadExisting = _serialPort.ReadExisting();

Is it possible that .Net SerialPort and VB6 MSComm work different?

In both cases, I´m reading data from the buffer, and both got me different strings, if I import the MSComm dll into my .Net project, It works perfectly (obviously).

Does anyone have more in deep information?

If it helps, here are my to simple samples, in both cases I send the same Byte Array...

VB6:

Dim MSComm1 As Object
Dim ArrToSend() As Byte
Dim IncomeData As String
Set MSComm1 = CreateObject("MSCommLib.MSComm")
With MSComm1
    .CommPort = 1
    .PortOpen = True
End With

ReDim ArrToSend(4)
ArrToSend(0) = 179
ArrToSend(1) = 1
ArrToSend(2) = 92
ArrToSend(3) = 92
MSComm1.Output = ArrToSend
IncomeData = MSComm1.Input

c#

SerialPort _serialPort = new SerialPort();
_serialPort.Open();
Byte[] _bytesToSend = new Byte[4];
_bytesToSend[0] = 179;
_bytesToSend[1] = 1;
_bytesToSend[2] = 92;
_bytesToSend[3] = 92;
_serialPort.Write(_bytesToSend, 0, _bytesToSend.Length);
String ReadExisting = _serialPort.ReadExisting();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-10-21 11:47:17

您正在混合字节和字符串。 MSComm 对此非常宽松,但 SerialPort 关心文本编码。显然,您使用的是二进制协议,您收到的字符串可能包含无法转换为 SerialPort.Encoding(默认为 ASCII)的字节的问号。您必须使用 Read() 方法来获取响应。

You are mixing bytes and strings. MSComm was very lax about it but SerialPort cares about text encoding. Clearly you are using a binary protocol, it is likely that your received string is containing question marks for bytes that could not be converted to the SerialPort.Encoding (default is ASCII). You have to use the Read() method to get the response.

悲凉≈ 2024-10-21 11:47:17

我希望它们都使用底层操作系统API;但我猜想他们可能会以不同的方式使用该 API:例如使用不同的默认 COM 端口参数(如果您没有明确指定参数)。

另一个区别可能在于时间上:当您读取输入/回复时,您如何知道它是否已发送,以及如何知道“读取”或“输入”功能是否等待足够长的时间?

I expect that they both use the underlying O/S API; but I guess that they may each use that API in difference ways: e.g. with different default COM port parameters (if you don't specify the parameters explicitly).

Another difference may be in the timing: when you read the input/reply, how do you know whether it's been sent yet, and how do you know whether the 'read' or 'input' function is waiting for long enough?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文