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. 5 2012
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]
- mv*@spam.guard.caspershouse.com
"JRB" <JR*@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.com... 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.
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 [.NET/C# MVP]" wrote: 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] - mv*@spam.guard.caspershouse.com
"JRB" <JR*@discussions.microsoft.com> wrote in message news:8A**********************************@microsof t.com... 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.
Hi JRB:
I dont know what the Port class is, but looking at the code I'd guess
it possible for it to return all 8 bytes on the first call to Recv,
but the code assumes it will only return 1.
--
Scott http://www.OdeToCode.com/blogs/scott/
On Mon, 1 Nov 2004 17:44:02 -0800, JRB <JR*@discussions.microsoft.com>
wrote: 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 [.NET/C# MVP]" wrote:
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] - mv*@spam.guard.caspershouse.com
"JRB" <JR*@discussions.microsoft.com> wrote in message news:8A**********************************@microsof t.com... > 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.
Hi, I'm not sure what you mean by port class. I'm reading in from a standard
serial port. RS-232. I haven't tried reading in all 8 bytes. I wasn't sure
how big the Rx buffer is, so thought it would be safer to read in one by one.
How many bytes will Rx hold before overflow? Thanks.
JRB
"Scott Allen" wrote: Hi JRB:
I dont know what the Port class is, but looking at the code I'd guess it possible for it to return all 8 bytes on the first call to Recv, but the code assumes it will only return 1.
-- Scott http://www.OdeToCode.com/blogs/scott/
On Mon, 1 Nov 2004 17:44:02 -0800, JRB <JR*@discussions.microsoft.com> wrote:
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 [.NET/C# MVP]" wrote:
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] - mv*@spam.guard.caspershouse.com
"JRB" <JR*@discussions.microsoft.com> wrote in message news:8A**********************************@microsof t.com... > 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.
Hi JRB:
I'm talking about this line of code:
nBytes = Port.Recv(out b);
The comment next to the line of code says "Port.Recv returns how many
bytes recieved.", but that return value (nBytes) is never checked. I'd
bet money you occasionaly get back more than a single byte.
--
Scott http://www.OdeToCode.com/blogs/scott/
On Tue, 2 Nov 2004 16:43:07 -0800, JRB <JR*@discussions.microsoft.com>
wrote: Hi, I'm not sure what you mean by port class. I'm reading in from a standard serial port. RS-232. I haven't tried reading in all 8 bytes. I wasn't sure how big the Rx buffer is, so thought it would be safer to read in one by one. How many bytes will Rx hold before overflow? Thanks.
JRB
"Scott Allen" wrote:
Hi JRB:
I dont know what the Port class is, but looking at the code I'd guess it possible for it to return all 8 bytes on the first call to Recv, but the code assumes it will only return 1.
-- Scott http://www.OdeToCode.com/blogs/scott/
On Mon, 1 Nov 2004 17:44:02 -0800, JRB <JR*@discussions.microsoft.com> wrote:
>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 [.NET/C# MVP]" wrote: > >> 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] >> - mv*@spam.guard.caspershouse.com >> >> "JRB" <JR*@discussions.microsoft.com> wrote in message >> news:8A**********************************@microsof t.com... >> > 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. >> >> >>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dennis Wheeler |
last post by:
I'm trying to find a commandline solution for switching projects.
Currently I have to modify the IIS virtual directory path to the
source files, and then open the solution file in .Net to be...
|
by: Lalit |
last post by:
Hi Friends,
I have developed a Windows service. Now i need icon for this service in
systray and context menu fo this icon. Can i do this?
With regards,
Lalit
|
by: AllenL |
last post by:
Ever since I've been using objects with VB I've instantiated the
business object from the form; the business object then
creates/destroys data access layer objects as needed. The business
object...
|
by: Michael Raczynski |
last post by:
Hey guys,
Apologies if this is in the wrong thread, whenever I search for <% or
"switching context" I get no results.
What I am trying to do is set the value of a html text box to a server...
|
by: Michael M. |
last post by:
In Perl, it was:
## Example: "Abc | def | ghi | jkl"
## -"Abc ghi jkl"
## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
pipe).
$na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;...
|
by: Frank Swarbrick |
last post by:
I had asked a question a few weeks ago about having problems at times
accessing DB2 Express-C 9.1, and getting "SQL1032N No start database manager
command was issued. SQLSTATE=57019" even when the...
|
by: grvsinghal |
last post by:
I am working on solaris OS. The I have eight dual core processors, and I am using pthreads. most of my threads will run for 5-10 hours, so I want to avoid switching of thread between processors so...
|
by: Ryan Liu |
last post by:
Hi,
I have a client/server application, using one thread/client approach.
I see very high context switch/sec. What are the ways to reduce it? Each
thread sleep longer in its endless loop if...
|
by: adlloyd |
last post by:
Hi all,
I've got an application that's written in C++ making use of MFC (VS6). Its purpose is to process SMS messages received from a GSM modem connected via a serial port (USB connection). The...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |