Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

the fastest serial port communication possible in C#?

I am new at this forum and I do not have many experiences with programming in C#. I have build simple Windows application in C# to communicate with some electronic board that my friend gave it to me. He has defined serial port baud rate in microcontroller at 38400. I want to send and receive bytes via serial port as fast as possible. With existing serialport tool in C# I was able only to read bytes but still not fast enough even with eventhandler in its own thread. My buffer[4096] was full in few seconds.

My question is if there exist some other functions or tools for serial port communication that does not use so much processor time. I need to read one byte in less than 200 us. I have more experiences with embedded systems where this was not a problem.

Thanks everyone for some advice or idea.

Toma?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

thanks for all replies. I can read all bytes coming from board, but I can not reply fast enough. Here is my code. I have used an event for reading buffer. If I try to reply in event the next bytes contains wrong values.

#region namespace usings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

using Microsoft.Win32;
using Zaber.Serial.Core;
using Diagnostics = System.Diagnostics;
#endregion

namespace Lucifire_aplication {

public partial class Form1 : Form
{


    int k = 1;
    string number_of_bytes;
    byte checksum;
    byte[] buffer = new byte[11];
    byte[] buffer_old = new byte[11];


    public Form1()
    {
        InitializeComponent();
        configure_button.Enabled = false;
        start_button.Enabled = false;
        foreach (string s in SerialPort.GetPortNames())
        {
            serial_port_name.Items.Add(s);
        }
        serial_port_name.Text = "Select port";
        for (int i = 4800; i <= 38400; i = i * 2)
        {
            baud_rate.Items.Add(i);
        }
        for (int i = 57600; i <= 921600; i = i * 2)
        {
            baud_rate.Items.Add(i);
        }

        serial_port_name.DropDownStyle = ComboBoxStyle.DropDownList;
        baud_rate.SelectedIndex = 3;
        baud_rate.DropDownStyle = ComboBoxStyle.DropDownList;


    }

    private void serial_port_name_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (serial_port_name.Text != "Select port")
        {
            configure_button.Enabled = true;
        }
    }

    private void configure_button_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = serial_port_name.Text;
        serialPort1.BaudRate = Int32.Parse(baud_rate.Text);
        serialPort1.DataBits = 8;
        serialPort1.StopBits = StopBits.One;
        //serialPort1.Handshake = Handshake.None;
        serialPort1.ReceivedBytesThreshold = 44;
        serialPort1.ReadBufferSize = 8192;
        MessageBox.Show("Communication is set on " + serialPort1.PortName + " with baud rate: " + serialPort1.BaudRate);
        start_button.Enabled = true;
    }

    private void start_button_Click(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen)
        {

            serialPort1.Close();
            start_button.Text = "START";
            read_button.Enabled = false;
            //read_write.Enabled = false;
            comport_status.Text = "DISCONNECTED";
        }
        else
        {
            serialPort1.Open();
            start_button.Text = "STOP";
            read_button.Enabled = true;
            //read_write.Enabled = true;
            comport_status.Text = "CONNECTED";
        }
    }

    private void quitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.Application.Exit();
    }

    private void read_button_Click(object sender, EventArgs e)
    {
        bytes_to_read.Text = serialPort1.BytesToRead.ToString();
        read_data.Text = serialPort1.ReadExisting().ToString();
        read_data.Text += k.ToString() + "   ";
        if (buffer_old[0] == 4)
        {
            serialPort1.ReadByte();
        }
        serialPort1.Read(buffer, 0, 11);
        for (int j = 0; j < 11; j++)
        {
            read_data.Text += buffer[j].ToString() + "  ";
        }
        read_data.Text += "
";
        k++;
        buffer_old = buffer;
    }

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

        serialPort1 = (SerialPort)sender;
        number_of_bytes = serialPort1.BytesToRead.ToString();
        if (buffer_old[0] == 4)
        {
            serialPort1.ReadByte();
        }
        serialPort1.Read(buffer, 0, 11);
        buffer_old = buffer;
        this.Invoke(new EventHandler(displaytext));
        Thread.Sleep(20);
    }

    private void displaytext(object sender, EventArgs e)
    {
        checksum = 0;
        bytes_to_read.Text = number_of_bytes;
        read_data.Text += k.ToString() + "	";
        for (int j = 0; j < 11; j++)
        {
            read_data.Text += buffer[j].ToString() + "  ";
            checksum += buffer[j]; 
        }
        checksum -= buffer[10];
        read_data.Text += "  	" + checksum.ToString();
        read_data.Text += "
";
        k++;
    }

}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...