- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在使用一种称为 Fluke 5500A 多产品校准设备的设备。我用 C# 编写了一个小程序来与它交互并了解有关它如何工作的更多信息,但不幸的是 SerialPort.DataReceived 给了我一些非常奇怪的结果。该程序不长,所以我将在此处完整发布它:
class Program
{
static public bool isExecuting = true;
static private string serialCommand;
static private string dataReceived;
static void Main(string[] args)
{
SerialPortConnection serialPort = new SerialPortConnection();
serialPort.OpenSerialConnection();
while (isExecuting == true)
{
Console.WriteLine("Enter a command or type q to quit.");
serialCommand = Console.ReadLine().ToUpper();
if (serialCommand == "Q")
isExecuting = false;
else if (serialCommand == "CLEAR")
Console.Clear();
else
{
dataReceived = serialPort.WriteSerialConnection(serialCommand);
Console.WriteLine(dataReceived);
}
}
serialPort.CloseSerialConnection();
}
}
还有我的 SerialPortConnection 类:
public class SerialPortConnection
{
private SerialPort serialPort;
private string dataReceived = "none";
public SerialPortConnection(string comPort = "Com3", int baud = 9600, System.IO.Ports.Parity parity = System.IO.Ports.Parity.None, int dataBits = 8, System.IO.Ports.StopBits stopBits = System.IO.Ports.StopBits.One)
{
serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits);
}
public void OpenSerialConnection()
{
try
{
serialPort.Open();
}
catch (Exception e)
{
Console.Write("\nError");
Console.Write(e);
}
}
public string WriteSerialConnection(string SerialCommand)
{
try
{
serialPort.Write(String.Format(SerialCommand + "\r"));
dataReceived = serialPort.ReadExisting();
return dataReceived;
}
catch (Exception e)
{
Console.Write("\nError");
Console.Write(e);
return "Execution Unsuccessful";
}
}
public void CloseSerialConnection()
{
try
{
serialPort.Close();
}
catch (Exception e)
{
Console.Write("\nError");
Console.Write(e);
}
}
}
我目前的问题是控制台的输出看起来像这样:
Enter a command or type q to quit.
*IDN?
Enter a command or type q to quit.
OUT 50V <-- Command input
*IDN? <-- Previous command echoed back
FLUKE,5500A,8030005,2.61+1.3+2.0+* <-- Data received from previous command
161>
Enter a command or type q to quit.
OPER
OUT 50V
162>
Enter a command or type q to quit.
STBY
OPER
163>
Enter a command or type q to quit.
*RST
STBY
164>
Enter a command or type q to quit.
命令执行得很好,但控制台的输出似乎是最后执行的命令以及该命令返回的任何数据。我不知道为什么会这样。
编辑:
感谢 Robert P 的回答,我实现了以下代码:
var received = "";
bool isReading = true;
while (isReading == true)
{
try
{
received += serialPort.ReadExisting();
if (received.Contains('>'))
isReading = false;
}
catch (Exception e)
{
}
}
Console.WriteLine(received);
最佳答案
你的问题的一部分是串行通信远非即时的。 .ReadExisting()
方法从 SerialPort
对象的缓冲区中提取数据,但不确保设备已完成发出命令。可以这样想:
+----+ +----------+ +------------+
|Your|--Command-->|Serial |---UART--->| Device |
|Code| |Port Obj | | |
+----+ +----------+ +------------+
+----+ +----------+ +------------+
|Your|<--Read-----|Serial | |Device |
|Code| (is empty) | | |(processing)|
+----+ +----------+ +------------+
+----+ +----------+ +------------+
|Your| |Serial |<-response-|Device |
|Code| |(has data)| |(responding)|
+----+ +----------+ +------------+
+----+ +----------+ +------------+
|Your|--Command2->|Serial |---UART--->| Device |
|Code| |(has data)| | |
+----+ +----------+ +------------+
+----+ +----------+ +------------+
|Your|<--Read-----|Serial | |Device |
|Code| (previous | | |(processing)|
+----+ response) +----------+ +------------+
相反,在发送下一个命令之前寻找模式、 token 或其他识别标记以了解传输已完成。继续阅读(预计超时 - 它们会作为异常抛出!)直到您知道您已收到完整的回复。在这种情况下,>
字符可能表示“准备好接受更多输入”。您也可以将其解释为“响应已完成”。
关于c# - SerialPort.DataReceived 返回奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574108/
我有一个 SerialPort 用于连接到虚拟 COM 端口。由于连接是持久的,我必须保留对 SerialPort 的引用,以便打开、关闭和管理端口。我还在我的类上实现了 IDisposable(不是
我正在尝试使用node.js串行端口模块在运行时获取设备的COM端口。 const SerialPort = require('serialport'); var fs = require('fs')
我目前有一个使用 VB6 代码制作的程序,该程序使用 MSCOMM 控件从串行端口拉回数据。这成功地从我的串行端口接收数据,其中连接了 Denso BHT-904B 设备。 我现在正尝试将此代码移至
从 SerialPort 对象外部看,写入缓冲区的大小以及是否已满似乎没有区别。 使用同步写入,write 方法会阻塞,直到所有数据都已发送且缓冲区为空。 使用异步写入,数据排队,程序继续运行。直到写
我正在用 C# 开发一个 .NET 4.0 WPF 应用程序,它通过 RS232 控制电机。我在退出应用程序时遇到问题,应用程序有时会在关闭端口时死锁。 在互联网上进行一些研究后,我注意到这是一个常见
这个问题的灵感来自这里的问题和答案: Passing a Command to a Comm Port Using C#'s SerialPort Class 这个问题本身回答了我遇到的一些问题,但也
假设我们有以下代码: SerialPort port = new SerialPort("COM2", 115200); port.Open(); // Thread.Sleep(5000); //
使用串行端口库的人注意到了这一点,还是只有我?我发现(非常)很少有人处于相同的情况,但我没有找到解决方案。 这是我使用 ruby serialport 得到的示例: ff d8 ff e0 00
我正在使用 Ruby 1.8.7 SerialPort gem 通过 FTDI232 USB 转串口转换器与 AVR 通信。我似乎遇到了 SerialPort 初始化新 SerialPort 的问题。
我正在开发需要与 COM 端口交互的程序。 从这个问答中学习:.NET SerialPort DataReceived event not firing , 我这样写我的代码。 namespace C
我正在监听串口 5 个字节的信息。轮询的完整答案需要 5 个字节。我的代码监听 serialport data_received 事件并检查 bytesToRead。有时,事件会触发并且 bytesT
我有一个 USB 电话连接到我的电脑上 我如何使用串口检测它所在的 COM? 最佳答案 遍历所有 COM 端口并尝试从每个端口获取一个标识。下面是一个最小的示例,应该对其进行扩展以进行更好的错误检查等
因为我是多线程应用程序的新手,所以我想在开始编写代码之前从更有经验的人那里得到一些建议... 我需要在串口事件中对串口接收到的数据进行排队,以便进一步处理。 所以我有以下事件处理程序: voi
我正在开发需要与 COM 端口交互的程序。 从这个问答中学习:.NET SerialPort DataReceived event not firing , 我这样写我的代码。 namespace C
偶尔,我的一些集成测试会因上述消息而失败。我正在使用下面的代码来准备端口。 for(int i = 0; i < 5; i++) {
我有两个设备想通过串行接口(interface)连接,但它们的连接不兼容。为了解决这个问题,我将它们都连接到我的 PC,并且我正在开发一个 C# 程序,它将 COM 端口 X 上的流量路由到 COM
我在尝试让串行端口接收正确消息时遇到了很多问题。它不断截断消息。这是我的代码,我将在代码之后尝试详细说明。 public SerialComms(SerialPort sp) { this.sp
我正在使用串行通信,我想知道是否应该保持 SerialPort 打开? 我正在创建一个命令队列,一次只会运行一个。我应该创建一个 SerialPort 并在每个命令中打开/关闭它,还是应该有另一个类来
我一直在尝试使用模块 Win32::SerialPort在 perl 5.10 (也许它与 5.14 有一些问题)。我必须向串行端口发出命令并获取它们的输出。我已经发现 write 将为我完成前面的部
我有一个 WPF 测试应用程序,用于评估基于事件的串行端口通信(与轮询串行端口)。问题是 DataReceived 事件似乎根本没有触发。 我有一个非常基本的 WPF 表单,其中包含用于用户输入的 T
我是一名优秀的程序员,十分优秀!