visualstudio2010-从c#中的FTDI串行端口读取22字节太慢

本文关键字:字节 读取 串行端口 中的 FTDI visualstudio2010- | 更新日期: 2023-09-27 18:00:39

我有一个FTDI串行端口,我必须在串行端口中写入一些东西才能开始读取22字节我有一个非常奇怪的问题。我读这个数据太慢

我不知道我的问题是什么

这是我的代码:`

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    int count;
    private void button1_Click(object sender, EventArgs e)
    {
       // serialPort1.Open();
       // serialPort1.Write(Convert.ToString(0x02));  //here we converted the same method to convert hex to string to be send using serial port
        //textBox3.Text = (Convert.ToString(0x02));   //here we will see what will be sent with serial port
        serialPort1.Write(textBox5.Text);
     //   serialPort1.Close();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = textBox1.Text;
        serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);

    }
    // int rs;
    byte[] rs=new byte[22];
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
           // rs = serialPort1.ReadByte();
            serialPort1.Read(rs, 0, 22);
            this.Invoke(new EventHandler(type));
        }
        catch (System.TimeoutException) { }
    }
    void type(object s,EventArgs e)
    {
        textBox4.Text = rs.ToString(); //this method should view what is being recieve by the serial port :)
        label4.Text =rs[0].ToString();
        label5.Text = "" + rs[1];
        label6.Text = "" + rs[2];
        label7.Text = "" + rs[3];
        label8.Text = "" + rs[4];
        label9.Text = "" + rs[5];
        label10.Text = "" + rs[6];
        label11.Text = "" + rs[7];
        label12.Text = "" + rs[8];
        label13.Text = "" + rs[9];
        label14.Text = "" + rs[10];
        label15.Text = "" + rs[11];
        label16.Text = "" + rs[12];
        label17.Text = "" + rs[13];
        label18.Text = "" + rs[14];
        label19.Text = "" + rs[15];
        label20.Text = "" + rs[16];
        label21.Text = "" + rs[17];
        label22.Text = "" + rs[18];
        label23.Text = "" + rs[19];
        label24.Text = "" + rs[20];
        label25.Text = "" + rs[21];
        count++;
    }
    private void button3_Click(object sender, EventArgs e)
    {
        serialPort1.Open();
        // start timer to count
        serialPort1.Write("?");
        serialPort1.Write("5");
        serialPort1.Write("1");
        serialPort1.Write("&");
        timer1.Start();
    }
    private void button4_Click(object sender, EventArgs e)
    {
        serialPort1.Close();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        // after 1000 mili second this timer will do the following 
        timer1.Stop();
        textBox6.Text = count.ToString();
        count = 0; // re initialize the count variable :)
        timer1.Start();
    }
}

}

当我使用serialport1-readExisting();时,时间是真的(正是我想要的(5/5)),但数据是字符串,我的数据是整数,所以它没有用处。

我确信我的问题与使用serialport1.Read(rs,0,22)有关,但我真的不知道我必须做什么

如果有人能帮我,我会很感激的。。

visualstudio2010-从c#中的FTDI串行端口读取22字节太慢

        serialPort1.Read(rs, 0, 22);

不能忽略Read()的返回值,它不会是22。如果您立即调用,也不能忽略e.EventType。你不应该在收集之前收集整个响应。你只需要另一个变量来跟踪你收到的字节数。修复:

    byte[] rs = new byte[22];
    int rsCnt = 0;
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) {
        if (e.EventType != SerialData.Chars) return;
        rsCnt += serialPort1.Read(rs, rsCnt, 22 - rsCnt);
        if (rsCnt == 22) {
            this.BeginInvoke(new Action(() => type(rs)));
            rs = new byte[22];
            rsCnt = 0;
        }
    }
    private void type(byte[] data) {
        // etc...
    }

检查e.EventType可以避免TimeoutException,如果将ReadTimeout设置为0,则不需要它。在调用serialPort1.Close()方法时,使用BeginInvoke()而不是Invoke(。当type()忙于处理数据而端口继续接收数据时,重新创建缓冲区可以避免线程竞争。

波特率以比特每秒为单位设置,因此请确保其足够高,以满足您预期的传输速度,因此8192波特将为每秒1KB(1024字节)。

以下是一篇可能对您有所帮助的文章:http://www.codeproject.com/Articles/75770/Basic-serial-port-listening-application