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);
}
}
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);
}
}