|
Hi,
IMO DataReceived will not necessarily fire for each byte that is received; basically when one or more bytes are available, you will get an event, and they expect you to obtain all available data each time. If you don't try getting it all, the extra data will sit in the input buffer, and your code will lag getting it out (the ReadExisting method is pretty useful for that provided your data is text, yours isn't); in the end you will loose data unless handshaking has been organized, in which case you only will get a potentially big latency (depending on buffer size).
I do have some more remarks:
1. "from 4 times a second to a thousand" is not compatible with 4800Baud, you need at least 9600Baud to be able to communicate the data at 1000 bytes/s.
2. the line display.Text = line; will never execute since InvokeRequired will always be true
3. if your serial port happens to be a USB-to-serial cable, be aware that those do buffer some characters, so they can transmit data in small packets, making better use of USB bandwidth.
4. I trust you did not enable handshaking and left ReceivedBytesThreshold at 1.
And some suggestions:
1. add a StopWatch to your code, and show both its value (up to the milliseconds) and the incoming data in a Console.WriteLine inside your DataReceived handler; that way you will see how the data comes in, and then decide whether or not it gets processed as it should.
Alternatively, add an incrementing counter to DataReceived or setDispText and have it display in another Label (using Invoke again when in DataReceived).
2. add an ErrorReceived handler, maybe something is going wrong and currently gets ignored.
[ADDED]
If you are mainly interested in the most recent value, there are alternative approaches to reading binary bytes:
1. use SerialPort.BytesToRead and Read(byte[],...) to read what is available, then process the last byte only.
2. instead of wiring DataReceived, just launch a thread that performs a synchronous ReadByte() inside an "eternal" loop, and choose an appropriate ReadTimeout value (maybe 100msec is fine). Such polling will provide better "real-time" behavior (never trust Windows to do any of that) but it will be more expensive though.
[/ADDED]
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Have you tried using BeginReadByte() from SerialPort.BaseStream?
Alternatively, try using SerialPort.Read()
Another possibility is tweaking the SerialPort.BaudRate
|
|
|
|
|
You are definitely not the first to encounter problems trying to interface Windows to some real time device or kit. Having heard/had this complaint many times, though not in .NET (as I've never used .NET), I've seen two possible solutions - none of which I suspect you want to hear.
1. Design your program so that it doesn't rely on real time reading of your port. For instance consider: Do you need to update a window with the potentiomer value 9800/sec when the human eye only scans at a lower frequency? (Can't remember what frequency but 18 or 180 scans/sec seems to come to mind).
2. I had a similar problem trying to make a 'software-scope' out of my old PC and ended up buying VToolsD in order to write a Device Driver and buying a blank PCI card so I could solder on some buffers, 555 timer etc and interface directly to the PCI bus - and my device driver. My device driver - much more responsive than my earlier program attempts - then recorded an array of the sample time and value which it passed to the main program.
|
|
|
|
|
hello ,
my application either run automatically on startup or can be manually executed by user.
is there a way to know who triggered the application ? i want to add a note about it in a log file my application creates.
thanks in advance,
avi
|
|
|
|
|
|
Well I know a dirty workaround: give the auto-startup entry a command line argument and use that.
Doesn't give any kind of guarantee, but it will "usually" be accurate and it's easy to implement.
|
|
|
|
|
|
shabya wrote: know who triggered the application ?
Depends on how you define "who". Computer Name or User Name?
I Love KongFu~
|
|
|
|
|
hi,
i ment by who : the computer itself on startup or manual activation from srart->programs -> ...
|
|
|
|
|
shabya wrote: the computer itself on startup or manual activation from srart->programs ->
Well, in this case, Environment.UserName is exactly what you are looking for. Jimmanuel already give you the anwser
I Love KongFu~
|
|
|
|
|
why is it not writing to screen the first line/value in the txt file this code?
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class MainClass
{
public static void Main(string[] args)
{
string inFile;
string line;
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
inFile = Convert.ToString(Console.ReadLine());
StreamReader sr = File.OpenText(inFile);
line = sr.ReadLine();
List<string> lines = new List<string>();
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
foreach (string fi in lines)
{
Console.WriteLine(fi);
}
Console.ReadKey();
sr.Close();
}
}</string></string>
Everything is ok but there aren't first line
|
|
|
|
|
wwwxyz wrote: line = sr.ReadLine(); List lines = new List(); //add all the lines to a list while ((line = sr.ReadLine()) != null)
Because you are reading the fisrt line before the loop and you are not adding it to Lines
Just lines.Add(line); add this before the while loop and after the List lines = new List(); . and it will be fine.
|
|
|
|
|
thanks for reply
Problem is solved but there are two "lines.Add(line);" in code anymore.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class MainClass
{
public static void Main(string[] args)
{
string inFile;
string line;
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
inFile = Convert.ToString(Console.ReadLine());
StreamReader sr = File.OpenText(inFile);
line = sr.ReadLine();
List<string> lines = new List<string>();
lines.Add(line);
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
foreach (string fi in lines)
{
Console.WriteLine(fi);
}
Console.ReadKey();
sr.Close();
}
}
</string></string>
|
|
|
|
|
Or you can simply remove the first line = sr.ReadLine(); (which is outside of the while loop) before List lines = new List(); in the original code. (means the code in your first post). I think that line of code is not needed.
and also, its good to check wether the file exist or not before reading/opening. you can implement try-catch block for that.
|
|
|
|
|
just wonderful
thank you very much
I removed the first line = sr.ReadLine(); which is outside of the while loop.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class MainClass
{
public static void Main(string[] args)
{
string inFile;
string line;
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
inFile = Convert.ToString(Console.ReadLine());
StreamReader sr = File.OpenText(inFile);
List<string> lines = new List<string>();
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
foreach (string fi in lines)
{
Console.WriteLine(fi);
}
Console.ReadKey();
sr.Close();
}
}
</string></string>
|
|
|
|
|
Or simply:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static void Main() {
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
File.ReadAllLines(Console.ReadLine()).ToList().ForEach(Console.WriteLine);
}
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Hi
I am using the Zedgraph addin to plot some curves. I would like to add labels to each curve individually instead of having a legend on the right-hand side. Can anyone shed some light on this?
Thanks,
Alex
|
|
|
|
|
have you consulted Zedgraph genie, I mean, Wikki[^], how about this[^]
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
Hi Yusuf,
Thanks for the tip, I have looked there and there is someone else with the same problem but I don't see a solution. There seems to be a function with pie charts but still no idea for line graphs?
Alex
|
|
|
|
|
Hi
I have written a small program to search my computer for certain files. However, I use Vista which has UAC. Unfortunately there are plenty of places where the application can't reach because of permissions. So I decided to search around how to get past the permission problem. I found the following guide:
http://blogs.msdn.com/shawnfa/archive/2006/04/06/568563.aspx
As expected my app now asks when launched to get highest possible permissions when running. Unfortunately, this still prevents me from accessing the same folders. Any suggestions why that is? Have I lost control of my own system or something since it can't even access my own home directory?
|
|
|
|
|
There are folders on the disk that you cannot get to, no matter what accoutn you use. Your code is just going to have to deal with this eventuality.
If you've already made a manfiest file for your app, requesting admin priv's, then that's about the limit of what you can do.
|
|
|
|
|
Dave Kreskowiak wrote: There are folders on the disk that you cannot get to, no matter what accoutn you use. Your code is just going to have to deal with this eventuality.
besides those belonging to other users, what are they?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
I can't think of a definitive list, but the first that springs to mind is System Volume Information. However, it would be fairly simple to get a list yourself. Just recurse, but print the directory name to the console if you catch an UnauthorisedAccessException. Leave it for a few minutes/hours and you have a list
|
|
|
|
|
Hi Dan,
I don't know them all, I trust C:\System Volume Information and C:\C:\Users\All Users\Start Menu are two of them.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 9:20 AM
|
|
|
|