Sunday, November 1, 2009

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).



2- Numerical System

As we said in previous section, we must represent our data using 0 and 1. We called it binary system.

So everything later must be converted to 0 and 1. Let us see how to represent our numbers (decimal number system), characters and symbols using this system.

For Example, this number (110101):

when we try to extract it to our decimal system, we can say it is:

(2^0*1)+(2^1*0)+(2^2*1)+(2^3*0)+(2^4*1)+(2^5*1) = 1+0+4+0+16+32=53.

For letters and symbols will find some encoding system such as ASCII which represent any single characters in 8 bit, for example it represent number (2) in this way (011 0010), (A) is (100 0001) and (/) is (010 1111).

3- Computer Ports

To start dealing with any external device, you must use one of computer ports to send your signals.

Here are examples of computer ports:

Serial Port

coolcode2

One of the oldest computer ports. It was used to connect external modem to the computer. It is sending 1 byte (8 bit) in each time.

USB Port

coolcode3

The most famous computer port. Most of new devices now are coming with USB port support.

With USB you can connect up to 127 device, you will have hot-swappable property so you will be able to have (Plug-in and Play). And with up to 480 MB/second speed in USB 2.0.

USB comes with only 4 wires, two for VCC/Ground, and the others for sending/receiving data.

Parallel Port

coolcode4

This is the port that we will use it in our examples, because it is the easiest one to deal with for the beginners.

You can find a very good article about it written by Levent Saltuklaroglu: http://www.codeproject.com/KB/cs/csppleds.aspx

Anyway, we will try to explain it one time again here.

Parallel port consists of 25 visible pens:

coolcode5

  • 1, 14, 16 and 17: Control Pins, you can use them for both input and output.
  • 2-9: Data Pins, you can use them to send data outside (output).
  • 10-13 and 15: Status Pins, used for reading data (Input).
  • 18-25: Ground Pins.
To start sending data through Parallel Port, you must have:
      Kernel Mode Driver: such as inpout32.dll for windows users.
      We will select C# as a programming language. You can select any.
      Any device which we can show some data through, the simplest one is LED
Now just take one LED, connect its positive pin to port number 2, and its negative one to any ground, and then open your visual studio any version, import inpout32.dll in this way:

[DllImport("inpout32.dll", EntryPoint="Out32")]


Then declare this function:

public static extern void Output(int adress, int value);

Finally, write some code like this in any button to open first pin:

Output(888, 1);

*** Hint: (888) is the Parallel Port Number, you can make sure from Device Manager->COM Ports->Parallel Port ->Resources-Resources Setting, may you will find it as hexadecimal, just convert it using your windows calculator.

*** Another Hint: Some time your inpout32.dll will gives you an error, use the one in the example.

1 comment:

  1. الله ينور يا باشا .... تسلم ايدك شغل عال العال ....... ننتظر منك المذيد
    أخوك : أحمد الشرقاوى
    elktrostar@yahoo.com

    ReplyDelete