串口键盘在Qt上的移植

本文介绍了如何在Qt项目中移植串口键盘驱动,涉及对qkbdtty_qws.cpp、qkbdtty_qws.h和qkbddriverfactory_qws.cpp的修改,以及串口数据校验的实现。通过修改Qt的源代码,将键盘驱动与串口通信结合,实现了自定义键盘的使用。编译并替换libQtGui.so库后,可在目标板上运行。
摘要由CSDN通过智能技术生成

项目中用到Qt进行开发,其中键盘部分电路自己设计,所以qt自带的键盘驱动无法直接使用,需要进行一些简单的移植才可以。

键盘电路是用单片机扫描方式,通过串口与系统的连接。为了修改方便,直接拿qkbdtty_qws.cpp进行修改,同时修改的还有qkbdtty_qws.h和qkbddriverfactory_qws.cpp,这几个文件都在src/gui/embedded下面。

为了串口传送的数据具有一定的完整性,我在串口发送键值时,采用的是一次发送2字节,即键值和键值的反码,接收后方便校验。

编译方法:代码修改完成后,只编译gui的库即可,即到gui目录下make,所作的改动都在qt的libQtGui.so里面,编译完成后拷贝该库到目标板即可。当然,Qt的系统配置里面键盘要配置为tty,一般在profile里面配置。

下面是源码:

qkbddriverfactory_qws.cpp文件修改部分如下:

# ifndef QT_NO_QWS_KBD_TTY
    if (driver == QLatin1String("tty") || driver.isEmpty())
        return new QWSMyKeyboardHandler(device);
# endif


qkbdtty_qws.cpp源码:


#include "qkbdtty_qws.h"

#if !defined(QT_NO_QWS_KEYBOARD) && !defined(QT_NO_QWS_KBD_TTY)

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/ioctl.h>

#include <fcntl.h>

#include <termios.h>

#include <unistd.h>

#include <errno.h>

#include <private/qcore_unix_p.h>

#include <qsocketnotifier.h>


#include<stdio.h>      
#include<stdlib.h>     
#include<string.h>



int UART_Open(int fd,char* port)
{

  fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY);
//fd = open( port, O_RDWR);
  if (FALSE == fd){
perror("Can't Open Serial Port");
  return(FALSE);
  }
  if(fcntl(fd, F_SETFL, 0) < 0){
printf("fcntl failed!\n");
    return(FALSE);
  } else {
           //    printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
  }
  if(0 == isatty(STDIN_FILENO)){
  printf("standard input is not a terminal device\n");
        return(FALSE);
  }
  return fd;
}
void UART_Close(int fd)
{
close(fd);
}
int UART_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
    
    int   i;
      int   status;
  int   speed_arr[] = { B115200,B57600,B38400, B19200, B9600, B4800, B2400, B1200, B300,
                        B115200,B57600,B38400, B19200, B9600, B4800, B2400, B1200, B300
                      };
   

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值