read comport

Hi there, I found an example for i/o on a comport, although, I don't believe I'm getting anything on the read side of things, can anybody point me in the right direction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SerialTest.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <windows.h>

#include "Serial.h"
#include "stdafx.h"

using namespace std;

#define RX_BUFFSIZE 20

void printUsage(_TCHAR progName[]);


int _tmain(int argc, _TCHAR* argv[])
{
	if(argc != 2)
	{
		printUsage(argv[0]);

		cout << "press any key and enter to quit" << endl;
		char temp;
		cin >> temp;

		return 10;
	}

		try
	{
		cout << "Opening com port"<< endl;
		tstring commPortName(TEXT("COM1"));
		Serial serial(commPortName, 9600);
		cout << "Port opened" << endl;

		cout << "writing something to the serial port" << endl;
		serial.flush();
		char hi[] = "Hello";
		int bytesWritten = serial.write(hi);
		cout << bytesWritten << " bytes were written to the serial port" << endl;
		if(bytesWritten != sizeof(hi) - 1)
		{
			cout << "Writing to the serial port timed out" << endl;
		}

		char buffer[RX_BUFFSIZE];

		cout << "Reading from the serial port: ";
		for(int i = 0; i < 10; i++)
		{
			int charsRead = serial.read(buffer, RX_BUFFSIZE);
			cout << buffer;
			Sleep(100);
			cout << charsRead;
		}
		cout << endl;

	}catch(const char *msg)
	{
		cout << msg << endl;
	}

	cout << "press any key and enter to quit" << endl;
	char temp;
	cin >> temp;

	return 0;
}

void printUsage(_TCHAR progName[])
{
#if defined(UNICODE)
	wcout << progName << " <comm port>" << endl
		 << "e.g., " << progName << " COM1" << endl;
#else
	cout << progName << " <comm port>" << endl
		 << "e.g., " << progName << " COM1" << endl;
#endif
	
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
serial.h
/** Serial.h
 *
 * A very simple serial port control class that does NOT require MFC/AFX.
 *
 * License: This source code can be used and/or modified without restrictions.
 * It is provided as is and the author disclaims all warranties, expressed 
 * or implied, including, without limitation, the warranties of
 * merchantability and of fitness for any purpose. The user must assume the
 * entire risk of using the Software.
 *
 * @author Hans de Ruiter
 *
 * @version 0.1 -- 28 October 2008
 */

#ifndef __SERIAL_H__
#define __SERIAL_H__

#include <string>
#include <windows.h>

typedef std::basic_string<TCHAR> tstring;

class Serial
{
private:
	HANDLE commHandle;

public:
	Serial(tstring &commPortName, int bitRate = 115200);

	virtual ~Serial();

	/** Writes a NULL terminated string.
	 *
	 * @param buffer the string to send
	 *
	 * @return int the number of characters written
	 */
	int write(const char buffer[]);

	/** Writes a string of bytes to the serial port.
	 *
	 * @param buffer pointer to the buffer containing the bytes
	 * @param buffLen the number of bytes in the buffer
	 *
	 * @return int the number of bytes written
	 */
	int write(const char *buffer, int buffLen);

	/** Reads a string of bytes from the serial port.
	 *
	 * @param buffer pointer to the buffer to be written to
	 * @param buffLen the size of the buffer
	 * @param nullTerminate if set to true it will null terminate the string
	 *
	 * @return int the number of bytes read
	 */
	int read(char *buffer, int buffLen, bool nullTerminate = true);

	/** Flushes everything from the serial port's read buffer
	 */
	void flush();
};

#endif 
]
What is plugged into your port?
ah, nothing. I thought this would talk to itself thru the com port as it were.

do I have any options to emulate writing something to a virtual comport and my app reading it?

I don't have any HW devices and it's for a distant project so I won't ever get to test hands on.

What is the physical port being used for the serial comms? Assuming it is USB, then one way is to get a USB-to RS232 DB9 convertor (preferably with lights). Something like:

https://www.amazon.co.uk/VTOP-Serial-RS232-Cable-Adapter/dp/B01LVYR1AB/ref=sr_1_3

Then get the matching opposite sex (so female in this case) DB9 connector eg

https://www.amazon.co.uk/VTOP-Serial-RS232-Cable-Adapter/dp/B01LVYR1AB/ref=sr_1_3

DB 9 connector, connect together pins 2 & 3 (assuming no hardware flow control). Then whatever is sent to the serial will come back as received. This is what used to be known as a serial loop-back connector.

As the convertor has lights, you can see the serial data being sent and received to help with testing/debugging.
Reading and writing to a COM port requires having an actual device attached, otherwise Windows can't find any active COM ports.*

How to Detect Available COM Ports
https://mariusbancila.ro/blog/2007/08/02/how-to-detect-available-com-ports/

(link found via meta-search: https://duckduckgo.com/?q=c%2B%2B+finding+com+ports&t=ffsb&ia=web)

*Well, obviously Windows "knows" if the PC has COM ports or not, you can see what COM ports are available by the Device Manager app, so it is possible to programmatically to find out what COM ports are available. To actually read and write to those ports requires a device attached, though.

Last edited on
you can talk to yourself via a network port, but not a serial one. Its much faster, too.
> do I have any options to emulate writing something to a virtual comport and my app reading it?
> I don't have any HW devices and it's for a distant project so I won't ever get to test hands on.

Use an RS232 loopback plug.

We can make one ourselves.
https://www.instructables.com/DB-9-RS-232-Loopback-Plug-Windows-and-OS-X/
Thanks guys, so it's a case of twisting some wires together on a rs232, I can manage that
@jonnin If I had a choice I'd use anything else, but it's for very old equipment [1970's radio mixing desk]
BTW, “comport” is a verb meaning “to be in agreement with [something]”: https://ninjawords.com/?q=comport

A COM port (two words, notice the capitalization) A.K.A communication port, is a serial hardware connection interface common to PCs: https://en.wikipedia.org/wiki/COM_(hardware_interface)

IIRC, modern Windows systems require a proper access token before they will let you use one.
Topic archived. No new replies allowed.