|
Thank you..
krishna
|
|
|
|
|
I am trying to develop a very simple wireless sensor node simulator using C#. I have figured out how to "divide" the simulation time into different "states" of the sensor node but I am currently having some problems on how to properly implement a global simulation time for all the sensor nodes.
Here's what I did so far but I am sure there is a much better way to do this.
I made a NODE class which contains a Run() method that has a for loop executed to simulate time. But the obvious problem is, if I duplicate this NODE class to make other nodes (5-10 other nodes), there's a tendency that they will not be synchronized (especially when they communicate with each other).
public void Run()
{
for (int i = 0; i < 1000; i++ )
{
if (sleep)
{
if (csleep < timeToSleep)
{
Sleep(i);
csleep++;
}
else
{
sleep = false;
csleep = 0;
wakeup = true;
}
}
if (wakeup)
{
if (cwakeup < timeToWakeUp)
{
WakeUp(i);
cwakeup++;
}
else
{
wakeup = false;
cwakeup = 0;
listen = true;
}
}
if (listen)
{
...same as above code...
}
if (send)
{
...same as above code...
}
if (receive)
{
...same as above code...
}
}
Console.WriteLine("END SIMULATION");
}
So, when I execute the nodes in the main simulation class, it looks like this:
class Program
{
static void Main(string[] args)
{
Node node1 = new Node();
Node node2 = new Node();
node1.Run();
node2.Run();
}
}
The above codes are obviously incorrect since the simulation time is placed inside each node object. What I am trying to do is this:
I want to treat all nodes as if they are threads communicating with each other or could sometimes just independently "exist" in the simulation. But how do I make it that all of these nodes could have one single basis for the simulation time?
Simulation loop
{
Node 1 executed;
Node 2 executed;
}
|
|
|
|
|
Unless NODE has some sort of interrupt mechanism (Sleep; I/O; etc.), once one is “Run”, it will hog the (one) thread indefinitely.
I would suggest putting the “timing” code in a separate class (or in static methods of the NODE base class), and use a “Timer” instead of a “for” loop for invoking “sleep”, “awake”, etc. states.
Prior to creating and “running” a NODE, start the “timing” object / static methods.
As a NODE is created, it should subscribe to (state-change) events in the timing object that call the “Sleep”, “Awake”, etc. event handlers in a subscribing NODE.
In this way, the timing object will call the “Sleep”, “Awake”, etc. methods in each (subscribing) NODE with each “state” change (i.e. x number of ticks of the Timer).
This of course assumes you want all NODEs to be “asleep” or “awake”, etc. at the same time.
You will need some sort of (message) queuing mechanism to actually “communicate” between these NODEs; since if they are all “sending” at the same time (state-wise), they can also not be “receiving” at the same time.
|
|
|
|
|
Thanks Gerry! I will try your suggestions
|
|
|
|
|
You're welcome. Glad I could help.
|
|
|
|
|
Hello experts,
I'm quite new to dotnet programming and so on, so perhaps my question is very easy to answer.
I'm sending data from a µC to PC over USB. This is byte wise and when I want so send a float, I did it like in this C++ test code and it worked:
int _tmain(int argc, _TCHAR* argv[])
{
char test[4] = {25,4,158,63};
float *blubb = (float*)&test[0];
printf("%f",*blubb);
return 0;
}
Now I want to do the same in C# and it doesn't work. Here is my attempt:
namespace csharpfloattest
{
class Program
{
unsafe static void Main(string[] args)
{
byte[] test = { 25, 4, 158, 63 };
fixed (float* p = (float*)test)
{
Console.WriteLine(*p);
}
}
}
}
Compiler gives out this error:
Cannot convert type 'byte[]' to 'float*'
Perhaps someone can give me an advice?
Thanks for your help!!
Florian
|
|
|
|
|
I would suggest using BitConverter.ToSingle to convert bytes to a float, and BitConverter.GetBytes to convert a float to bytes.
Depending on the architecture of the sender and receiver (little versus big Endian), you may have to consider byte ordering.
|
|
|
|
|
Thanks a lot! Didn't know this method...
|
|
|
|
|
In general, c# (or any managed .NET language) does not have pointers.
(Yes, there's unsafe and IntPtr but that is not necessary to accomplish what you've described.)
The way to do this kind of thing in c# is to use the class System.BitConverter .
So to convert a byte[] into a float is:
class Program
{
static void Main(string[] args)
{
byte[] test = { 25, 4, 158, 63 };
float p = BitConverter.ToSingle(test, 0);
Console.WriteLine(p);
}
}
Going the other way, from float to byte[] , you would use BitConverter.GetBytes(value);
|
|
|
|
|
You can't cast an array to a pointer (not even a fixed-size array). But you can take the address of the 0th item of the array, and then attempt to cast that:
fixed (float* p = (float*)&test[0])
{
}
But that turns out to be explicitly forbidden: "The right hand side of a fixed statement assignment may not be a cast expression".
So there's not much choice, you'd have to do something like this:
fixed (byte* temp = data)
{
float* p = (float*)temp;
}
There was the alternative of not using unsafe code (not directly anyway, BitConverter.ToSingle is implemented using unsafe code itself) in this case, so this is just for completeness really.
|
|
|
|
|
i got a routine which said that it will highlight word in word document but it is giving error "Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))"
when this below line execute then exception occur and error as above occur.
range.find.highlight()
here is my full code
private static void HighlightText()
{
object fileName = "D:\\CVArchievePath\\C0000000001.doc";
object textToFind = "test1";
object readOnly = true;
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = Type.Missing;
try
{
doc = word.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing);
doc.Activate();
object matchPhrase = false;
object matchCase = false;
object matchPrefix = false;
object matchSuffix = false;
object matchWholeWord = false;
object matchWildcards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object matchByte = false;
object ignoreSpace = false;
object ignorePunct = false;
object highlightedColor = Word.WdColor.wdColorGreen;
object textColor = Word.WdColor.wdColorLightOrange;
object missingp = false;
Word.Range range = doc.Range();
bool highlighted = range.Find.HitHighlight(ref textToFind,
ref highlightedColor,
ref textColor,
ref matchCase,
ref matchWholeWord,
ref matchPrefix,
ref matchSuffix,
ref matchPhrase,
ref matchWildcards,
ref matchSoundsLike,
ref matchAllWordForms,
ref matchByte,
ref missingp,
ref missingp,
ref missingp,
ref missingp,
ref missingp,
ref ignoreSpace,
ref ignorePunct,
ref missingp);
System.Diagnostics.Process.Start(fileName.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Error : " + ex.Message);
}
}
tbhattacharjee
modified 8-Mar-13 7:45am.
|
|
|
|
|
You are using the boolean type false for a number of your parameters, but the ducumentation[^] defines these as ref object types. Try changing your code.
Use the best guess
|
|
|
|
|
i modify my code. now send
object missingp = false; ref missingp instead of harcode code false but still no luck. can u guide me please. thanks
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: can u guide me please Sorry but this method is not available in my versions of Word (2003 and 2007) so I am unable to test it. I can only suggest that you try one of the Microsoft forums.
Use the best guess
|
|
|
|
|
Is that the actual code? I surprised that it compiles as the HitHighlight methods has ref parameters.
|
|
|
|
|
i modify my code. now send
object missingp = false; ref missingp instead of harcode code false but still no luck. can u guide me please. thanks
tbhattacharjee
|
|
|
|
|
The problem with many of these MSWord methods is that they have many parameters and it not easy to know which combinations are valid. I would suggest simplifying the HitHighlight method call and just specify the text to find, as that is the only required parameter. Set all others to Type.Missing. If that works as expected then reintroduce the other parameters, possibly one at a time.
There are warnings in the documentation that some parameters "may not be available", e.g. HanjaPhoneticHangul is available only if you have support for Korean languages. This may mean that it must be set to Type.Missing and not true or false. The documentation is not clear about this.
http://msdn.microsoft.com/en-us/library/office/bb226067(v=office.12).aspx[^]
http://msdn.microsoft.com/en-jm/library/microsoft.office.interop.word.find.hithighlight.aspx[^]
Alan.
|
|
|
|
|
In a C# 2008 windows application, I planning on locating files that I need to find by utilizing the following code:
var RFiles = from path in Directory.EnumerateFiles(filesaveLocation, "*.*",
SearchOption.AllDirectories)
let extension = Path.GetExtension(path)
where extension == ".pdf" || extension == ".xlsx" || extension == ".xls"
select path;
However once I find each selected file, I need to know the exact location of where each specified file was located. I need to be able to store the exact directory structure location in a sql server 2008 r2 database.
Basically the code statement would be similar to tell me exactly where each seelcted file is located at.
Thus can you me in code and/or explain to me how to accomplish my goal?
|
|
|
|
|
Have you checked the properties of that Directory object for anything useful, like the directory path it's representing??
|
|
|
|
|
In this case, you can switch to use the DirectoryInfo class to do this:
DirectoryInfo filesaveDir = new DirectoryInfo(filesaveLocation);
var RFiles = from fi in filesaveDir.EnumerateFiles("*.*", SearchOption.AllDirectories)
let extension = fi.Extension
where extension == ".pdf" || extension == ".xlsx" || extension == ".xls"
select fi.FullName;
|
|
|
|
|
How to use corrupted sql server database into oracle using c#
|
|
|
|
|
why do you want to move a corrupt database into Oracle? surely you should fix the SQL Database first
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
|
Add the .MDF file as a BLOB.
|
|
|
|
|
Dear All,
I am using Gridfs to store all my images in mongodb. How do i retrieve the images from mongodb using Gridfs. And with which C# control i can display the mondodb images. Please sugget any ideas.
|
|
|
|
|