gpt4 book ai didi

c# - Windows CE和串口问题(打不开端口)

转载 作者:行者123 更新时间:2023-11-30 22:42:43 25 4
gpt4 key购买 nike

我正在尝试为 GPS 设备编写自定义应用程序,我需要读取 GPS 数据。

我面临的问题是我无法打开 GPS 端口。我知道它是哪个 COM,我也知道波特率,但每当我进入 Open() 方法时,我都会得到一个 IOException

port = new SerialPort("COM6", 9600);
port.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
try
{
port.Open();
}
catch (IOException ex)
{
SetLabel(label1, ex.Message);
}

堆栈跟踪:

at System.IO.Ports.SerialStream.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at PortTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at PortTest.Program.Main()

让事情变得非常令人恼火的是,我发现的示例 native C++ 代码确实有效,可以毫无问题地打开端口,我什至可以浏览一些 NMEA 字符串。但是这一切都是使用 Web 上提供的示例 C++ 代码完成的,我觉得我的整个应用程序都不够熟练,无法以这种语言为基础。我想坚持使用 C#。

是否有任何其他方法可以使用 Windows CE 中的 SerialPort 类在 C# 中打开端口?或者可能完全使用其他类?
如果没有,是否有 C++ 编写的 DLL 允许相同(或相似)的功能可用于 Windows CE?

编辑(更多细节):我得到的异常(exception)就是这样。异常。 Visual Studio 调试器没有告诉我更多信息。我不知道这是否是由于设备上一些拙劣的 Windows CE 设置造成的。我确实记得我的 Windows Mobile 设备上有一个异常字符串问题,但通过添加对 System.SR 的引用解决了这个问题,我已经在这种情况下尝试过。如果我遗漏了有关 Windows CE 和异常消息的一些技巧,我也很想知道。 ;)

我也尝试过在没有事件的情况下使用代码并在构造函数中指定更多参数,但在尝试打开端口时我总是会遇到异常。

最后,我已经尝试在端口名称中添加一个 :,这也没有帮助。

最佳答案

我建议为构造函数提供更多选项;奇偶校验、停止位和握手模式。

SerialPort port = new SerialPort ("COM6", 9600, Parity.None, 8, StopBits.One);
port.Handshake = Handshake.None;

关于c# - Windows CE和串口问题(打不开端口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4277968/

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