|
It is a project for our university. We got an processor, and now we have to program an driver for the USB port, and a C# application which can comunicate with it (one byte would be enough).
|
|
|
|
|
Do you mean to say the microprocessor has a USB port and a serial port, and your task is to
actually turn it into a USB-to-serial cable?
if one byte is enough, let the microprocessor be powered through the USB port, and have it
output "Hello World!" as soon as it exits power-up reset!
Writing an actual Windows driver is not a simple task. You will need the right DDK (Driver
Development Kit), which I expect comes with some examples.
Hint: your microprocessor vendor may hold the solution for you; he probably has an elementary
solution for this kind of application.
BTW: which processor is it?
|
|
|
|
|
Hi guys,
I know how to open a file using the OpenFileDailog control or by drag-drop it on the object.
What I'm curious about is, how to associate a file type with my application or right-click on that file and select my application from the open-with list?
any ideas on how to get the file path will be appreciated
|
|
|
|
|
Hi,
1.
to associate a file extension with an app you can:
- use Windows Explorer, menu Tools/Folder Options/File Types
- modify the registry with regedit or write some code (search CodeProject for details)
- or easiest: double-click, and let the Explorer dialogs guide you (make sure to check "Always...")
2.
for Open With, I expect the same possibilities but don't have the experience.
3.
if your file extension is associated to your app, then double-clicking the file will launch
the app and pass the full file path as command line arguments; see the arguments of your static
main() method and/or Environment.GetCommandLineArgs().
4.
Once you have all this in place you will probably want the double-click to reuse your app
if it is already open; if so search CP for "singleton application".
|
|
|
|
|
|
I need to have a sound play in response to some internal event. I know how to play .wav files of any sort, but I don't want to play a .wav file..just send a beep. I think there's some unicode command that will do it, but if anybody could give me a tip on how to do it, that would be, well, helpful.
Thanks.
|
|
|
|
|
|
Write a character 7, or use System.Console.Beep which allows specifying the frequency and duration of the beep.
|
|
|
|
|
In .NET:
using System.Media;<br />
<br />
SystemSounds.Beep.Play()
You can also play any of the other Windows sounds.
|
|
|
|
|
|
Hi,
I'd like to know if casting based on a Type object is possible. Consider the following example:
using System;
namespace ConsoleApplication1
{
class BaseClass
{
}
class ClassA : BaseClass
{
}
class ClassB : BaseClass
{
}
class Program
{
static void Main(string[] args)
{
BaseClass obj1 = new ClassA();
BaseClass obj2 = new ClassB();
Program prog = new Program();
prog.TreatAnything(obj1);
prog.TreatAnything(obj2);
}
public void TreatAnything(BaseClass obj)
{
if (obj is ClassA)
TreatClass((ClassA)obj);
else if (obj is ClassB)
TreatClass((ClassB)obj);
else
{
}
}
private void TreatClass(ClassA obj)
{
}
private void TreatClass(ClassB obj)
{
}
}
}
The TreatAnything function receives a BaseClass object, and is forced to check its type by the is operator before casting it and calling the appropriate function. Now, I know the example looks simplistic, but imagine there are tens of classes derived rom BaseClass. Sure, I could make tens of if clauses and call the correct function each time... What I would really love to do, though, is turn the TreatAnything function into something like this:
public void TreatAnything(BaseClass obj)
{
Type t = obj.GetType();
TreatClass(SomeHypotheticalCastingFunction(t, obj));
}
So the tens of if clauses would be reduced to a small number of statements. Is there any way to do this kind of thing?
In case you want to know what I'm trying to do: I made a socket that parses what is receives and builds an object which is then passed around various methods. I have a Message base class, and then several derived classes. Each derived class represents a specific type of message, whose reception puts in motion very different actions on the server side. No, I don't think it's feasible to make a virtual method Treat() on the Message class and override it on each derived class, as the actions involved require much interaction with other objects the messages know nothing about.
I thank you in advance for any suggestions you might have.
Leonardo Pelisoli
|
|
|
|
|
Hi
You could use Reflection to invoke the appropriate method. something like this:
public class Program
{
public Program()
{
}
public void DoStuff()
{
BaseClass obj1 = new ClassA();
BaseClass obj2 = new ClassB();
ProcessObject(obj1);
ProcessObject(obj2);
}
void ProcessObject(BaseClass obj)
{
Console.WriteLine("BaseClass");
GetType().GetMethod("ProcessObject",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance,null,new Type[]{obj.GetType()},null).Invoke(this,new object[]{obj});
}
void ProcessObject(ClassA obj)
{
Console.WriteLine("ClassA");
}
void ProcessObject(ClassB obj)
{
Console.WriteLine("ClassB");
}
}
public class BaseClass
{
}
public class ClassA:BaseClass
{
}
public class ClassB:BaseClass
{
}
|
|
|
|
|
I'm not sure you need to cast at all in that case, have you tried it without casting? And why have the TreatAnything method at all?
Well, once again I've spoken too soon. But there should still be a simpler way...
modified on Monday, December 10, 2007 7:09:54 PM
|
|
|
|
|
OK, here's another way, using Reflection as m@u suggested, but rather than going to Reflection for each incoming message (which is very costly) I do it once in a static constructor and populate a static dictionary (my latest favourite toy). It's still not as clean as I'd like, but it's fun!
namespace ConsoleApplication1
{
class BaseClass { }
class ClassA : BaseClass { }
class ClassB : BaseClass { }
class Program
{
private static readonly System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodInfo> dic ;
static Program
(
)
{
System.Reflection.ParameterInfo[] pi ;
System.Type pt ;
dic = new System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodInfo>() ;
foreach
(
System.Reflection.MethodInfo mi
in
typeof(Program).GetMethods()
)
{
if ( ( mi.Name == "TreatClass" ) && ( mi.ReturnType == typeof(void) ) )
{
pi = mi.GetParameters() ;
if ( pi.Length == 1 )
{
pt = pi [ 0 ].ParameterType ;
if ( pt.IsSubclassOf ( typeof(BaseClass) ) )
{
dic.Add
(
pt
,
mi
) ;
}
}
}
}
}
static void Main(string[] args)
{
BaseClass obj1 = new ClassA();
BaseClass obj2 = new ClassB();
Program prog = new Program();
prog.TreatAnything(obj1);
prog.TreatAnything(obj2);
}
public void TreatAnything(BaseClass obj)
{
if ( dic.ContainsKey ( obj.GetType() ) )
{
dic [ obj.GetType() ].Invoke ( this , new object[] { obj } ) ;
}
}
public void TreatClass(ClassA obj)
{
System.Console.WriteLine ( "A" ) ;
}
public void TreatClass(ClassB obj)
{
System.Console.WriteLine ( "B" ) ;
}
}
}
|
|
|
|
|
My thanks to you both, PIEBALDconsult and m@u. That's effectively a very elegant solution to my problem.
Best regards,
Leonardo Pelisoli
|
|
|
|
|
I'm a java programmer and am not sure how to do property
This code:
public DateTime StartDate<br />
{<br />
get<br />
{<br />
return this.StartDate;<br />
}<br />
set<br />
{<br />
this.StartDate = value;<br />
}<br />
}
creates a stack overflow, whereby if it's set like this:
projectA.StartDate = new DateTime(2007, 12, 15);
This is only call once to initalize it, however it keeps looping around inside the property and overflows the callstack.
Anyone have any ideas what I'm doing wrong?
|
|
|
|
|
gunner_uk2000 wrote: return this.StartDate;
that is refering itself.
you probably want a member variable (say private DateTime startDate; and have your
get and set manipulate it, rather than refer itself.
|
|
|
|
|
You need a member variable, say:
private DateTime _startDate;
and then set and get the member variable using:
get{ return _startDate; }
set{ _startDate = value; }
what you code does is to keep calling the property's setter.
"On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn't.
"I phoned Camelot and they fobbed me off with some story that -6 is higher - not lower - than -8 but I'm not having it."
-Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.
|
|
|
|
|
I think it should be this
private DateTime _startDate;<br />
<br />
public DateTime StartDate<br />
{<br />
get<br />
{<br />
return _startDate;<br />
}<br />
set<br />
{<br />
_startDate = value;<br />
}<br />
}
The property works as a public getter/setter, but needs to assign the value to an actual (usually private) variable.
|
|
|
|
|
I am getting a mouse click event on a User Control object, which has not been clicked or manipulated in any way, immediately after another class object, base on a User Control, is dropped in the Panel where these objects have been added.
I am using the standard Drag and Drop events to move an object around within the same Panel. I initiate the "DoDragDrop(...)" from a Mouse_Down event in the User Control object that I am dragging.
Does anyone know from where this mouse click event might originates.
|
|
|
|
|
hi to all, i want to do a job per 40 milisecond but i can't !;
timer component can not support this resolution ,
can i access to this resolution in other way?
|
|
|
|
|
you can go for waitable timers
|
|
|
|
|
may I suggest you read my timers article.
|
|
|
|
|
Luc Pattyn wrote: may I suggest you read my timers article.
Shameless plugging going on?
Seriously though, I was going to point him at your article anyway.
|
|
|
|
|
Pete O'Hanlon wrote: Shameless plugging going on?
Of course. That's what articles are for, I would say.
|
|
|
|