Peter Boldt
Peter Boldt

Reputation: 73

Serial port reading loop in background

Have an STM32 device, that send over Serial port an AES encrypted Hex data any 2 seconds. All messages are finished with newline. My program (with User Interface, not a console one) is able to read from serial and to decrypt the data, even in loop still working good, but the problem is that in loop i can't use the disconnect function, so my program stay in "infinite loop". Here is a code:

public void button1_Click(object sender, EventArgs e)
{
    try 
    {
        serialPort1.PortName = cBoxComPort.Text;
        serialPort1.BaudRate = 9600;
        serialPort1.DtrEnable = true;
        serialPort1.ReadTimeout = 5000;
        serialPort1.WriteTimeout = 500;
        serialPort1.Open();
        lblStatusCom.Text = "Connected";
        lblMessage.Text = "I am on!";

        while (serialPort1.IsOpen)
        {
            string mgs1 = serialPort1.ReadLine();
            byte[] key = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
            byte[] enc = StringToByteArray(mgs1);
            byte[] dec = Decrypt(enc, key);
            lblMessage.Text = getString(dec);
        }
    }
    catch(Exception err)
    {
        MessageBox.Show(err.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //lblStatusCom.Text = "Disconnected";
    }
}

How can i "put to background" this loop process, so that a user can click to disconnect, if he wish to quit the reading?

Upvotes: 2

Views: 1541

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

Here is a version that would use the DataReceived event:

With the button click you would open the port and register the event

public void button1_Click(object sender, EventArgs e)
{
    try 
    {
        serialPort1.PortName = cBoxComPort.Text;
        serialPort1.BaudRate = 9600;
        serialPort1.DtrEnable = true;
        serialPort1.ReadTimeout = 5000;
        serialPort1.WriteTimeout = 500;
        serialPort1.Open();
        lblStatusCom.Text = "Connected";
        lblMessage.Text = "I am on!";

        serialPort1.DataReceived += portDataReceived;

    }
    catch(Exception err)
    {
        MessageBox.Show(err.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //lblStatusCom.Text = "Disconnected";
    }
}

private void portDataReceived(object sender, EventArgs args)
{
    SerialPort port = sender as SerialPort;

    if (port == null)
    {
        return;
    }

    string mgs1 = port.ReadLine();
    byte[] key = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
    byte[] enc = StringToByteArray(mgs1);
    byte[] dec = Decrypt(enc, key);
    if (lblMessage.InvokeRequired)
    {
        lblMessage.Invoke(new Action(()=> lblMessage.Text = getString(dec)));
    }
    else
    {
        lblMessage.Text = getString(dec);
    }
}

Upvotes: 2

Related Questions