## The Hello-World-Sample

> The previous section introduces an very short overview. Here we want to show you a whole sample. This sample is cut into smaller pieces. Each piece have its own desciption. Ok, let's start with a skeleton.

>     // sample.c
>     #include <stdio.h>
>     #include <ezV24/ezV24.h>

>     v24_port_t *UsedPort=NULL;

>     static void installSignalhandler ( void )
>     {
>         signal(SIGINT,mySignalHandler);
>         signal(SIGTERM,mySignalHandler);
>     }

>     static void mySignalHandler ( int reason )
>     {
>         v24ClosePort(UsedPort);
>         exit(99);
>     }

>     void main (void)
>     {
>         installSignalhandler();
>         // part-2 ...
>     }

> The above skeleton `sample.c` show's several important parts. First it includes the base header of the library. After this, the global variable `UsedPort` is declared and set to `NULL`. This variable will hold the initialized handle. To ensure, that program close the port, a signal handler is installed by `installSignalhandler`.

>         // part-2
>         UsedPort=v24OpenPort("/dev/ttyS0",V24_STANDARD);
>         if ( UsedPort==NULL )
>         {
>             fputs("error: sorry, open failed!\n",stderr);
>             return;
>         }
>         // part-3 ...
>         v24ClosePort(UsedPort);

> This part opens the device `/dev/ttyS0`. After the work (of part-3) is done, the port is closed. This snippet doesn't use any special open flags. The port name is fix. To be a little bit more platform independent, we can use v24PortName.

>         // part-3
>         rc=v24SetParameters(UsedPort,V24_B9600,V24_8BIT,V24_NONE);
>         if ( rc!=V24_E_OK )
>         {
>             fputs("error: setup of the port failed!\n",stderr);
>     	v24ClosePort(UsedPort);
>     	return 1;
>         }
>         // part-4 ...

> In part-3, we try to set the communication parameters of the opened port. In the above sample, the baudrate is set to 9600. The size of the data byte is set to 8 bits, and the parity bit generation is disabled. To see all possible parameters, just have a look at v24SetParameters. The shown setup is the default used by v24OpenPort. Nevertheless, it is better to use a explicit call to `v24SetParameters` to setup the port. The code will be more readable and IMO it's better style.

> Note: as you can see, the program aborts, if the setup fails. Because of this, we have to close the port!

>         // part-4
>         char* msg="Hello world.\n\r";
>         char answer[80];
>         rc=v24Puts(UsedPort,msg);
>         if ( rc < strlen(msg) )
>         {
>             fputs("error: v24Puts failed.\n",stderr);
>         }
>         else
>         {
>             rc=v24Gets(UsedPort,answer,sizeof(answer)-1);
>             if ( rc < 0 )
>             {
>                 fputs("error: v24Gets failed!\n",stderr);
>             }
>             else
>                 printf("the answer is `%s'\n",answer);
>         }

> This snippet of part-4 send the string `"Hello world.\n\r"`. If all characters are sent, it waits for a reply. Look's good?

> For now that's all folks. (but I'm working on more stuff ;-)

Alphabetic index

This page was generated with the help of DOC++.