Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, November 1, 2009

Control Physical World Through Computer (Step by Step) - Part 3/3



Second Example, Driving Remote Control Car


What we will need?

      Remote Control Car.
      Relay.
      Transistor and Buffer (for security purpose only).
      Some wires.

Start

Again we will use the same concept; we will use Relay to control the power from the remote control.

If we try to open and remote, we will find this structure:

Control Physical World Through Computer (Step by Step) - Part 2/3



4- Tools

To start dealing with hardware, it is better if you have some tools to help you, such as

Breadboard:

coolcode6

It will help you to put your wires and pins; it contains connected line (5 pins for example) with the same value so you can read it from many places.


Control Physical World Through Computer (Step by Step) - Part 1/3



Introduction

coolcode1

In this article, we will take the first step inside physical computing world.

Physical Computing, in the broadest sense, means building interactive physical systems by the use of software and hardware that can sense and respond to the analog world. In physical computing, we take the human body as a given, and attempt to design within the limits of its expression. (Wikipedia Definition)

Background

To be able to understand this article, those things must be clear for you:

1- Binary System

As we all know, the only understandable commands for any computer is the (On/Off) power commands which represented as 0 and 1.

And for those two commands, we have only three logical operators. They are (AND, OR and NOT).

And: 1 AND 1 is 1, anything else is equal to Zero.

OR: 0 OR 0 is 0, anything else is equal to One.

Not: Not 1 is Zero, Not 0 is One.

Those three operators are implemented using transistors so they will be able to deal with electricity and give the result.

After that, we have started implementing many functions using those three operators. And collecting them in some electric circles called gates, using those only we are able to build what we call as IC which can perform more complex operations to the data, and for storage also using the concept of flip flops.

You don't need to understand all of this, but at least you must understand the general points of how we can build computer using only (0 and 1).


Saturday, October 31, 2009

StringBuilder



Most of us when we concat any strings, we are using (+) operator (or & in vb.net) this way:


// C#
Label1.Text = Text1.Text + "@hotmail.com";
// Vb.net
Label1.Text = Text1.Text & "@hotmail.com"


But .net framework provides us another way to concat strings, using StringBuilder which is under System.Text name space, this is how to use:


//C#:
System.Text.StringBuilder mail = New System.Text.StringBuilder(Text1.Text);
mail.Append("@hotmail.com");
'VB.net:
Dim mail As New System.Text.StringBuilder(Text1.Text)
mail.Append("@hotmail.com")


What is the difference?

When using (+,&), new object will initialized every time we use this operator. But with StringBuilder, we will use the same object.

From Net Gotachas book, the auther(Venka) assume simple concat operations within loop. And he run the loop using different maximum values of loops. This is snap shoot from the book representing the execution time:






3562.933 second is equal to 59.4 minutes. So will you still use (+,&)?

Finaly, StringBuilder gives us another operations such as Replace, Insert and Remove, you can find all about it with a graphs represent the diferent from this article in CodeProject:
http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspx


Different Between Const and static readonly



Declaring with const or static readonly will give the same result, we will have uneditable variable.

The only one difference is that (const) must take its value at compile time, but (static readonly) will take its value in run time.

so we can get more benefit from static readonly by declare it in static constructor:


class Program
{
public static readonly Test test = new Test();

static void Main(string[] args)
{
test.Name = "Program";
}



Tuesday, June 17, 2008

IComparable Interface.



From The name, this Interface used for comparison. This is the implementation of this interface:

C#:

public interface IComparable
{
int CompareTo(object o);
}

vb.net:

Public Interface IComparable
Function CompareTo(ByVal o As Object) As Integer
End Interface

The result of this function is Integer; it returns 0 in similarity, -1 when the first one is smaller and 1 when larger .

Now we don't need to know who is larger, let’s assume that we have car class, something like that:

C#:

class Car
{

string Name;
int year;
}

vb.net:

Class Car
Private Name As String
Private year As Integer
End Class

Now we need to sort the cars depends on the creation year, our first step it to implement IComparable interface.

Thursday, June 5, 2008

FileSystemWatcher




FileSystemWatcher Class is very benefit when trying to monitor some files and inform user or program when any change occurs, all events are listed in NotifyFilters enum, this is the list of events can be handled by this:

public enum NotifyFilters {
Attributes, CreationTime, DirectoryName, FileName, LastAccess, LastWrite, Security, Size,
}

Now we need to declare functions to be called when some event occurred, this function will run through this delegate:


void MyNotificationHandler(object source, FileSystemEventArgs e)


Only rename event will run through this delegate:

void MyNotificationHandler(object source, RenamedEventArgs e)


Now we will start console application to monitor files, this application will run till user press q, this program will monitor all *.txt files in c:\ folder:


C#:


FileSystemWatcher watcher = new FileSystemWatcher();

// monitor files at:
watcher.Path = @"c:\";

// monitor files when
watcher.NotifyFilter = NotifyFilters.LastAccess NotifyFilters.LastWrite NotifyFilters.FileName NotifyFilters.DirectoryName;

// watch files of type
watcher.Filter = "*.txt";

// watch events:
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);

watcher.EnableRaisingEventys = true;

Console.WriteLine("Press 'q' to quit app.");

while (Console.Read() != 'q') ;

vb.net:


Dim watcher As New FileSystemWatcher()


' monitor files at:
watcher.Path = "c:\"

' monitor files when
watcher.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName

' watch files of type
watcher.Filter = "*.txt"

' watch events:
AddHandler watcher.Created, AddressOf OnChanged
AddHandler watcher.Deleted, AddressOf OnChanged

watcher.EnableRaisingEvents = True

Console.WriteLine("Press 'q' to quit app.")

While Console.Read() <> "q"C

End While



As you seen, we call OnChanged function every event occurs, so we can implement this function to print this event and its time like that:


C#:


static void OnChanged(object source, FileSystemEventArgs e)

{

Console.WriteLine("File Changed, File Path: {0} , Change: {1}, DateTime: {2}", e.FullPath, e.ChangeType,DateTime.Now.ToString());

}

vb.net:


Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

Console.WriteLine("File Changed, File Path: {0} , Change: {1}, DateTime: {2}", e.FullPath, e.ChangeType, DateTime.Now.ToString())

End Sub


Now, this is screen shot of application when delete .txt file and create it again.