- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我写了一个简单的程序,从串行端口 (/dev/ttyS1) 读取来自外部设备(条形码扫描仪)的字符,并将其提供给当前事件的窗口(使用 XSendEvent)。
程序在速度较快的计算机上运行良好,但在速度较慢的计算机上,会(经常)发生接收字符的顺序与发送顺序不同的情况。例如,扫描仪向串行端口发送 1234567,我的程序发送 char 事件 1234567,但事件程序(例如 xterm)收到 3127456。我尝试在各个地方调用 XSync 并添加 usleep 调用,但它没有帮助。
有谁知道如何强制字符的“顺序”?
或者是否有其他方法可以将字符串发送到事件窗口(如果需要,我什至不介意使用外部程序)?
这是代码,也许我只是做错了什么:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h> // serial port stuff
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <X11/Xlib.h>
/*
BarCode KeyboardFeeder: BCKF
Copyright (c) Milan Babuskov
Licence: GNU General Public Licence
Compile with: g++ bckf.cpp -lX11 -L /usr/X11R6/lib/ -o bckf
Keycodes: /usr/X11R6/include/X11/keysymdef.h
*/
//-----------------------------------------------------------------------------
void SendEvent(XKeyEvent *event, bool press)
{
if (press)
XSendEvent(event->display, event->window, True, KeyPressMask, (XEvent *)event);
else
XSendEvent(event->display, event->window, True, KeyReleaseMask, (XEvent *)event);
XSync(event->display, False);
}
//-----------------------------------------------------------------------------
bool sendChar(int c)
{
if (c >= 0x08 && c <= 0x1b) // send CR twice
{
sendChar(0xff0d);
c = 0xff0d;
}
printf("Sending char : 0x%02x\n", c);
char disp[] = ":0";
char *dp = getenv("DISPLAY");
if (!dp)
dp = disp;
else
printf("Using env.variable $DISPLAY = %s.\n", dp);
Display *dpy = XOpenDisplay(dp);
if (dpy == NULL)
{
printf("ERROR! Couldn't connect to display %s.\n", dp);
return false;
}
else
{
Window cur_focus; // focused window
int revert_to; // focus state
XGetInputFocus(dpy, &cur_focus, &revert_to); // get window with focus
if (cur_focus == None)
{
printf("WARNING! No window is focused\n");
return true;
}
else
{
XKeyEvent event;
event.display = dpy;
event.window = cur_focus;
event.root = RootWindow(event.display, DefaultScreen(event.display));
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = True;
event.type = KeyPress;
event.state = 0;
event.keycode = XKeysymToKeycode(dpy, c);
SendEvent(&event, true);
event.type = KeyRelease;
SendEvent(&event, false);
}
XCloseDisplay(dpy);
}
usleep(20);
return true;
}
//-----------------------------------------------------------------------------
// Forward declaration
int InitComPort(const char *port);
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Usage: bckf serial_port\n");
return 1;
}
int port = InitComPort(argv[1]);
if (port == -1)
return 1;
while (true)
{
char buf[30];
ssize_t res = read(port, buf, 30);
if (res > 0)
{
for (ssize_t i=0; i<res; ++i)
{
int c = buf[i];
printf("Received char: 0x%02x\n", c);
if (c >= '0' && c <= '9' || c >= 0x08 && c <= 0x1b)
if (!sendChar(c)) // called from console?
break;
}
}
}
return 0;
}
//-----------------------------------------------------------------------------
int InitComPort(const char *port)
{
int c, res;
struct termios newtio;
struct termios oldtio;
// Open modem device for reading and writing and not as controlling tty
// because we don't want to get killed if linenoise sends CTRL-C.
int fdSerial = open(port, O_RDWR | O_NOCTTY );
if (fdSerial < 0)
{
printf(0, "Error opening port: %s\n%s", port, strerror(errno));
return -1;
}
tcgetattr(fdSerial,&oldtio); // save current port settings
memset(&newtio, 0, sizeof(newtio));
newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD; // CREAD : enable receiving characters
// CS8 : character size 8
// CLOCAL : Ignore modem control lines
newtio.c_iflag = IGNPAR; // IGNPAR : ignore bytes with parity errors
newtio.c_oflag = 0; // 0 : raw output (no echo, non-canonical)
//newtio.c_cc[VTIME] = timeout; // 10=1sec : inter-character timer (deciseconds)
newtio.c_cc[VMIN] = 1; // 50 : blocking read until 50 chars received
tcflush(fdSerial, TCIOFLUSH); // clear the line and...
tcsetattr(fdSerial,TCSANOW,&newtio); // ...activate new settings for the port
return fdSerial;
}
//-----------------------------------------------------------------------------
最佳答案
您的代码中的问题是您每次都使用 XOpenDisplay(...) 打开显示。每次调用 XOpenDisplay 都会创建一个新的协议(protocol)上下文。您应该只打开显示一次,并且在发送事件时始终使用相同的显示句柄。在单个显示句柄的上下文中,事件保证保持有序。
初始化显示一次,例如在 main(...) 中,在开始调用 sendChar(...) 之前,始终使用相同的 Display 指针。仅在完成后关闭显示,就像您对 Com 端口所做的那样。
关于c++ - 将字符串(字符)发送到事件窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/643413/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
为什么在 C# 中添加两个 char 结果是 int 类型? 例如,当我这样做时: var pr = 'R' + 'G' + 'B' + 'Y' + 'P'; pr 变量变为 int 类型。我希望它是
下面的代码可以编译,但 char 类型的行为与 int 类型的行为不同。 特别是 cout ::ikIsX >() ::ikIsX >() ::ikIsX >() using names
我正在寻找一个正则表达式,它可以匹配长度为 1 个或多个字符但不匹配 500 的内容。这将在 Rails 路由文件中使用,特别是用于处理异常。 路线.rb match '/500', to: 'err
对于 C 编程作业,我正在尝试编写几个头文件来检查所谓的“X 编程语言”的语法。我最近才开始,正在编写第一个头文件。这是我编写的代码: #ifndef _DeclarationsChecker_h_
为什么扩展的 ascii 字符(â、é 等)被替换为 字符? 我附上了一张图片...但我正在使用 PHP 从 MySQL 中提取数据,其中一些位置有扩展字符...我使用的是 Arial 字体。 您可以
我有一个与 R 中的断线相关的简单问题。 我正在尝试粘贴,但在获取(字符/数字)之间的断线时遇到问题。请注意,这些值包含在向量中(V1=81,V2=55,V3=25)我已经尝试过这段代码: cat(p
如何将 ANSI 字符 (char) 转换为 Unicode 字符 (wchar_t),反之亦然? 是否有用于此目的的任何跨平台源代码? 最佳答案 是的,在 中你有mbstowcs()和 wcsto
函数 fromCharCode 不适用于国际 ANSI 字符。例如,对于 ID 为 192 到 223 的俄语 ANSI (cp-1251) 字符,它返回特殊字符。如何解决这个问题? 我认为,需要将A
如果不喜欢,我想隐藏 id,但不起作用 SELECT * FROM character, character_actor WHERE character.id NOT LIKE character_a
现在这个程序成功地反转了键盘输入的单词。但是我想在我反转它之前“保存”指针中的单词,所以我可以比较两者,反转的和“原始的”,并检查它们是否是回文。我还没有太多经验,可能会出现比我知道的更多的错误,但我
Memcpy 和 memcmp 函数可以接受指针变量吗? char *p; char* q; memcpy(p,q,10); //will this work? memcmp(p,q,10); //w
恐怕我对一个相当过饱和的主题的细节有疑问,我搜索了很多,但找不到一个明确的答案来解决这个特定的明显-imho-重要的问题: 使用UTF-8将byte[]转换为String时,每个字节(8bit)都变成
我有一个奇怪的问题。我需要从 stat 命令打印输出字符串。 我已经编写了获取一些信息的代码。 import glob import os for file in glob.glob('system1
我正在使用 Java 并具有其值如下所示的字符串, String data = "vale-cx"; data = data.replaceAll("\\-", "\\-\\"); 我正在替换其中的“
String urlParameters = "login=test&password=te&ff"; 我有一个String urlParams,& - 是密码的一部分,如何使其转义,从而不被识别为分
大家好,我只想从此字符串中提取第一个字母: String str = "使 徒 行 傳 16:31 ERV-ZH"; 我只想获取这些字符: 使 徒 行 傳 并且不包括 ERV-ZH 仅数
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
所以, 我有一个字符**;它本质上是一个句子,带有指向该句子中每个单词的指针;即 'h''i''\0''w''o''r''l''d''\0''y''a''y''!''\0' 在这种情况下,我希望使用可
这个问题在这里已经有了答案: Using quotation marks inside quotation marks (12 个答案) 关闭 7 年前。 如何打印 " 字符? 我知道打印 % 符号
我是一名优秀的程序员,十分优秀!