在C#中读取串口 [英] Reading serial port in C#

查看:109
本文介绍了在C#中读取串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我试图通过C#通过我的PC上的RS232连接与一个Aglient设备通过XP操作系统与我交谈,我遇到了麻烦。我可以发送一个命令,例如有一个查询(* IDN?),我从来没有在我的GUI中得到响应。但是,如果我打开超级终端,设备IDN会显示为超级终端,就像它应该出现在我的GUI中一样。从我的C#GUI,我可以发送一个cammand到设备来改变电压电平,设备改变电压电平,因为我可以在设备显示屏上看到它。但是,当我向设备发送查询时,我希望看到设备发送的输入缓冲区中的数据,但输入缓冲区始终为空。我连接了多个设备,我还没有从输入缓冲区读取任何数据。我可以使用hyperterminal和Labview写入输出缓冲区并从输入缓冲区中读取正确的响应。我可以通过我的C#GUI发送查询,并使用超级终端和Labview从输入缓冲区中读取正确的响应。使用C#读取输入缓冲区时是否有错误?



我尝试过:



这是我的代码:

Hello,

I am trying to talk to an Aglient device via C# through a RS232 connection on my PC with XP OS and I am having trouble. I can send a command, such has a query (*IDN?), and I never get a response in my GUI. However, if I open hyperterminal the device IDN shows up in hyperterminal like it should have showed up in my GUI. From my C# GUI, I can sent a cammand to the device to change voltage level and the device changes voltage level as I can see it on the device display. However, when I send a query to the device, I expect to see data in the input buffer that was sent by the device, but the input buffer is always empty. I have connect more than one device and I have yet to read any data from the input buffer. I can write to the output buffer and read the correct reponse from the input buffer using hyperterminal and Labview. I can send the query via my C# GUI and read the correct response from the input buffer using hyperterminal and Labview. Is there a bug when reading the input buffer using C#?

What I have tried:

this is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        // Set the COM1 serial port to speed = 4800 baud, parity = odd,
        // data bits = 8, stop bits = 1.
        SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

        public Form1()
        {
            InitializeComponent();
            this.sendRead.Text = "Enter Commands or queries here";

            // set read time out to 2 seconds
            port.ReadTimeout = 2000;

            // open serial port
            port.Open();

            //Enable Event Handler
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        }

        private void sendRead_TextChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //write line to serial port
                port.WriteLine(sendRead.Text);

                //clear the text box
                sendRead.Text = "";
            }

            catch (System.Exception ex)
            {
                sendRead.Text = ex.Message;
            }
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int intBuffer;
            intBuffer = port.BytesToRead;
            byte[] byteBuffer = new byte[intBuffer];
            port.Read(byteBuffer, 0, intBuffer);
            this.Invoke(new EventHandler(DoUpDate));
        }

        private void DoUpDate(object s, EventArgs e)
        {
            sendRead.Text = port.ReadLine();
        }

        private void Form1_FromClosing(object sender, EventArgs e)
        {
            port.Close();
        }
    }
}

推荐答案

引用:

private void port_DataReceived(object sender,SerialDataReceivedEventArgs e)

{

int intBuffer;

intBuffer = port.BytesToRead;

byte [] byteBuffer = new byte [intBuffer];

port.Read(byteBuffer,0,intBuffer); //< ---你在这里阅读(和丢弃)数据

this.Invoke(new EventHandler(DoUpDate));

}



private void DoUpDate(object s,EventArgs e)

{

sendRead.Text = port.ReadLine(); //< ---你正在尝试再次阅读

}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int intBuffer;
intBuffer = port.BytesToRead;
byte[] byteBuffer = new byte[intBuffer];
port.Read(byteBuffer, 0, intBuffer); //<--- YOU ARE READING (AND DISCARDING) DATA HERE
this.Invoke(new EventHandler(DoUpDate));
}

private void DoUpDate(object s, EventArgs e)
{
sendRead.Text = port.ReadLine(); //<--- YOU ARE TRYING TO READ AGAIN
}





问题是你正在阅读然后丢弃 DataReceived 处理程序中的读取数据。您应该将读取的数据传递给 DoUpdate 方法。



The problem is you are reading and then discarding the read data inside the DataReceived handler. You should pass the read data to the DoUpdate method.


尝试设置 DtrEnable [ ^ ]和 RtsEnable [ ^ ]属性到 true :可能是该单位期望HyperTerminal设置为高的硬件握手。



但也试试将设备中的数据添加到DoUpDate方法中的字符串:

Try setting the DtrEnable[^] and RtsEnable[^] properties to true: it may be that the unit expects hardware handshaking that HyperTerminal is setting high.

But also try adding the data from your device to the string in your DoUpDate method:
private void DoUpDate(object s, EventArgs e)
        {
            sendRead.Text += port.ReadLine();
        }

串口是慢速设备 - 9600波特少于每秒1000个字符 - 所以你很可能会为每个字符获得一个单独的DataReceived事件。如果最后一个是空格或不可打印,您可能在文本框中看不到任何有用的内容。

Serial ports are slow devices - 9600 baud is less that 1000 characters per second - so you will very likely get a separate DataReceived event for each character. If the final one is a space or unprintable, you might not see anything useful in your text box.


// Set the COM1 serial port to speed = 4800 baud, parity = odd,
// data bits = 8, stop bits = 1.
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);



评论和代码在速度和平价方面不一致,哪一个是正确的?


Comment and code disagree on speed and parity, which one is correct ?

Quote:

但是,如果我打开超级终端,设备IDN会显示在超级终端中,就像它应该出现在我的GUI中一样。

However, if I open hyperterminal the device IDN shows up in hyperterminal like it should have showed up in my GUI.



使用Hyper终端,您可以检查您的GUI应用程序是否正确地向设备发送命令。


Using Hyper terminal you can check if your GUI app send command correctly to device.


这篇关于在C#中读取串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