Friday, March 23, 2012

Writing Web Services


Web Services, unlike other ASP.NET files, have .asmx extensions. They are essentially all code with little or no user interface.
Each Web unlike Service can be discovered from a remote server via a .DISCO file, which is on the server containing the Web Service. This .DISCO has all the information necessary to enable the remote server to find out about available Web Services and how to use them.
Web Services make available a lot of information about themselves. They use the service description language (SDL) to do so. Although SDL is what .NET is using now, it is moving to Service Control Language (SCL) very soon. This is a more expressive version of the SDL. It will probably replace SDL by the final release of .NET.
Now I would like to show you examples of what I have been talking about. The first thing I would like you to take a look at is the contents of a discovery file as follows:

<?xml version="1.0" ?>

<dynamicDiscovery

  xmlns="urn:schemas-dynamicdiscovery:disco.2000-03-17">

<exclude path="_vti_cnf" />

</dynamicDiscovery>
The next thing you should look at, just so you can familiarize yourself with it, is an SDL file. The following is a simple SDL file from a Web Service named HelloWorld.


<?xml version="1.0"?>

<serviceDescription>

  <soap >

    <service>

      <addresses>

        <address uri="http://localhost//HelloWorld.asmx"/>

      </addresses>

      <requestResponse name="SayHelloWorld" soapAction= "http://tempuri.org/

graphics/ccc.gifSayHelloWorld">

        <request ref="s0:SayHelloWorld"/>

        <response ref="s0:SayHelloWorldResult"/>

      </requestResponse>

    </service>

  </soap>

</serviceDescription>
Finally, I would like to show you the SOAP payload that a simple Web Service might return. In the following example the SOAP envelope contains a credit card number and a credit card PIN.

<?xml version="1.0"?>

    <Envelope>

        <Body>

           <GetCCInfo>

            <CCNum>12345</CCNum>

        <CCPin>1234</CCPin>

       </GetCCInfo>

    </Body>

</Envelope>
Now, I am going to walk through the creation of a simple Web Service. Start by running Visual Studio.NET. Go to the file menu, select New Project, and set up for a C# Web Service project.
VisualStudio.NET creates the project and all its support files. If you open the project's default .asmx (which is usually named WebServicex.asmx) file and then right-click on it to view the code, you can see the boilerplate code for the Web Service. In this code you can find a HelloWorld() method. The HelloWorld() method gives you an idea of how to create methods for Web Services.
The HelloWorld() method is actually remarked out when the code is first created. If you unremarked it out you could make the following observations. First, there is a Web method attribute above it. This is all that is necessary to make any method in a Web Service implement SOAP to communicate across the Internet. The rest of the method looks pretty standard. It is declared as public. It might or might not have a return type and it might or might not have parameters.
I am going to create a Web Service for this discussion that will have a method that multiplies two numbers. When I create the project I will open up the multiply.asmx and find the HelloWorld() method. Next I will remove the HelloWorld() method and create my own method called multiply.
The multiply method takes two parameters and they are both integers. I named the first parameter nFirst and the second nSecond. The multiply method returns an integer, and this integer will be the result of multiplying the first and second parameters. You can see this Web method (marked with a WebMethod attribute) in the following code:

[WebMethod]

public int Multiply(int nFirst, int nSecond)

{

    return( nFirst * nSecond );

}
One of the problems with writing Web Services is that there is no user interface—this means you can't run them and see anything. Visual Studio.NET takes care of this for you, and gives you a way to actually invoke Web Services. By right-clicking on the default .asmx file you can run the Web Service in a browser. Before doing this, though, you must build it. What happens when the browser first runs is that it queries the Web Service and finds out what the parameters are. So when it examines my multiply Web Service it finds that the Web Service takes two parameters, both of them integers, and returns an integer. Visual Studio .NET actually creates a user interface for you in HTML so that you can type in the parameters and then invoke your Web Service.
When you invoke the service what you get back is XML. This XML is the result of invoking the Web Service. For instance, if my first integer were 5 and my second integer were 6, the resulting answer would be 30. 

Wednesday, March 21, 2012

WEB SERVICES


