简体   繁体   English

写入串口 (C++/Windows)

[英]Writing to Serial Port (C++/Windows)

I've been looking for hours for a way to write to a serial port.我一直在寻找一种写入串口的方法。 Every way that I have found I haven't been able to implement.我发现我无法实施的每一种方式。 The program I'm currently using for Recvfrom() functions requires me to disable precomplied headers (I'm not sure if thats an issue).我目前用于 Recvfrom() 函数的程序要求我禁用预编译标头(我不确定这是否是一个问题)。 So what I'm basically asking is, is there a way to transmit a variable through a serial port, and if so what is the easiest way to go about it.所以我基本上要问的是,有没有一种方法可以通过串行端口传输变量,如果有的话,最简单的方法是什么 go 关于它。

Precompiled headers isn't really an issue - if you are including other code directly in your project rather than building a library you might have to add the #include stdaxf.h to that code, or you can simply include the.h and.cpp files directly into your code file.预编译头文件并不是真正的问题 - 如果您直接在项目中包含其他代码而不是构建库,您可能必须将#include stdaxf.h添加到该代码,或者您可以简单地包含.h 和.cpp文件直接写入您的代码文件。

If you want to talk to the serial port directly in win32 it's easy enough.如果你想在 win32 中直接与串口通信,这很容易。 The big issue is handling the threading and waiting for new data to arrive - but if you just want to send and sit there waiting for a response it's easy最大的问题是处理线程并等待新数据到达——但如果你只想发送并坐在那里等待响应,这很容易

You could use qextserialport a library for serial communication with QT.您可以使用 qextserialport 库与 QT 进行串行通信。

Here is the link to the function recvfrom() in the msdn documentation.这是 msdn 文档中 function recvfrom() 的链接。 Also contains a sample.还包含一个示例。 http://msdn.microsoft.com/en-us/library/windows/desktop/ms740120%28v=vs.85%29.aspx I have compiled without any problems in VS2010. http://msdn.microsoft.com/en-us/library/windows/desktop/ms740120%28v=vs.85%29.aspx我在VS2010中编译没有任何问题。

Just make sure that the very first line in your cpp file is只需确保您的 cpp 文件中的第一行是

#include "stdafx.h" 

otherwise you would run into all sorts of compiler and link errors.否则你会遇到各种编译器和链接错误。 Precompiled header is not an issue if you follow what I wrote previously.如果您按照我之前写的,预编译 header 不是问题。

Hope that helps.希望有所帮助。

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

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