I am working on a code where I need to simulate a character string on the serial port com1. (/dev/ttyS0).
I have a WinCom aplication which helps me to read the output from an Enigma receiver.
This receiver sends out signal strings like this:

5012 444418E13001000<DC4> // signal
@ <DC4> // heart beat

This string is interpreted by an aplication which reads on its serial port the signal string.
If this string is properly formated the application shows its meaning.

The problem is that when I try to replace the receiver which sends this output with my simulator application it does not understand the string.

The application is very simple:

int open_port(void)
	int fd; /* File descriptor for the port */

	fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1)
	* Could not open the port.

	perror("open_port: Unable to open /dev/ttyS0 - ");
	fcntl(fd, F_SETFL, 0);

	return (fd);

int main()
	cout << "Simulating output." << endl << endl;

	int status;
	int n;

	char EventString[27] = {"5012 444418E130010020<DC4>"};
	EventString[26] = 0x0d;   // *
	EventString[27] = 0x0a;   // *

	status = open_port();
	n = write(status, EventString, 27);
	if (n < 0)
	fputs("write() of 27 bytes failed!\n", stderr);
	cout << "Alarm signal: 5012 444418E130010020" << endl << endl;


I cache the output of this receiver with my WinCom serial port reader program and open it in KHexEdit, binary editor. After the character string there are to more characters 0x0d and 0x0a (in hex).
In the commented lines in the code I tried to put those characters into the output string on my serial port.
But when I inserted the EventString[26] and [27] it generates output like this at the end of the string <DC4><CR> and the 0x0d and 0x0a after the carridge return.

Can be the <DC4> a special character like the <CR>.

this code:

char EventString[27] = {"5012 444418E130010020<DC4>"};
	EventString[26] = 0x0d;   // *
	EventString[27] = 0x0a;   // *

Is wrong. You can't write data to the element 27 of your array. You declared your array to have 27 elements, which is EventString[0]-EventString[26]. So the text can only occupy element 0-25 because element 26 has to be the 'end of string char' ( == '\0').

So you're writing in memory that is yours. I'm surprised this program didn't crash

This article has been dead for over six months. Start a new discussion instead.