How to send a 'string' to a port ?
-
I want to send a Command string to the modem port (COM3). I have created a file with CreateFile() then what should be the parameters to the WriteFile() function to send a string to the COM3 port ... ?
Apurv
Have you tried passing the file name as "COM3" (no extension, no root directory or absolute path required). That must give you direct access to the port. Besides that, what is attached at COM3 port? Are you aware that some devices might require you to follow a specific text pattern (usually a series of ASCII keywords) if you want to send it a command? For instance, a printer usually follows the EPSON escape code sequences. I think you need to try specifying the file name as "COM3", as I already said and if it doesn't give you the desired results, you will need to provide more details.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
-
Have you tried passing the file name as "COM3" (no extension, no root directory or absolute path required). That must give you direct access to the port. Besides that, what is attached at COM3 port? Are you aware that some devices might require you to follow a specific text pattern (usually a series of ASCII keywords) if you want to send it a command? For instance, a printer usually follows the EPSON escape code sequences. I think you need to try specifying the file name as "COM3", as I already said and if it doesn't give you the desired results, you will need to provide more details.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
i m configuring the COM3 port as follows ...
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
DWORD dwBytesWritten = 0;
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.// SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR\_57600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
Apurv
-
Have you tried passing the file name as "COM3" (no extension, no root directory or absolute path required). That must give you direct access to the port. Besides that, what is attached at COM3 port? Are you aware that some devices might require you to follow a specific text pattern (usually a series of ASCII keywords) if you want to send it a command? For instance, a printer usually follows the EPSON escape code sequences. I think you need to try specifying the file name as "COM3", as I already said and if it doesn't give you the desired results, you will need to provide more details.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
i m configuring the COM3 port as follows ... and it is getting configured correctly ....
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
DWORD dwBytesWritten = 0;
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.// SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR\_57600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
Apurv
-
i m configuring the COM3 port as follows ... and it is getting configured correctly ....
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
DWORD dwBytesWritten = 0;
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.// SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR\_57600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
Apurv
Good. Now, you can treat it as a file and write your commands as if you were writing it into a file. You may use WriteFile[^] for instance. Or you have a trouble somewhere else? :)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]