Hi, here is the part in my code that is relevant. Might be some syntax errors
as I didn't copy and paste it, buy you'll get the idea. This is basically
what is running in the thread that gets started and stopped from a button on
my GUI.
byte[] b = null;
byte firstByte = 0;
byte secondByte = 0;
int nBytes = 0;
while(true)
{
Port.Send("A"); //Something to start transaction with device.
nBytes = 0;
//Read from port until actually recieve something.
do
{
nBytes = Port.Recv(out b); //Port.Recv returns how many bytes recieved.
} while(nBytes == 0);
firstByte = b[0];
nBytes = 0;
//Wait to recieve seond byte.
do
{
nBytes = Port.Recv(out b);
} while(nBytes == 0);
secondByte = b[0];
//I do this 8 times to get 8 bytes that the device sends successively.
//The device waits for 100 milliseconds after each time it sends a byte
in order
//to make sure that I'm in the do-while loop.
//Then when the user clicks stop, runningThread gets set to stop.
if(runningThread == false)
break;
}
Like I said, if I just let the program run without trying to open up any
other programs, or do anything, it works fine. But when I start doing other
things, one of the bytes gets missed. So for the 8th byte, the program and
the device are locked. My program is waiting to recieve a byte in the last
do-while loop, and the device has looped back and is waiting for the "A" to
be sent to start another cycle. If there was a way to lock 2 statements
together so there can't be a switch in between them then I can send a start
character before each do-while statement to the device to say "I'm ready for
another byte". Then I can drop into the do-while loop and wait for it. If I
can't be assured that both statements will execute one after another with no
switching, there is a possibility that there could be a context switch after
I send the statement to say I'm ready for a byte, then my program will miss a
byte again and be locked waiting for a byte, while the device is locked
waiting for the next "I'm ready" statement. Any thoughts would be
appreciated. Thanks.
Nicholas Paldino said:
JRB,
How are you reading the bytes from the serial port to begin with? To be
honest, I doubt that context switching is the issue here, but rather, how
you access the bytes, and other operations that you might be performing.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi, I'm creating a small C#program that communicates through the serial
port.
I have a separate thread that continuously takes in data from an external
device in a while loop. The problem is that when I try and run another
program, open a menu, or anything, I miss getting bytes. If I set the
threads
priority to above normal it works fine, but the rest of the program slows
down dramatically. Is there any command or way to ensure that a block of
code
will execute together and not get context switched out in the middle? If
there is, then my problem is solved. I can send a command to the external
device saying I'm ready for the next byte, and be assured that I'll get it
if
I can execute those commands together in a block. Any advice would be
appreciated. Thanks.