Modbus Software Development Practical Guide to develop your own Modbus poll tools-2

Source: Internet
Author: User
Tags gettext

To the content of the previous article.

After looking at the complex command-line parsing features mentioned earlier, many people start to get scared, but they don't have to.

As we all know, Linux programs often provide complex command-line parameter handling mechanisms, as this is

The main means of interacting with other programs or users, in which case it is commendable that in order to mitigate the development

The burden of personnel on command-line processing, Linux provides system functions getopt () or Getopt_long () specifically parsing command-line parameters.

In a Linux system, the function getopt ()/getopt_long () is located in the Unistd.h system header file, with the following prototypes:

int getopt (int argc,char * Const ARGV[],CONST char * optstring);

int Getopt_long (int argc, char * const ARGV[],CONST Char *optstring,

const struct Option *longopts, int *longindex);

Where the parameters argc and argv are the number and contents of the arguments passed by main function main ().

The parameter optstring represents the option string to be processed. This function returns the next option letter in the argv,

This letter corresponds to the letter in the parameter optstring. If the letter in the option string is followed by a colon ":", it indicates that there are also related parameters,

The global variable Optarg points to this extra parameter. If getopt () does not find a matching parameter, the error message is printed and the whole domain

Variable optopt set to "?" Character, if you do not want getopt () to print error messages, simply set the global variable Opterr to 0.

Parameters can be easily divided into two types, short and long, getopt () uses the string referred to by optstring as a short argument list,

Like "1ac:d::" is a short argument list. The definition of a short parameter is a '-' followed by a letter or number, like-A,-B is a

Short parameters, each number or letter defines a parameter.

The long parameter is shaped like "--debug", preceded by 2 '-' symbols, which can be followed by adding multiple letters or numbers.

The Getopt_long () function contains the function of the getopt () function, and you can also specify a "long parameter" (or Long option).

Compared to the getopt () function, Getopt_long () has two more parameters than getopt ().

The basic usage of this function is as follows (Linux):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25

#include <stdio.h>
#include <unistd.h>

int main (int argc, int *argv[])
{
int ch;
Opterr = 0;

Getopt () can be replaced by Getopt_long ()
while ((ch =getopt(ARGC, argv, "A:BCDE")) ! =-1)
{
Switch (CH)
{
Case ' a ':
printf ("Option A: '%s ' \ n", Optarg);
Break
Case ' B ':
printf ("option b:b\n");
Break
Default
printf ("Other option:%c\n", ch);
}
}
printf ("Optopt +%c\n", optopt);
}

As a reference, the visible call function getopt () or Getopt_long () can easily parse the command line.

However, it is a little pity that such a convenient function is not available under Windows, what to do? Of course, there's a way.

Since the function getopt ()/getopt_long () is a function in GNU C, the source is visible and can be ported directly to Windows, depending on the situation.

Say dry, then briefly introduce the transplant method, master a little new skills, if this part is not interested, you can skip, look at the back of the content.

First, visit the GNU C Library (glibc) home page http://www.gnu.org/software/libc/and download the latest GLIBC library,

The current latest version is glibc-2.24.tar.gz, downloaded and unzipped.

Extract the 4 source files under the pressurized directory \glibc-2.24\posix\ getopt.h/getopt.c/getopt_int.h/

Getopt_init.c,.

Figure extract Getopt () related files

Start Visual Studio 2015, select the menu "File", "New", "Project ...",

Prepare to create a new default project with the project type "Visual C + +" → "Win32 Console application".

After you have created a new default project, switch to the Explorer screen, copy the above 4 files to the directory where the new project is located, and add them to the project.

Figure Add getopt () source file

After the file has been added, we try to compile it and see that there is a compile error in the file getopt.c:

GETOPT.C: Fatal error C1083:cannot open include file: 'gettext.h': No such file or directory

The first thing to change is that there is no "gettext.h" for this header file. Modify the method to comment out or delete it directly, and then modify the following macro definition.

Place the following raw code (approximately 70 lines):

1
2
3
4
5
6

#ifdef _LIBC
# include <libintl.h>
#else
# include "Gettext.h"
# define _ (MsgId) GetText (MsgId)
#endif

Modified to:

1
2
3
4
5

#ifdef _LIBC
# include <libintl.h>
#else
# define _ (MsgId) (MsgId)
#endif

After the modification, continue to compile a look, the following compilation error appears.

                    

Figure compilation Error Alloca not recognized

The text description of the error is:

GETOPT.C (568): Warning C4013: ' alloca ' undefined; assuming extern returning int

Error lnk2019:unresolved external symbol _alloca referenced in function __getopt_internal_r

It can be found that the reason for the error here is that the ALLOCA function is not defined, so what does the Alloca function mean?

The original alloca is a memory allocation function, similar to malloc, Calloc, realloc, but notice an important difference,

The Alloca function applies a space on the stack (stack) and releases it immediately after it is exhausted.

In general, the function Alloca is included in the header file malloc.h, which is defined as the macro definition of the intrinsic function _alloca in some systems.

Now that the prototype has been known, then modify Alloca to _alloca can solve the problem.

Figure modified to _alloca resolve compilation errors

Continue adding the definition of Getopt_long ()/getopt_long_only (), which is declared in the Getopt.h file.

However, it is defined in getopt1.c and can be used directly from the getopt1.c file, but because there is not much content in the file,

In order to reduce the number of files, it is a good idea to copy the useful parts directly into the getopt.c file.

The content to be copied in the file getopt1.c is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

Int
Getopt_long (int argc, char *const *argv, const char *options,
const struct Option *long_options, int *opt_index)
{
Return _getopt_internal (argc,argv,options,long_options,opt_index,0,0);
}

Int
_getopt_long_r (int argc, char *const *argv, const char *options,
const struct Option *long_options, int *opt_index,
struct _getopt_data *d)
{
Return _getopt_internal_r (Argc,argv,options,long_options,opt_index,
0, D, 0);
}

/* like Getopt_long, but '-' as well as '--' can indicate a long option.
If an option is starts with '-' (not '--') doesn ' t match a long option,
But does match a short option, it's parsed as a short option
instead. */

Int
getopt_long_only (int argc, char *const *argv, const char *options,
const struct Option *long_options, int *opt_index)
{
Return _getopt_internal (argc,argv,options,long_options,opt_index,1,0);
}

Int
_getopt_long_only_r (int argc, char *const *argv, const char *options,
const struct Option *long_options, int *opt_index,
struct _getopt_data *d)
{
Return _getopt_internal_r (Argc,argv,options,long_options,opt_index,
1, D, 0);
}

Copy the above code into the file getopt.c function getopt () after the definition can, modify the compile, all ok!

This is the end of the function getopt () migration. After the above modification, some simple tests can be performed to verify

The test cases do not have to write their own, in the file getopt.c and getopt1.c files have, directly to use it.

At this point, the rebuilt 4 files: getopt.h/getopt.c/getopt_int.h/getopt_init.c is the required command line parsing source code files, can be used under Windows system.

At this point, for their own development of the Modbus Poll tool command-line parsing function is basically implemented.

Next, you will perform the Code analysis and debugging of the functional section.

Modbus Software Development Practical Guide to develop your own Modbus poll tools-2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.