Writing to Serial Port (C++/Windows)

Go To StackoverFlow.com

1

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). 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.

2012-04-05 18:06
by Linked
I think some clarification might be helpful here. recvfrom is used with sockets. To read from the serial port (under Windows) you'd normally use ReadFile or something similar. Are you really dealing with a network connection, a serial port, or (possibly) something like SLIP or PPP that creates a socket connection over a serial port? Also, the title says writing, but recvfrom is for reading - Jerry Coffin 2012-04-05 18:17
I recommend using Boost.ASIO, which has native support for serial ports. Here are some slides from BoostCon 2010 that give an excellent overview of how one should use ASIO properly - ildjarn 2012-04-05 18:19
Sorry the Recvfrom was to receive another variable from a virtual machine sent through across the network - Linked 2012-04-05 18:29


3

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.

If you want to talk to the serial port directly in win32 it's easy enough. 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

2012-04-05 18:14
by Martin Beckett


0

You could use qextserialport a library for serial communication with QT.

2012-04-05 18:32
by David Feurle


0

Here is the link to the function recvfrom() in the msdn documentation. 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.

Just make sure that the very first line in your cpp file is

#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.

Hope that helps.

2012-04-05 19:32
by ervinbosenbacher
Ads