What Is a Web Service?
What would you use a Web Service for? Well, that is a fair enough question. Web Services are used to program the Web. Web Services represents a way to abstract a Universal Resource Identifier (URI) and access it across the Web on another server. If this doesn't seem clear, keep reading and you will understand it as I explain and talk about Web Services.
The Simple Object Application Protocol (SOAP) is what Web Services use to communicate across the Internet.
What Is SOAP?
SOAP is used to send messages across the wire. One computer can use SOAP to send a block of data—say, for instance, a billing record—to another computer. When using SOAP, both computers understand the protocol, and can send and receive the data.
SOAP (Simple Object Access Protocol) starts with simple, and that's one of the key things it delivers. If you've ever worked with DCOM to communicate to remote servers, you know that DCOM is anything but simple—as a matter of fact, it's pretty ugly.
SOAP is based on XML. The data that is carried in a SOAP package is always represented by XML. If you plan to do much with SOAP, you may want to take a look at the XML specification or do some reading and understand the basics of XML.
SOAP works with any operating system because it doesn't rely in any way on operating systems. It's a protocol on its own merit that dictates no hardware, operating system, or language specifications.
SOAP is also built on standards, and therefore can easily be implemented by anyone. First, it relies on HTTP to send data across the wire. Second, it relies on XML to represent the data. And third, it relies on the SOAP specification so that every implementer will be in compliance and able to communicate with all other implementers. You can get the SOAP specification at http://msdn.microsoft.com/workshop/xml/general/soapspec.asp.
SOAP Packages
SOAP data is sent in a well-organized manner as it goes across the Internet. This section shows you the hierarchy of SOAP packages.
Because all SOAP packages are sent within an HTTP message, the first part of a SOAP message is the HTTP header. Information in the section usually contains the domain name, the keyword POST, the Content-Type specification, and other optional information.
After the HTTP header comes the SOAP envelope. This encompasses the entire SOAP message. It's kind of like what's inside the box that arrives at your doorstep. It's all the important stuff you need (the actual data). The address label on the front of the box (the HTTP header) can almost always be discarded.
The SOAP envelope starts off with a SOAP header. This is different than the HTTP header. It doesn't contain information about the routing of the data (such as the domain name and the size of the content), but contains information related to the data, such as a transaction ID.
Why Is SOAP Important for Web Services?
Using SOAP for Web Services makes Web Services easy to use and powerful. SOAP enables Web Services to communicate across the Internet without having to worry about what operating system they are talking to and what language the other site is using.
If you have every programmed with VB5 or VB6 you will remember how easy it was to use an object that was registered or installed on a system. It was a matter of declaring the object and "newing" it as the following code shows:

MyObject Obj = new MyObject

Obj.DoSomething()
As you can see, after I created my object it was a simple matter to make method calls on that object. Wouldn't it be nice to have a programming model this simple for calling methods across the Web? Of course—and that's what Web Services offer. The same type of simple object programming model that VB offered is now available using Web Services. You simply create a service object and call methods on it, as follows:

MyService Ser = new MyService();

Ser.DoSomething();
As I said at the beginning of this chapter, Web Services might change how you develop and build Web applications. After seeing this new simplified—yet powerful—programming model, you are going tohave to agree that Web Services open things up for you unlike any other technology that has been introduced in recent years.
Why Are Web Services Useful?
Imagine you have some sort of an e-commerce Web site, and you have a partner that provides you with content. Or you might provide your partner with some sort of content or you have some other interaction. Web Services give you an easy way to integrate with partners. I will give some examples of this shortly.
Web Services are very easy to program. They are also based on standards. The fundamental standard on which they are based is HTTP; all the SOAP information uses this protocol as its basis. Then, the data is contained within a SOAP envelope, which is also a standard that has been submitted to the WC3. Within the SOAP envelope is XML. And XML is the standard that is becoming very popular and widely adopted for Internet data representation.
Another reason you would want to implement Web Services is because they can be easily upgraded on one server without affecting the applications that call them. If server A contains a Web Service, I can easily change it as long as I don't change the calling and return parameters. And I can do this without any knowledge or effect on server B, which calls methods in my Web Service on server A.
Real-World Scenarios for Using Web Services
I would like to talk for a minute about when you would use a Web Service. Although I can think of many examples, I'll cover just a few here. I'm sure that with a little effort you can come up with your own.
Suppose I have a Web site on which I want to serve up news content to users. The news content comes from a server located somewhere else on the Internet. A Web Service provides the easiest programming model for a Web application to retrieve and consume from across the Web.
Although I have carried out tasks before that pull in data and content from other Web servers, it hasn't always been easy. It has taken me several days to do this, then several more days to debug the material I received, and making changes was sometimes difficult. In the end, this process might have taken me anywhere from four to five days of programming. With a Web Service I can consume content from a remote server in about two hours.
Credit card authorization of course comes to mind. A Web Service makes the ideal method of doing credit card authorization. From one server you can easily hit another server and authorize a credit card with just several lines of programming code.
Centralization of user information is another option that Web Services provide. You might have a user who has a set of information that is associated with that user. A Web Service can enable multiple Web Services to retrieve and use information about a given user. Of course this assumes that there is proper security clearance and the proper authorization credentials.
Web Services enable you to create a fantastic ad server. Imagine being able to provide user information, such as a user's preferences, and an ad server serves up a targeted ad toward that particular user. Say, for instance, that I am on a Web site and somehow I have let it be known that I like golf. As a matter of fact, I may have indicated on the Web site that I am looking for a new set of clubs. Rather than just rotate through a bunch of ads, a Web application can call a Web Service and let the Web Service know that the user likes or has an interest in golf, and the Web Service serves up a targeted ad with golf clubs or golf accessories.

