两台计算机之间使用 2 c++ 进行通信节目

发布于 2024-09-12 09:32:10 字数 1434 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

七分※倦醒 2024-09-19 09:32:10

您可以使用简单的 UDP 协议 - 优点是,如果您了解 RS232 上的简单数据包协议的概念,您会发现很容易将这些知识转移到通过 UDP 发送数据包。

如果您希望两台 PC 之间有可靠的(例如,系统的其他部分会担心错误和重试)字节流,那么 TCP/IP 的使用并不比 UDP 复杂多少。

UDP 和 TCP 都是通过“套接字”访问的。恐怕您会发现 C++ 有很多繁琐的样板文件来使其工作,但周围有很多很多的例子。

You might use a simple UDP protocol - the advantage being that if you understand the concepts of simple packet protocols on RS232 you'll find it easy to transfer that knowledge to sending the packets via UDP.

If you want a reliable (as in, other parts of the system will worry about errors and retries) stream of bytes between the two PCs, then TCP/IP is not much more complicated to use than UDP.

Both UDP and TCP are accessed through 'sockets'. I'm afraid you'll find that from C++ there is rather a lot of tedious boilerplate to getting that working, but there are lots and lots of examples around.

苍暮颜 2024-09-19 09:32:10

如果它们是网络连接的,你可以只使用套接字。

If they are network-connected you could just use sockets.

油焖大侠 2024-09-19 09:32:10

最好的选择是使用网络通信。解决这个问题的最简单方法应该是查看 Qt 中的网络示例。

您基本上将创建一个客户端和一个服务器应用程序。您可以决定客户端在看到来自服务器的特定消息时执行的操作。就这样。 Qt 应该处理剩下的事情。

其他答案建议 TCP/IP、UDP、RS232...当您使用 QtNetwork 模块时,所有这些都只是选项。我认为既然你问了你的问题,你就不知道它们之间的区别。因此,最安全的选择是使用最高级别(免费)的库,因此建议研究 Qt。

另一种选择是使用 Boost.Asio。我更喜欢 Qt 解决方案,因为他们的 API 更好。

The best option will be to use network communication. The easiest way to approach this should be to look at the networking examples in Qt.

You basically will create a client and a server application. You decide what the client does when it sees a certain message from the server. That's all. Qt should take care of the rest of the stuff.

Other answers suggests TCP/IP, UDP, RS232, ... All those things are just options when you use QtNetwork module. I assume that since you ask your question, you don't know about the difference between those. So the safest bet will be to use the highest level (free) library, hence the suggestion to look into Qt.

Another option is to use Boost.Asio. I tend to prefer Qt solution since their API is nicer.

不必了 2024-09-19 09:32:10

这听起来对于网络套接字来说是一个相当好的用途。如果您的两台机器都运行 Windows,您甚至可以使用命名管道。

That sounds like a fairly good use for the network socket. If both your machines are on Windows you can even use named pipes.

情感失落者 2024-09-19 09:32:10

对于 Windows,您需要将 COM n 端口作为文件打开,以便通过串行端口进行通信[1]。我现在无法访问我的代码,我可以回家后查看。

RS232 很简单,我喜欢它。然而,它很慢。您需要在设计中考虑这一点。

[1] 对于 C++。

For Windows, you will need to open the COM n port as a file to communicate over a serial port[1]. I don't have access to my code now, I can look it up when I get home.

RS232 is easy and I like it. However, it it is slow. You need to consider that in your design.

[1] For C++.

梦中的蝴蝶 2024-09-19 09:32:10

大多数现代计算机都具有以太网功能,因此请为自己准备一个便宜的集线器或交换机并查看网络 API。通常有一些相当简单的套接字东西。这样做的优点之一是,如果您想稍后提高通信能力,例如让视觉软件为机器人提供指令和指导,那么您已经完成了基础知识设置。

或者,设置您的视觉程序,以便您可以通过按随机键来启动和停止它。当您要使用它时,将键盘放在机器人计算机的 CD 驱动器前面,并在机器人运行开始和结束时弹出。

