Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to control the hardware under Qt

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to control hardware under Qt". In daily operation, I believe many people have doubts about how to control hardware under Qt. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to control hardware under Qt". Next, please follow the editor to study!

We can use C and C++ for mixed programming. Let's first look at an APP code that controls the buzzer. Here, take Terminator I.MX6ULL as an example. ITOP-4412 can refer to the test routine code as follows:

# include "stdio.h" # include "unistd.h" # include "sys/types.h" # include "sys/stat.h" # include "fcntl.h" # include "stdlib.h" # include "string.h" # define BEEPOFF 0

# define BEEPON 1

/ *

* @ description: main main program

* @ param-argc: number of elements in the argv array

* @ param-argv: specific parameters

* @ return: 0 success; other failures

, /

Int main (int argc, char * argv [])

{

Int fd, retvalue

Char * filename

Unsigned char databuf [1]

If (argc! = 3) {

Printf ("Error Usage!\ r\ n")

Return-1

}

Filename = argv [1]

/ * Open the beep driver * /

Fd = open (filename, O_RDWR)

If (fd

< 0){ printf("file %s open failed!\r\n", argv[1]); return -1; } databuf[0] = atoi(argv[2]); /* 要执行的操作:打开或关闭 */ /* 向/dev/beep 文件写入数据 */ retvalue = write(fd, databuf, sizeof(databuf)); if(retvalue < 0){ printf("BEEP Control Failed!\r\n"); close(fd); return -1; } retvalue = close(fd); /* 关闭文件 */ if(retvalue < 0){ printf("file %s close failed!\r\n", argv[1]); return -1; } return 0; } 通过上面的代码我们可以发现,使用 C 语言来控制一个蜂鸣器,无非就是打开设备节点,然后通过 write来写入数据就可以控制蜂鸣器了。既然我们可以使用 C 和 C++混合编程,那么我们是不是只需要把这个 C控制蜂鸣器的代码直接加到 QT 程序里面就可以了,那我们我们要怎么进行混合编程呢? 我们先新建一个 QT 工程,然后添加俩个按钮来控制蜂鸣器的打开和关闭,UI 界面如图 62.4.1:

Then we started to migrate the code, because C++ is object-oriented and C is process-oriented, so on QT, we have to program with object-oriented ideas, so our first step is to create a class that encapsulates the code of the C voice control buzzer into a class.

We right-click the project, then select add New File, and add C++ Class as shown in figure 62.4.2:

Then we add the name of the class, because we are operating a buzzer, so we write it as Beep here, as shown in figure 62.4.3:

In the first step, we add the header file of the C language control buzzer code to the file beep.h generated by the Beep class we just added, but we cannot directly copy the header file. We need to enclose it in extern "C" {}. The code is as follows:

Extern "C" {

# include "stdio.h" # include "unistd.h" # include "sys/types.h" # include "sys/stat.h" # include "fcntl.h" # include "stdlib.h" # include "string.h"}

The addition is completed as shown in figure 62.4.4:

Figure 62.4.4

The second step is to copy the parameter definition in our C language control buzzer to public with the following code:

Int fd, retvalue

Char * filename

Unsigned char databuf [1]

The addition is shown in figure 62.4.5:

Then we open beep.cpp and copy the code that opens the device node in the C language control buzzer into the constructor. Note that we cannot use printf in QT, as follows:

/ * Open the beep driver * /

Fd = open (filename, O_RDWR)

If (fd < 0) {

Return

}

The addition is shown in figure 62.4.6:

Figure 62.4.6

Then we create a function that turns on the buzzer and a function that closes the buzzer. The first step, now declared in beep.h, and then in the

Implemented in beep.cpp.

Declaration:

The code is as follows:

Void Beep_On ()

Void Beep_Off ()

The addition is shown in figure 62.4.7:

Figure 62.4.7

Achieve:

The code is as follows. Note that the c language function is preceded by::, indicating that this function is a C function.

Void Beep::Beep_On ()

{

/ * write data to / dev/beep file * /

Databuf [0] = 1

Retvalue =:: write (fd, databuf, sizeof (databuf))

If (retvalue < 0) {

:: close (fd)

Return

}

}

Void Beep::Beep_Off ()

{

Databuf [0] = 0

Retvalue =:: write (fd, databuf, sizeof (databuf))

If (retvalue < 0) {

:: close (fd)

Return

}

}

The addition is shown in figure 62.4.8:

Figure 62.4.8

After adding it, we can just use this class directly. Let's first create a new beep in widget.

Then we create a new object in the constructor, as shown in figure 62.4.9:

Figure 62.4.9

Then we can perform the corresponding opening and closing actions in the slot function, as shown in figure 62.4.10:

At this point, the study of "how to control the hardware under Qt" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report