gpt4 book ai didi

Forcing handshake signals on a serial port(在串口上强制握手信号)

转载 作者:bug小助手 更新时间:2023-10-25 19:58:23 25 4
gpt4 key购买 nike



I have a piece of equipment that I control via an RS-232 serial interface. It has a basic binary protocol for sending commands and receiving results, including a command for powering it down. However, to power it up you cannot use a serial command since its UART is inactive. Instead you hold the two hardware handshake signals high for over a second and the device powers up.
I have been reading up on Microsoft's documentation and ended up with the code below. It does not throw any exceptions but does not work either. Obviously my app contains much more code than the below example but I believe it covers the gist of it. What am I missing?

我有一台通过RS-232串口控制的设备。它有一个基本的二进制协议,用于发送命令和接收结果,包括关闭它的命令。但是,由于其UART处于非活动状态,因此不能使用串口命令来给其通电。相反,您将两个硬件握手信号保持为高电平超过一秒钟,设备就会通电。我一直在阅读微软的文档,最后得到了下面的代码。它不抛出任何异常,但也不工作。显然,我的应用程序包含的代码比下面的示例多得多,但我相信它涵盖了它的要点。我遗漏了什么?


[DllImport("kernel32.dll")\]
internal static extern bool EscapeCommFunction(IntPtr hFile, uint dwFunc);

internal const uint SETRTS = 3;
internal const uint CLRRTS = 4;
internal const uint SETDTR = 5;
internal const uint CLRDTR = 6;

Port = new SerialPort("COM7" : comPort, 9600, Parity.None, 8, StopBits.One);

object stream = typeof(SerialPort).GetField("internalSerialStream", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Port);
SafeFileHandle handle = (SafeFileHandle)stream.GetType().GetField("\_handle", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(stream);

IntPtr hFile = handle.DangerousGetHandle();

EscapeCommFunction(hFile, SETDTR);
EscapeCommFunction(hFile, SETRTS);
Thread.Sleep(1500);
EscapeCommFunction(hFile, CLRDTR);
EscapeCommFunction(hFile, CLRRTS);


I have tried setting the Handshake, DtrEnable, and RtsEnable properties to various values without seeing any difference.

我尝试将握手、DtrEnable和RtsEnable属性设置为不同的值,但没有看到任何差异。


更多回答

Did you measure the pins to see if and what actually goes to high signal?

你有没有测量引脚,看看是否高信号,以及高信号是什么?

Why not try the same thing using C++ and the Win32API directly and see if it makes a difference?

为什么不直接使用C++和Win32API来尝试同样的事情,看看是否会有所不同呢?

This pinvoke doesn't do anything more than the Dtr/RtsEnable properties do. Crucial is to set the Handshake property to Handshake.None so you can control the lines in software. If that doesn't help then use a program like Putty to validate the wiring, port selection and device behavior.

除了Dtr/RtsEnable属性之外,此pInvoke不做任何其他事情。关键是要将握手属性设置为Handshake.None,这样您就可以控制软件中的线路。如果这样做没有帮助,那么使用像Putty这样的程序来验证布线、端口选择和设备行为。

Thanks for the answers. I have measured the pins. The interface works just fine via Putty. The Handshake property has no effect on the behavior.

谢谢你的回答。我已经量过大头针了。通过Putty,界面工作得很好。握手属性对行为没有影响。

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com