Re: Threading and Serial port issue
- From: "Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT com>
- Date: Tue, 4 Apr 2006 16:32:10 -0700
"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.
"Derek" <derek0402@xxxxxxxxxxx> wrote in message
news:Xns979BF1DED79BAderek0402hotmailcom@xxxxxxxxxxxxxxxx
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[i] = 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);
}
}
.
- Follow-Ups:
- Re: Threading and Serial port issue
- From: Derek
- Re: Threading and Serial port issue
- References:
- Threading and Serial port issue
- From: Derek
- Threading and Serial port issue
- Prev by Date: Threading and Serial port issue
- Next by Date: Re: VS2005 - Console CAB Wizard?
- Previous by thread: Threading and Serial port issue
- Next by thread: Re: Threading and Serial port issue
- Index(es):
Relevant Pages
|