|
You can't simply put a Write, then a Read, then a Write for the next command, then a Read, ..., in order an expect it to work. Your design is going to have to be much more flexible, like writing a method to send a command, another method to setup an asynchronous read of the port, another method to handle timeouts. A timer is completely useless to you.
<blockquote class="FQ"><div class="FQA">haolan wrote:</div>Another thing that I am worried about is the COM Port name. It is different for each computer I connect it to..
Is there any way to find out which COM port my device is connected to? </blockquote>
The only way to know is to go through each COM port on the machine and issue some command to it that the device understands and responds to. If the response in what you expect, you found the device. There is no other way to do it.
|
|
|
|
|
Ok..
So translated into code you are saying that I should do something like this when i read:
while(true)
{
DoRead();
if(TimeOutException)
ReadAgain;
else {
if(bytesToRead > 10)
ReadAgain;
else
break;
}
}
I know this isn't a real code, just a little theory..
The 10 bytes to read is because I know that the valid data always will be 10 characters long.
Is this correct understood?
By the way.. I really appreciate your patience in this thread
Sorry if my english seems a little bad sometimes, I am not originally english and it has been a while since I last got lections in it.
|
|
|
|
|
haolan wrote: So translated into code you are saying that I should do something like this when i read:
Kind of, though there are async read methods that do all of this work for you and call you when there is some data to read.
haolan wrote: The 10 bytes to read is because I know that the valid data always will be 10 characters long.
That's not how you do it. Even though your return data may be 10 bytes, you can still get it in several small chunks in multiple reads. Your code has to be able to reassemble the data as it comes in so it can provide a completed message back to the code that's expecting a 10 byte message.
|
|
|
|
|
Ok..
I found this example at MSDN:
public static void Read()
{
while (_continue)
{
try
{
string message = _serialPort.ReadLine();
Console.WriteLine(message);
}
catch (TimeoutException) { }
}
}
And then you say I have to create a method that tries to parse the code, to check if it is a valid message, and if not, then it should run the read() again until the code can be parsed right?
|
|
|
|
|
|
I have an interface:
public interface IBarStream {}
an abstract class:
public abstract class Barable {}
And I'm trying to create a generic class that inherits from Barable, but also constrains the Type passed in to being Barable:
so I have
public class BarStream<t> where T: Barable {} ...but how to specify that BarStream inherits from IBarStream?
Any ideas?
Here's what doesn't work:
public class BarStream<t> where T : Barable : IBarStream {} ...doesn't work
public class BarStream<t> : IBarStream, where T : Barable {} ...doesn't work either
I think if I made IBarStream generic, i.e. IBarStream<t>, then I could get it to work, but I have no reason for IBarStream to be generic here...
Can this be done?
BW
|
|
|
|
|
Off the top of my head you need to do this:
public interface IBarStream{}
public abstract class Barable : IBarStream {}
public class Barstream<T> : Barable where T : Barable, new() {}
|
|
|
|
|
That would work, but doesn't do what I need.
My generic class implements IBarStream AND the generic parameter (T) it uses has to be Barable..it's combining these two things that I can't seem to get down.
One thing I could do is remove the IBarStream interface, make the BarStream class abstract, and then take the IBarStream members and put them in the BarStream as abstract...(I hope I communicated that well enough). But, I would think there should be a way to do it this first way I have it set up (with the interfaces).
BW
|
|
|
|
|
This one compiles (in VS2005):
class BarStream<T> : IBarStream where T : Barable
{
}
Regards,
Tim
|
|
|
|
|
I believe that's it! I had an extra comma in there, and I think that's what was throwing it. Now I've got other problems to deal with
Thanks for the help.
BW
|
|
|
|
|
hi
does any1 know how to put a session value in a url so that when the page is visited it is in a url e.g
www.awebsite.co.uk?Session[Session01]
something like the above
cheers
|
|
|
|
|
Not quite sure what you mean. Like this???
link.NavigationURL = "www.awesite.co.uk?" + Session[Session01];
only two letters away from being an asset
|
|
|
|
|
Hi all,
I am trying to add a RichTextBox to a TabControls TabPage. So I have created a new tab page (or just added one) i.e.
...
tabControlMain.TabPages.Add("Untitled " + tabControlMain.TabPages.Count.ToString());
...
So how can I add a RichTextBox to the newly created tab page??
Thanks in advance
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
Never mind ....
tabControlMain.TabPages[tabControlMain.TabPages.Count-1].Controls.Add(rtext);
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
I am not sure, as I am not near a dev machine, but I think you do this:
TextBox tb = new TextBox();
yourTabControl.TabPages[TabPageIndex].Controls.Add(tb);
"If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
|
Yup, you bet
"If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
Hello all, I got a new question for you today
I've been playing around with the concept of wiping files from a thumbdrive and it seems to be working pretty well.
I have the code below and the file seems to be overwritten as they should.
I have tested Ontrack EasyRecovery and PC Inspector File Recovery and I wasn't able to get any viewable data back.
The only annoying thing is that booth recovery-applications were able to bring back the filename of the overwritten file which I don't want of course.
If I don't rename the file before overwriting it, all that is recovered is a zero-byte file with the correct filename.
If I rename it first, I can recover a file with the correct filename and size but the data is destroyed.
So what I'm asking for is, is there a way to completely delete a file or change the name of the file permanently so it can't be recovered.
private void WipeFile(string filename, int timesToWrite)
{
if(File.Exists(filename))
{
string newFileName = Path.GetDirectoryName(filename) + GetRandomFileName();
File.Move(filename, newFileName);
filename = newFileName;
byte[] dummyBuffer = new byte[new FileInfo(filename).Length];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(dummyBuffer);
for (int timesWritten = 0; timesWritten < timesToWrite; timesWritten++)
{
FileStream inputStream = new FileStream(filename, FileMode.Create);
inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
rng.GetBytes(dummyBuffer);
inputStream.Close();
}
FileStream tmpStream = new FileStream(filename, FileMode.Create);
tmpStream.Close();
File.Delete(filename);
}
}
|
|
|
|
|
Johan Martensson wrote: is there a way to completely delete a file or change the name of the file permanently so it can't be recovered.
Not really. Best you can do is just to fill the file you want to delete with garbage data and rename the file in each iteration of the overwrite. Do this several times over if you are concerned about people recovering the data. Seems like in your code, you are already doing so, there isn't much else you can do.
Just take what you have inside the if block and iterate it over x times. More times you iterate over, the more it is wiped, but also costing more time depending on file size. Here, you are going to have to weight out how many times to do this.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I've run some tests and it does work sometimes if I change the name of the file each time I fill it with junk but it's not worth the time it takes.
Oh well, at least the data-erasing part works.
However, I found that all it took was two lines of code on the for-loop, ones more I find there are easier ways of doing the same thing.
File.WriteAllBytes(filename, dummyBuffer);
rng.GetBytes(dummyBuffer);
|
|
|
|
|
|
Oh, and... in the file eraser I wrote for myself (in C) last June wipes the entire last sector (512 bytes), not just the part that was used by the file. This can help disguise the original length of the file.
Unless you know you'll only wipe small files I wouldn't allocate a buffer the size of the file; why not a fixed buffer size of 512 bytes?
|
|
|
|
|
Wiping the last sector might help, but I'm not sure how I would implement such a thing.
I have only worked with filestreams and I have no idea how to read/write directly to the sectors, so could you point me in a direction of how to do that?
I guess what I'm looking for now is a way to get the exact sectors that a file occupies and write to those.
From what I understand I should probably look at the W32 API functions ReadFile/WriteFile but I havent been able to find any C# examples.
|
|
|
|
|
double sectors = System.Math.Ceiling ( filelength / 512 ) ; ought to work.
Whoops, I mean 512.0
modified on Friday, January 04, 2008 12:17:33 PM
|
|
|
|
|
That gives me the total number of sectors a file occupies, right?
So I should still find a way to figure out which sectors the file occupies and find out how to write some random data do those specific sectors?
|
|
|
|