Threading and Serial port issue

D

Derek

I am having a strange problem with reading from a serial port in a
thread. The device on the serial port is an RFID reader which will only
receive data when the user scans a tag.

I am using the Opennetcf Port class to handle the serial comms. The port
opens fine and some initialisation stuff is done to turn on the reader.
I then sit in a loop waiting for data to arrive. I want the application
to wait for the data to arrive before anything else is done. In the loop
it sleeps for 400ms, wakes up to check for data and then goes to sleep
again. The event handler checks incoming data and writes it to a global
variable.

This all works fine but when I create a thread and then call the code
from the thread it fails but only on a device with Compact Framework sp2
(XDA2i). On another device (Axim X5) with CF RTM it works fine and
breaks when I update the CF on that device to SP1 2 or 3.

I need the code to run in a thread to maintain responsiveness of the UI
and allow the user to exit at any time.

Anybody got any idea why it breaks when I update the CF version? I have
used both Thread and ThreadEx and it gives the same results.

Code snippets

private void SetupBoard_Click(object sender, System.EventArgs e)
{
stBr.Text = "Please Wait";
ClearLabels();

//need to check for running threads and kill them
StopRunningThreads();

// create a thread to run the Board setup
Thread setupThread = new Thread(new ThreadStart
(BoardSetup));
setupThread.Start();
}

private void BoardSetup()
{

ReaderControl.Initialize();
setupTagIDs = ReaderControl.Read();

}


//ReaderControl

private static void Read()
{
// clear receivedData
receivedData = "";

// send continuous read command
// check it hasnt been sent already
if(continRead == false)
{
SendCommand("C");
continRead = true;
}
// loop until tag has been read

while (receivedData.Length < 9)
{
Thread.Sleep(400);
}
}
 
P

Paul G. Tobey [eMVP]

"fails" is pretty useless information. If it generates an exception, what
type and what string does the exception provide to indicate the problem is?
At a guess, I'd say that you are fooling with user interface components from
your thread, which is not the UI thread (and those operations are therefore
illegal).

Paul T.
 
D

Derek

Sorry about "fails". What I meant to say was that the event handler for
the data received (Port.CommEvent) never gets fired. No exception is
thrown, it just sits in the loop. The port is open and the device
initializes fine and there should be input The exact same code works
fine when debugging from Visual Studio on the device but when I deploy
it on to the device and run it this eror shows up.

Thanks for any help

Derek

"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam
DOT com> wrote in
 
G

Guest

The serial code already spawns a thread for data receive. It fires an event
when that occurs. I don't quite understand the point of your worker
thread - it's basically polling an already asynchronous item.

-Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top