我需要将二进制数据传递给接受命令行参数的bash程序.有没有办法做到这一点?
这是一个接受一个参数的程序:
script arg1
Run Code Online (Sandbox Code Playgroud)
但是arg1
,我想传递一些不是很好的ASCII字符的字节0x02
,而不是字符串,特别是字节0xc5
和0xd8
.
我该怎么做呢?
我知道Linux从/ dev/input/mice中发出了9位2的赞美数据.我也知道你可以通过/ dev/hidraw0获取数据,其中hidraw是你的USB设备,从HID中提供原始数据.我知道发送的数据是移动的增量(位移)而不是位置.通过我也可以通过"cat/dev/input/mice"查看乱码数据.我的问题是:
你能用Python语言告诉我如何读取这些数据?我真的很喜欢用简单的整数来获取数据.但事实证明这很难.真正的问题是阅读该死的数据.有没有办法读取位并进行位运算?(目前我并不担心root用户相关的问题.请假设脚本在root中运行.)
我尝试在Linux中控制鼠标.Xlib似乎有效,但是当我尝试将它与OpenCV一起使用时,它会一直返回:
Resource temporarily unavailable
Run Code Online (Sandbox Code Playgroud)
所以我决定写"/ dev/psaux".代码如下:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
unsigned char a[5]={0, 0xff, 0, 0x28, 0xff};
int fp = open ("/dev/psaux", O_WRONLY);
if(!fp)printf("open error:%s\n", strerror(errno));
for(int i = 0; i < 10; i++)
printf("write:%d\t\t%s\n", write(fp, a, 5), strerror(errno));
close(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译它:
gcc my_psaux.c -o my_psaux -std=gnu99 -g
Run Code Online (Sandbox Code Playgroud)
跑,得到
$sudo ./my_psaux
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success …
Run Code Online (Sandbox Code Playgroud)