SerialPort.BaseStream.BeginWrite & BeginRead issues

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Im having some issues trying to figure out Async communication on a serialport object and was hoping to get some help. Im still learning Async stuff so be gentle:)

I basically need to make sure that the buffer isnt full prior to writing to the serial port and also all data has been read before any more data gets written to avoid packet collision. Unfortunately the microcontroller that I am writing to does not have
any handshaking or CTSEnabled so I have to mimic it in code.


<div style="color:Black; background-color:White
<pre><span style="color:Blue public <span style="color:Blue partial <span style="color:Blue class Form2 : Form
{
System.IO.Ports.SerialPort port = <span style="color:Blue new System.IO.Ports.SerialPort();
System.Timers.Timer tmrPolling = <span style="color:Blue new System.Timers.Timer(200);
<span style="color:Blue static <span style="color:Blue byte[] inputPacket = <span style="color:Blue new <span style="color:Blue byte[2] { (<span style="color:Blue byte)255, (<span style="color:Blue byte)0 }; <span style="color:Green // Byte to send to SerialPort when input polling timer is activated.

AsyncCallback callback;
IAsyncResult result;

<span style="color:Blue public Form2()
{
InitializeComponent();
port.PortName = <span style="color:#A31515 "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 += <span style="color:Blue new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
callback = <span style="color:Blue new AsyncCallback(ReadComplete);
port.ReadBufferSize = (<span style="color:Blue byte)60;
port.WriteBufferSize = (<span style="color:Blue byte)60;
port.Open();
tmrPolling.Elapsed += <span style="color:Blue new System.Timers.ElapsedEventHandler(tmrPolling_Elapsed);
}

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

<span style="color:Blue void ReadComplete(IAsyncResult result)
{

}


<span style="color:Blue void tmrPolling_Elapsed(<span style="color:Blue object sender, System.Timers.ElapsedEventArgs e)
{
WritePacket(inputPacket); <span style="color:Green // writes the input packet to the SerialPort - needed to read the IO values
}

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

<span style="color:Blue private <span style="color:Blue void UpdateValues()
{
<span style="color:Blue byte[] b = <span style="color:Blue new <span style="color:Blue byte[] { (<span style="color:Blue byte)255, (<span style="color:Blue byte)6, (<span style="color:Blue byte)hsbPan.Value, (<span style="color:Blue byte)vsbTilt.Value, (<span style="color:Blue byte)0,
(<span style="color:Blue byte)0, (<span style="color:Blue byte)0, (<span style="color:Blue byte)0, (<span style="color:Blue byte)13, (<span style="color:Blue byte)10 };
WritePacket(b);
}

<span style="color:Blue private <span style="color:Blue void chkEnablePolling_CheckedChanged(<span style="color:Blue 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 = ((<span style="color:Blue float)tmrPolling.Interval / (<span style="color:Blue float)1000).ToString() + <span style="color:#A31515 " sec";
}

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

<span style="color:Blue private <span style="color:Blue void vsbTilt_Scroll(<span style="color:Blue object sender, ScrollEventArgs e)
{
UpdateValues();
}

<span style="color:Blue private <span style="color:Blue void hsbPan_Scroll(<span style="color:Blue object sender, ScrollEventArgs e)
{
UpdateValues();
}
}
[/code]



View the full article
 
Back
Top