Monday, March 12, 2012

DC Motor interfacing with L293D H-Bridge


L293D is a dual H-Bridge motor driver, So with one IC we can interface two DC motors which can be controlled in both clockwise and counter clockwise direction and if you have motor with fix direction of motion the you can make use of all the four I/Os to connect up to four DC motors. L293D has output current of 600mA and peak output current of 1.2A per channel. Moreover for protection of circuit from back EMF ouput diodes are included within the IC. The output supply (VCC2) has a wide range from 4.5V to 36V, which has made L293D a best choice for DC motor driver.

A simple schematic for interfacing a DC motor using L293D is shown below:
As you can see in the circuit, three pins are needed for interfacing a DC motor (A, B, Enable). If you want the o/p to be enabled completely then you can connect Enable to VCC and only 2 pins needed from controller to make the motor work.


As per the truth mentioned in the image above its fairly simple to program the microcontroller. Its also clear from the truth table of BJT circuit and L293D the programming will be same for both of them, just keeping in mind the allowed combinations of A and B. We will discuss about programming in C as well as assembly for running motor with the help of a microcontroller.

►Assembly programming
CODE:
L293D_A   equ P2.0          ;L293D A - Positive of Motor
L293D_B   equ P2.1          ;L293D B - Negative of Motor
L293D_E   equ P2.2          ;L293D E - Enable pin of IC

         org 0H
Main:
         acall rotate_f     ;Rotate motor forward
         acall delay        ;Let the motor rotate
         acall break        ;Stop the motor
         acall delay        ;Wait for some time
         acall rotate_b     ;Rotate motor backward
         acall delay        ;Let the motor rotate
         acall break        ;Stop the motor
         acall delay        ;Wait for some time
         sjmp  Main         ;Do this in loop
               
rotate_f:
         setb  L293D_A      ;Make Positive of motor 1
         clr   L293D_B      ;Make negative of motor 0
         setb  L293D_E      ;Enable to run the motor
         ret                ;Return from routine
       
rotate_b:
         clr   L293D_A      ;Make positive of motor 0
         setb  L293D_B      ;Make negative of motor 1
         setb  L293D_E      ;Enable to run the motor
         ret                ;Return from routine
       
break:
         clr   L293D_A      ;Make Positive of motor 0
         clr   L293D_B      ;Make negative of motor 0
         clr   L293D_E      ;Disable the o/p
         ret                ;Return from routine
       
delay:                      ;Some Delay
         mov   r7,#20H
back:    mov   r6,#FFH
back1:   mov   r5,#FFH
here:    djnz  r5, here
         djnz  r6, back1
         djnz  r7, back
         ret
 




►C programming
CODE:
#include <AT89X51.H>#define L293D_A P2_0         //Positive of motor
#define L293D_B P2_1         //Negative of motor
#define L293D_E P2_2         //Enable of L293D

// Function Prototypes
void rotate_f(void);         //Forward run funtion
void rotate_b(void);         //Backward run function
void breaks(void);           //Motor stop function
void delay(void);            //Some delay

void main(){                 //Our main function
    while(1){                //Infinite loop
                rotate_f();          //Run forward
                delay();             //Some delay
                breaks();            //Stop
                delay();             //Some delay
                rotate_b();          //Run Backwards
                delay();             //Some delay
                breaks();            //Stop
                delay();             //Some delay
        }                        //Do this infinitely
}

void rotate_f(){
        L293D_A = 1;             //Make positive of motor 1
        L293D_B = 0;             //Make negative of motor 0
        L293D_E = 1;             //Enable L293D
}

void rotate_b(){
        L293D_A = 0;             //Make positive of motor 0
        L293D_B = 1;             //Make negative of motor 1
        L293D_E = 1;             //Enable L293D
}

void breaks(){
        L293D_A = 0;             //Make positive of motor 0
        L293D_B = 0;             //Make negative of motor 0
        L293D_E = 0;             //Disable L293D
}

void delay(){                //Some delay...
    unsigned char i,j,k;
    for(i=0;i<0x20;i++)
        for(j=0;j<255;j++)
                for(k=0;k<255;k++);
}