Testing Serial port application - c++

I have a code to send data to serial port. I just want to make sure that it is sending the data properly. I checked the function's return value and the number of bytes written and it is success. Is there any other way to actually see the data whatever i am writing?
if(WriteFile(m_hSerialComm, pszBuf, dwSize, &dwNumberOfBytesWritten, NULL) != 0)
I tried to use "Advanced Terminal Port software"
but the data is not coming in that window.

There are several ways to test your software. If you have two serial ports then connect them with a cable and listen on the other port with a terminal application such as the one you mentioned. Otherwise, you could loop back on the same port by connecting pins 2 and 3 together. A hardware-free option would be to use virtual serial ports as provided by tools like com0com.

Assuming from your piece of code that you are developing on a Microsoft Windows operating system, I would recommend the Portmon for Windows "official" serial port monitoring utility. I have used it in in the past, and found it simple enough, and also quite useful specifically for its multiple filtering/search options (since sometimes the amount of data passed on your serial port is huge).
If all you want is a log of the data you have written to your own port, why not encapsulate your WriteFile (and maybe also your ReadFile) functions in some "utility" function(s) that reads/writes both on your serial port and in some log file? You could even add timestamps, and filter "whatever you are looking for" straight from your own code.
I found this last option really useful when remotely debugging applications with customers. You add a button in your application that toggles the logging on and off, then you simply have to ask your customer to hit the "log" button and to send you the results.

Related

Wemos D1 Mini (ESP8266) control with USB

I have a Wemos D1 Mini (ESP8266), flashed with WLED and I want to create an application like .exe and have control on turning it on/off and choose colors. Does anyone have any idea how I can do without to connect on wifi and just do everything via USB?
To control the WLED firmware over USB, you would use the firmware's ability to be controlled over a serial connection. It looks like you would open the virtual serial port that appears when you plug in the device, at a speed of 115200, and then you take the same JSON that you would POST to /json/state for controlling it over WiFi, and instead send it over the serial connection, and get your responses back over the serial connection.
You could also implement the TPM2 protocol, or the Adalight protocol (which doesn't really seem to be documented except in code), as those are also supposed to be supported.
If you want to do all this in C++ on Windows, you might start by reading the (very old) Windows tutorials for Win32 API serial port programming. If you only want to target Win10 or newer, you could learn C++/WinRT and then use the new WinRT serial APIs. Or you could consult the answers to this question about serial programming on Windows.

Finding out comm port settings of a running app

The situation is as follows: I have a piece of hardware connected to a, windows running, PC via a serial port. This custom hardware is used to interface other hardware through GPIB. Now, the software that is used to operate this setup on the PC side needs to be changed without touching the hardware in the middle. The problem is getting a hold of the proper serial comm. parameters used for communication - setting them (the timeouts in particular - baud rates and bits are fine) to arbitrary values in the new software leads to a lot of comm errors so I'd like to know them precisely. The function GetCommTimeouts() would do the job, however you need the handle to the comm. device aquired via call to CreateFile() - only the original software has access to it. So the question is if one can get a hold of these settings from outside of the running, old app?
Thanks,
drinker
I would suggest using a tool like Process Monitor. This tool can monitor the calls that are used to open and configure a serial port.

Modifying windows TCP/IP responses

I have written a small program that returns a custom response to receiving SYN packets to some ports in Linux. This required me to make a minor edit to the Linux Kernel and recompile. I have tested this and i have the functionality i require in Linux.
I wish to have the same functionality in Windows XP/7. To achieve this i believe i would need to edit a driver/file/registry setting that controls tcpip functions. The goal would be to disable the default action of sending RST packets when a SYN is received on a closed port?
The research i have done so far has pointed me towards something like tcpip.sys or the tcpip settings in registry?
I am realistic that this may not be possible due to the drivers being unsigned if i modify them, but hopefully someone can put my mind at ease about the plausibility of this being possible?

Determine is a Serial Port exists, Linux C/C++

My development target is a Linux computer that has two physical serial ports located at /dev/ttyS0 and /dev/ttyS1. I also expect /dev/ttyS2 and /dev/ttyS3 to be defined.
Using stty -f /dev/ttyS0 and S1 reports the configuration of the two serial ports and reports something menaing "doesn't exist" for S2 and S3.
The hardware designers are talking about offering USB to Serial ports built onto the main board. They'll be DB9 connectors on the outside and just circuitry - no USB connectors on the inside. The number of USB-to-serial connections is not guaranteed and I know enough to design for "many" instead of one.
So, in setting up my port server daemon, I need to be able to determine which ttyS's and which ttyUSB's are "real" and which aren't. Will there ever be placeholdeer ttyUSB's? What if one were to be "unplugged" (say it was, indeed, a real USB coupler on the inside of the PC)?
Is there a better approach than popen()ing stty and examining its output to determine the status of the serial ports? Is there a C API for stty?
Thanks!
The "C-API" which stty uses is tcsetattr(3) and tcgetattr(3).
For finding TTYs without opening the device you may look at this question:
How to find all serial devices (ttyS, ttyUSB, ..) on Linux without opening them?

Windows: C++: Redirection from file to serial port (input)

I have inherited some rather old software which connects to a serial port, formats the incoming data and displays and saves it. The software is written in an old Borland compiler (C++ Builder 5.0), and uses CreateFile to open the port, followed by SetupComm for the comm parameters.
Someone has asked me to add new functionality: Enable taking a recorded session, and replaying it as though it was coming in on the serial port.
I'm looking at several possible solutions, the easiest and quickest involving a second serial port and a null modem cable. This isn't exactly what they had in mind, but it does solve the problem. However, assume I don't have this option. Is there any way to redirect a comm port to a file, so that when the "com port" was opened it would open the file and read from it? Or is there a simple parallel way to open the file (the same call to CreateFile, for example), and bypassing the call to SetupComm so that the input would come from file if desired?
Since I do have the simple HW solution, I don't want to invest hours rewriting the code, but if there is a simple way of going about it, I'd like to know.
I'll award somehow the reputation points to Eugen Rieck, the suggestion was his. The way I did this was with a package called com0com, which is an open source virtual com port. I wrote a routine which sends to one virtual com port, connected the com port of the original software to another one paired with that one, and the funcationality is there.