简体   繁体   中英

serialPort.DataReceived running in thread not working

I'm working in a thread to check the GPRS connection in CompactFramework .

The idea of the thread is simple: If the program isn't connected then I run the code to connect (this code is giving me errors), but if the connection is OK then I recheck again in 60 seconds and so on.

Now, focusing in connection code. The following code check if it's connected or not, if it isn't then I subscribe to DataReceive event.

void initFormText()
{
    if (isThereConnect()) //true if it is connected
    {
       //enable timer to recheck if it's connected
    }
    else //it isn't connected
    {  

        serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
        if (serialPort1.IsOpen)
        {
            serialPort1.Close();
        }
        serialPort1.Open();   

        timerStep.Enabled = true;
    }    
}  

Now comes the issue, in the serialPort1_DataReceived I check the data and set a variable which is tested by the timerStep and it make some steps.

The problem occurs in the DataReceived event, the thing is that when I run the following code outside of a thread it works fine, it does all the job and make the connection, but in the thread it doesn't work. I test this adding some MessageBox and I realize that the ones inside the DataReceive never appear.

void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    byte[] data = new byte[1024];
    int n = serialPort1.Read(data, 0, data.Length);
    string rec = Encoding.GetEncoding("windows-1252").GetString(data, 0, n);

    if (string.IsNullOrEmpty(rec))
    {
        return;
    }

    if (rec.Contains("AT+CIMI") && rec.Contains("OK"))
    {
        MessageBox.Show("serialPort 1");
        currState = 1;
    }
    else if (rec.Contains("READY"))
    {
        MessageBox.Show("serialPort 11");
        currState = 1;
    }
    else if (rec.Contains("0,1") || rec.Contains("0,5"))
    {
        MessageBox.Show("serialPort 2");
        currState = 2;
    }
}

So by some reason the serialPort isn't receiving anything and I can't figure it out why. The fact that it works outside the thread but not in the thread is frustrating me.

I appreciate any help. Thanks in advanced!

The event must run in the same thread (I suppose UI thread) where you have already declared serialPort1. You can execute the code from the serialPort1_DataReceived event in different thread. That thread should be started by serialPort1_DataReceived event handler. The problem is that CompactFramework doesn't have ParameterisedThreadStart so you can not effectively pass received data to the thread. You will need to set some global field using delegates.

Yes, but I think that your thread finishes before event is fired. You should create your Form in a following manner, please note that this is code for desktop but simulates what is available in CompactFramework since I don't have it installed here. First Form1 is main form and it starts thread in which is the Form2. The Form2 has a button and Click EventHandler that is working, but you need to show your Form2 with Application.Run(). Here is the sample code:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(new ThreadStart(ThreadMethod));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }
    void ThreadMethod()
    {
            Form2 f = new Form2();
            Application.Run(f);
    }
}  

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Something");
    }
}

Hope it will work this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM