Send Data During Serial Port Data received Event
<p>I am using COM4 Serial Port to both send and receive data. I have a demand request that comes in - I verify that the request is of mydatastring = myrequeststring - this works fine. I am trying to reply data through the com port during this event handler but of course Access is denied. How can I accomplish sending of data after receiving my request..??</p>

<pre><code>    void scScale_OnReceiving(object sender, DataStreamEventArgs e)
    {
        if( e.Response == myrequeststring )
        {
            scScale.Transmit(this.data);
        }             
    }
</code></pre>

<p><strong>EDIT</strong></p>

<p>relevant parts of scScale - it is a SerialPort Client class - basically encapsulates the SerialPort details.</p>

<p>Relevant code:</p>

<pre><code>public bool OpenConn()
{
    try
    {
        if (_serialPort == null)
            _serialPort = new SerialPort(_port, _baudRate, Parity.None);

        if (!_serialPort.IsOpen)
        {
            _serialPort.ReadTimeout = -1;
            _serialPort.WriteTimeout = -1;

            _serialPort.Open();

            if (_serialPort.IsOpen)
            {
                serThread.Start(); /*Start The Communication Thread*/
                IsOpen = true;
            }
            else
            {
                IsOpen = false;
            }

        }
    }
    catch (Exception ex)
    {
        return false;
    }

    return true;
}


public void Transmit(string packet)
{
    _serialPort.Write(packet); // 0, packet.Length);
}

public void Transmit(byte[] packet)
{
    _serialPort.Write(packet, 0, packet.Length);
}
public int Receive(byte[] bytes, int offset, int count)
{
    int readBytes = 0;

    if (count &gt; 0)
    {
        readBytes = _serialPort.Read(bytes, offset, count);
    }

    return readBytes;
}
    private void OnSerialReceiving(byte[] res)
    {
        if (OnReceiving != null)
        {
            OnReceiving(this, new DataStreamEventArgs(res));
        }
    }
</code></pre>

<p>SCSCALE is this code at link..
<a href="https://roofman.wordpress.com/2012/09/13/fast-serial-communication-for-c-real-time-applications/" rel="nofollow">https://roofman.wordpress.com/2012/09/13/fast-serial-communication-for-c-real-time-applications/</a></p>