Re: Threading and Serial port issue

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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



"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: How to pass more than two parameters in the event handler
    ... // Com port & baud rate settings can be changed by opening this ... private void settingsToolStripMenuItem_Click1(object sender, ... EventArgs e, int[,] curveArray) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: UDP deaf in WinService
    ... it binds to the port but totally fails to ... private void Implementation ... UdpClient listener = new UdpClient; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Serial Communication Problem
    ... I have no problems reading from a serial port within a sub-thread. ... the basic loop within the thread, ... > The problem is that function WaitCommEvent never return! ...
    (microsoft.public.windowsxp.embedded)
  • SSA Cabling and Adapter problem
    ... A2 -> Port 16 ... So the A loop is fine, ... The server is an S7A with two I/O drawers, ... There is a seventh adapter in the server, not connected to any disks at all, ...
    (AIX-L)
  • Re: serial port question.
    ... Note that you have left the port in non-blocking mode. ... the termios settings are for raw mode and the VTIME and VMIN ... dependent upon that configuration, and when used as an example ... necessarily must be in a loop. ...
    (comp.os.linux.hardware)