The ultimate goal is to send simple messages ("GH", "HC", etc.) to a CMUcam2 and to read what messages I get back. I need help in setting up a serial communications port in Windows. I have seen how to open the port on other threads on this website, but I can't figure out how to write to or read from the serial port. Also how can I see what I am reading and writing to the serial port to verify that the information being transmitted is correct, hyperterminal perhaps? Also I am using Dev C++ to do the programming.

Recommended Answers

All 3 Replies

I believe I have the port open, now I am trying to read and write. I have the read and write function set up like this.

BOOL WINAPI ReadFile(
           HANDLE hFile,
           LPVOID lpBuffer,
           DWORD nNumberOfBytesToRead,
           LPDWORD lpNumberOfBytesRead,
           LPOVERLAPPED lpOverlapped
);

BOOL WINAPI WriteFile(
            HANDLE hFile,
            LPCVOID lpBuffer,
            DWORD nNumberOfBytesToWrite,
            LPDWORD lpNumberOfBytesWritten,
            LPOVERLAPPED lpOverlapped
);


DWORD Write(char*data, int length)
	{
		DWORD written_length;
		WriteFile(hCom, data, length, &written_length, NULL);
		return(written_length);
	}  
	
	DWORD Read(char *buffer, int buffer_length)
	{
		DWORD length = 0;
		if (ReadFile(hCom,  // handle of file to read
			buffer,
			buffer_length-1,	// number of bytes to read
			&length,			// pointer to number of bytes read
			NULL) == 0)			// pointer to structure for data
		{
			return 0;
		}
		return(length);
	}

I am also trying to print out what is read using the printf function to confirm that it is working properly but I am having problems getting the read and write functions working, any suggestions?
(Sorry if the syntax is improper in the thread, I'm trying to learn this site still)

Reads are normally done in another thread so that it can block while waiting for incoming data. If you don't do that then your program is highly likely to miss data. Follow the links in MSDN for CreateFile() and it will tell you how to read/write serial communications.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.