简体   繁体   English

如何通过串行端口发送请求从目录中检索数据?

[英]How to retrieve a data from a directory by sending a request through the serial port?

I have a controller which has a serial port and ethernet. 我有一个具有串行端口和以太网的控制器。 I want to retrieve an event and the data associated with this event from the event directory through the serial or ethernet port of the controller. 我想通过控制器的串行或以太网端口从事件目录中检索事件和与此事件关联的数据。 I do have a packet format (request packet data) for the specified event to be retrieved. 我确实有要检索的指定事件的数据包格式(请求数据包数据)。 Can anyone tell me how to retrieve the data by sending a request through the serial port? 谁能告诉我如何通过串行端口发送请求来检索数据? I am beginner and not that much well-versed in programming. 我是新手,对编程不太了解。

You will need to have some kind of a program running on your embedded platform, listening to the serial port and answering requests. 您将需要在嵌入式平台上运行某种程序,以侦听串行端口并回答请求。 This kind of program is usually called a "daemon" (pronounced the same as "demon"; just like "Caesar" rhymes with "sea star"). 这种程序通常称为“守护程序”(发音与“ demon”相同;就像“ Caesar”押韵与“ sea star”一样)。

If you already have a daemon, you will need to figure out what format it uses. 如果您已经拥有一个守护程序,则需要弄清楚它使用什么格式。 Since I have no idea what you might have I cannot even guess. 由于我不知道您可能拥有什么,我什至无法猜测。

If you will be writing your own daemon, you will need to choose some sort of protocol. 如果要编写自己的守护程序,则需要选择某种协议。 Personally I like the JSON format for a serial protocol; 我个人比较喜欢串行协议的JSON格式。 it is simple enough that you can extract data just using sscanf() from the C library if there is not a better library available, and of course it's easy to build JSON strings just using sprintf() . 如果没有更好的库可用,那么只需使用sscanf()从C库中提取数据就足够简单了,当然,仅使用sprintf()即可构建JSON字符串很容易。

http://json.org/ http://json.org/

What you want is the Serial Programming Guide for POSIX Operating Systems . 您需要的是《 POSIX操作系统串行编程指南》 If you are bound to Windows for some reason, you get POSIX through installing Cygwin. 如果由于某种原因绑定到Windows,则可以通过安装Cygwin获得POSIX。 Expect to become familiar with man pages like termios and fcntl since you'll first have to set the serial port parameters to work with your device, though they're likely to be the standard 8-N-1 at some rate. 希望您熟悉诸如termiosfcntl类的手册页,因为您必须首先设置串行端口参数才能与您的设备一起使用,尽管它们在某种程度上可能是标准的8-N-1。 Then it's a matter of reading and writing bytes to the port's file descriptor. 然后,只需将字节读取和写入端口的文件描述符即可。 You're more likely to be using the low level open() , close() , read() , and write() , which are a level below the stdio ( printf , fopen , stdout ) you're more likely to be used to as a newer programmer. 您更可能使用低级的open()close()read()write() ,它们是您更可能使用的stdioprintffopenstdout )之下的级别。作为一个新的程序员。

Computers these days often lack the RS232 serial port, so if you need one you can find a cheap USB adapter. 如今的计算机通常缺少RS232串行端口,因此,如果需要RS232串行端口,您可以找到便宜的USB适配器。 Be aware that USB adapters don't necessarily implement some of the ancillary signals (RTS,CTS,etc.) in my experience. 请注意,根据我的经验,USB适配器不一定会实现某些辅助信号(RTS,CTS等)。

Also look into libraries for your specific needs and situation. 另外,请针对您的特定需求和情况查看图书馆。

You should specify the controller for more useful answers. 您应该指定控制器以获得更多有用的答案。

Your controller should support any data exchange protocol. 您的控制器应支持任何数据交换协议。 You can find this info in documenttion. 您可以在文档中找到此信息。 May be, it supports MODBUS or MODBUS TCP. 可能是,它支持MODBUS或MODBUS TCP。 In this case you can use any modbus compatible software. 在这种情况下,您可以使用任何与Modbus兼容的软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM