Re: Threading and Serial port issue



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


.



Relevant Pages

  • Re: Exceptions design problem
    ... in any case, remove the throw in the catch inside the loop, that will solve ... (B downloads a website, so there might occur an exception. ... if one fails the other should nevertheless be started ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: long double versions of functions in gcc under Cygwin
    ... rather than the nearest enclosing one) and a decent exception ... them it doesn't seem like goto usage would be affected ... int typfun() ... Why use a for loop when it is just a while loop in disguise? ...
    (comp.lang.c)
  • Re: CInternetSession
    ... the presence of the Sleepindicates the serious design flaw. ... Sleep() calls around like pixie dust, your design is fundamentally broken and will need to ... If you use Sleepin a loop, your design is probably wrong and needs to be ... The "First Chance Exception" message usually indicates nothing harmful. ...
    (microsoft.public.vc.mfc)
  • Re: Get_Line problem (GNAT bug?)
    ... Now, if the program specs says: "read the lines from input until EOF", then this for me immediately translates into a loop with some exit condition. ... "Read until" - you have a regular end-of-sequence condition here. ... I'm not convinced that exception might be a correct design choice for breaking the loop that reads data from well formatted file. ...
    (comp.lang.ada)
  • SqlCeConnection
    ... I have two module level SqlCeConnection objects, ... error but the Exception object does not contain a message to display. ... private void CloseConnection() ... SqlCeCommand sqlCommand = null; ...
    (microsoft.public.sqlserver.ce)