SerialPort.BaseStream.BeginWrite & BeginRead questions

I am having some problems trying to figure out the Async connection on a serialport object and hoping to get some help. I'm still learning Async, so be careful :)  

 

I basically need to make sure that the buffer is not full before writing to the serial port, and that all data was read before all data was written to avoid packet collisions. Unfortunately, the microcontroller I am writing does not have any acknowledgment or CTSEnabled, so I have to & mimic it in code.  

 

 

public partial class Form2 : Form
{
    System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort();
    System.Timers.Timer tmrPolling = new System.Timers.Timer(200);
    static byte [] inputPacket = new byte[2] { (byte)255, (byte)0 }; // Byte to send to SerialPort when input polling timer is activated.

    AsyncCallback callback;
    IAsyncResult result;

    Form2()
    {
        InitializeComponent();
        port.PortName = "COM9";
        port.BaudRate = 38400;
        port.DataBits = 8;
        port.StopBits = System.IO.Ports.StopBits.One;
        port.Parity = System.IO.Ports.Parity.None;
        port.ReadTimeout = 1;
        port.ReceivedBytesThreshold = 5;
        port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
        callback = new AsyncCallback(ReadComplete);  
        port.ReadBufferSize = (byte)60;
        port.WriteBufferSize = (byte)60;
        port.Open();  
        tmrPolling.Elapsed += new System.Timers.ElapsedEventHandler(tmrPolling_Elapsed);
    }

    void WritePacket(byte[] packet)
    {
        result = port.BaseStream.BeginWrite(packet, 0, packet.Count(), callback, port); // writes the input packet to the SerialPort - needed to read the IO values
    }

    void ReadComplete(IAsyncResult result)
    {

    }


    void tmrPolling_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {  
        WritePacket(inputPacket); // writes the input packet to the SerialPort - needed to read the IO values
    }

    void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        port.BaseStream.EndWrite(result);
        // Reads the data coming in from the serial port and calls the thread safe delegate to update the values on the form.
        byte [] received = new byte[port.BytesToRead];
        result = port.BaseStream.BeginRead(received, 0, received.Count(), callback, port);
        port.BaseStream.EndRead(result);  
    }

    private void UpdateValues()
    {
        byte[] b = new byte[] { (byte)255, (byte)6, (byte)hsbPan.Value, (byte)vsbTilt.Value, (byte)0,  
        (byte)0, (byte)0, (byte)0, (byte)13, (byte)10 };
        WritePacket(b);
    }

    private void chkEnablePolling_CheckedChanged(object sender, EventArgs e)
    {  
        tmrPolling.Enabled = chkEnablePolling.Checked;
        test.Enabled = chkEnablePolling.Checked;
        vsbInputInterval.Enabled = chkEnablePolling.Checked;
        vsbInputInterval.Value = Convert.ToInt32(tmrPolling.Interval);
        txtInputInterval.Enabled = chkEnablePolling.Checked;
        txtInputInterval.Text = ((float)tmrPolling.Interval / ( float)1000).ToString() + " sec";
    }

    private void vsbInputInterval_Scroll(object sender, ScrollEventArgs e)
    {
        // adjusts the tick frequency of the Timer for input polling. 
        txtInputInterval.Text = ((float)tmrPolling.Interval / (float)1000).ToString() + " sec";
        tmrPolling.Interval = vsbInputInterval.Value;
    } 

    private void vsbTilt_Scroll(object sender, ScrollEventArgs e)
    {  
        UpdateValues();
    }

    private void hsbPan_Scroll(object sender, ScrollEventArgs e)
    {
        UpdateValues();
    }
}
+3
source share
1 answer

I'm a little late, but I try anyway :)

, , , , .

, :

if(port.BytesToRead > 0){ // do something}
if(port.BytesToWrite > 0){ // do something}

, :

port.DiscardInBuffer();  // for the In-Buffer
port.DiscardOutBuffer(); // for the Out-Buffer

WritePacket.

void WritePacket(byte[] packet)
{
    if(port.BytesToRead > 0){ // read first all the data}

    // clear the in and out buffer to have a clean start
    port.DiscardInBuffer();  
    port.DiscardOutBuffer();  

    result = port.BaseStream.BeginWrite(packet, 0, packet.Count(), callback, port); // writes the input packet to the SerialPort - needed to read the IO values
}

, .

-1

Source: https://habr.com/ru/post/1762691/


All Articles