Linux 充当代理的IO服务器
嗨,我需要建立一个IO服务器,就像一个经纪人 Web服务器与服务器(代理)对话以发出请求,例如可以控制哪些设备。嵌入式系统。。通过rs232或usb和以太网连接到服务器的 例如,Web服务器与设备A对话并请求执行X 服务器(代理)还与设备对话。当设备启动时,它将自己注册到代理。我是设备b,我的名字是k,我准备接受命令 我使用的是DebianLinux,我需要知道如何使用它 这是否需要创建内核设备 非常感谢 编辑:我刚刚在这里读到关于守护程序编程的内容。我在这里复制并粘贴了代码 我写下了关于我将更改代码哪一部分的注释Linux 充当代理的IO服务器,linux,linux-kernel,embedded-linux,Linux,Linux Kernel,Embedded Linux,嗨,我需要建立一个IO服务器,就像一个经纪人 Web服务器与服务器(代理)对话以发出请求,例如可以控制哪些设备。嵌入式系统。。通过rs232或usb和以太网连接到服务器的 例如,Web服务器与设备A对话并请求执行X 服务器(代理)还与设备对话。当设备启动时,它将自己注册到代理。我是设备b,我的名字是k,我准备接受命令 我使用的是DebianLinux,我需要知道如何使用它 这是否需要创建内核设备 非常感谢 编辑:我刚刚在这里读到关于守护程序编程的内容。我在这里复制并粘贴了代码 我写下了关于我将更
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
int main(void) { // change this to "int main(int argc,char *argv[])"
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
/* The Big Loop */
while (1) {
/* Do some task here ... */ // Device detect here. eg(/dev/ttyS0-usb-serial)
sleep(30); /* wait 30 seconds */ // Instead of using 30 seconds here I plan on removing it and changing it with events like wake up when new device is plugged.
}
exit(EXIT_SUCCESS);
}
换成这个
int main(int argc,char *argv[])
所以我可以从debian的命令行发送命令
我将在while循环中添加此代码-大循环
if(strcmp(argv[1], "status")==0){
//check status of devices here
}
if(new Device plugged){ // not real code. just to get the idea.
// register the device type and name.
}
然后在php中使用shell_exec()
我的想法可行吗?RS232、usb和以太网连接设备通常不需要新的驱动程序 您只需要创建一个与设备对话的守护程序和一个在web服务器上执行的cgi,该cgi通过IPC与守护程序对话并交换命令和结果。您可以尝试使用更简单的设备,即从cgi读取设备,但在这种情况下,您可能会有较高的延迟,并且无法对设备输入做出反应
尽管如此,这个问题的表述过于笼统,无法给出更多的建议。这些设备做什么?您希望使用哪种语言?IPC机制的一个好选择是。您让守护进程在系统总线上公开一个端点,并在收到请求时进行webservice调用。对于您可以用于Web服务的几乎每一种可能的实现技术,都有dbus绑定。我认为PHP或Python是显而易见的。@Ottavio-谢谢你的回答,我计划使用c作为语言。是的,我也在想,在debian中创建一个应用程序/程序,它可以与您所说的daemon程序之类的设备进行通信。因此,Web服务将向该程序发送命令,读取它并检查哪些设备可用。这些设备控制电机(例如步进电机、直流电机、伺服电机)。有没有关于如何创建用于检测设备是否在线的守护程序的教程?@Marko-谢谢Marko,是的,PHP是我的目标web服务。。php向debian发送命令,然后程序(守护进程)根据webservice发送的命令进行读取和执行。我计划发送文本字符串发送到程序,例如(status-对于可用设备)或字节流,如status的“01”命令。我的想法是否适用于此?。谢谢,我有一个不太不同的项目。我使用消息队列,如Unix网络编程第2卷所述。我的cgi是用C编写的,但通过谷歌搜索,我在这里看到,这也是用php编写的。通过巧妙地组织代码,您还可以在设备上同时使用不同的输入。
if(strcmp(argv[1], "status")==0){
//check status of devices here
}
if(new Device plugged){ // not real code. just to get the idea.
// register the device type and name.
}
<?php $output = shell_exec('daemon status'); echo "$output"; die;
Device Name | Status
/dev/ttyS0 = online
/dev/sample = offline