Win32 SetupComm function throwing media is write protected error

1

I am writing a program in C++ to read and write from a serial port.

One issue that I have not been able to fix is, after calling SetupComm it fails.

Using GetLastError() tells me that it was error 0x00000013, "The media is write-protected" is the definition of that error code.

Googling what that means has lead me nowhere in terms of programming. I keep finding results pointing to blogs over harddrives refusing to let the user write and stuff like that. None have been programming based results.

One additional, interesting piece of information, is despite SetupComm failing I can still successfully write to the serial port. I just can't read from it.

Here is the code I have for opening the connection, up until I call SetupComm:

string s = "com1";
LPCSTR port = s.c_str();
DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
DWORD dwShareMode = 0;
LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
DWORD dwCreationDistribution = OPEN_EXISTING;
DWORD dwFlagsAndAttributes = FILE_FLAG_OVERLAPPED;
HANDLE hTemplateFile = NULL;

handle = CreateFile(port, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDistribution, dwFlagsAndAttributes, hTemplateFile);
bufferSizeReceive = 16;
bufferSizeTransmit = 16;

BOOL success = SetupComm(handle, bufferSizeReceive, bufferSizeTransmit);
c++
winapi
serial-port
asked on Stack Overflow Oct 1, 2015 by Carson • edited Oct 1, 2015 by theB

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0