Most modern computers have Ethernet capability, so get yourself a cheap hub or switch and look at networking APIs. There's usually some fairly easy socket stuff. One advantage of this is that, if you want to increase communication ability later, such as having your vision software provide instructions and guidance to your robot, you've got the basics set up.

Alternately, set up your vision program so you can start and stop it by hitting random keys. When you're going to use it, put the keyboard in front of the robot computer's CD drive, and eject at the start and end of the robot run.

多情癖 2024-09-19 09:32:10

在您的情况下,这可能有点过分了,但如果我处于您的位置,我可能会使用 HTTP 协议来实现它。视觉计算机将运行 HTTP 服务器,机器人计算机将使用 POST 请求传达状态变化。 Poco C++ Net 库 为您提供了执行此操作所需的工具。

This may be overkill in your situation, but if I were in your shoes I would probably implement it using the HTTP protocol. The vision computer would run a HTTP server and the robot computer would communicate the state changes using POST requests. The Poco C++ Net library provides you with the facilities required to do this.

╭⌒浅淡时光〆 2024-09-19 09:32:10

我会使用 TCP/IP 套接字进行通信。 TCP 保证数据能够到达。因此,您所需要做的就是解析数据。

I would use a TCP/IP socket for communications. TCP guarantees that the data will make it. So, all you need to do is parse the data.

抱猫软卧 2024-09-19 09:32:10

RS232 是一个易于编程的选项,但现代 PC 往往没有 RS232 端口。您可能需要获取 USB-RS232 适配器或安装 PCI 卡。

RS232 的另一个问题是您需要担心额外的电线,这可能会很麻烦。此外,RS232 电缆的长度可能受到限制 (5-15m),除非您投资一些笨重的 RS232 中继器或蓝牙连接器等。

最重要的是,您还向项目中添加了一项可能会出错并导致成本高昂的项目部署和调试的时间。

IMO,一个优雅的工程解决方案是利用您拥有的硬件并使用 TCP/IP 套接字进行通信。

网络上充斥着在服务器和客户端之间传递消息的示例:

如果您使用的是 Linux:
http://www.linuxhowtos.org/C_C++/socket.htm

使用 Windows:
http://www.adp-gmbh.ch/win/misc/sockets。 html

RS232 is an easy option to program for, however modern PCs don't tend to have RS232 ports. You may need to get USB-RS232 adapters or install a PCI card.

The other problem with RS232 is that you have an additional wire to worry about which can be a nusiance. Also RS232 cables can be limited in length (5-15m) unless you invest in some clunky RS232 repeaters or bluetooth connectors, etc.

On top of all that you're also adding one more item to your project that can go wrong and cost you time in deploying and debugging.

IMO, an elegant engineering solution would be to utilise the hardware that you have and use TCP/IP sockets to communicate.

The web is awash with examples on passing messages between servers and clients:

If you're using Linux:
http://www.linuxhowtos.org/C_C++/socket.htm

Using Windows:
http://www.adp-gmbh.ch/win/misc/sockets.html

三生一梦 2024-09-19 09:32:10

我还可能会查看类似 0MQ 的内容,以使连接更加可靠。无论传输方式如何,它都会传输和重组消息,并在暂时失去连接的情况下处理缓冲。

但底线是我会使用 TCP/IP,但根据机器人的性质,您可能需要比 TCP 套接字稍微更强大的连接系统。 UDP 很好,因为它是无连接的——如果机器人暂时超出范围/视线/等,您将不必重建套接字和上下文。

I also might look at something like 0MQ to make the connection more robust. It will transmit and reassemble messages regardless of the transport, and handle buffering in the case of temporary loss of connectivity.

But the bottom line is that I would use TCP/IP, but depending on the nature of the robot you may want a slightly more robust connection system than TCP sockets. UDP is nice because it's connectionless-- if the robot temporarily travels out of range/sight/etc you wont have to rebuild the socket and context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文