Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to Com1

Status
Not open for further replies.

sweep123

Technical User
May 1, 2003
185
GB
I have been trying to use this section of code to sent data to my old RS232 printer/console. But the device only prints the data when the program terminates.

What I would like to do is send data then receive data, but currently have to terminate the program to have the data sent.

Code listing below:-
//init the variables first
DWORD cbWritten; // No of bytes written
char mystring[255]; //string variable
int len; //Init the len variable for use of sizing our string
int error;
//set the handle for the CreateFile function
HANDLE hCom = CreateFile( "COM1:",GENERIC_WRITE | GENERIC_READ ,0, 0, OPEN_EXISTING, 0, NULL);
//Init the settings for the DCB (device control block) structure
DCB dcb; //Init the device control block
if(GetCommState(hCom, &dcb) == false)
printf("GetCommState Failed \n");
else
printf("GetCommState OK \n");
dcb.BaudRate = 300; //set baud rate
dcb.ByteSize = 8; //Set Bytes
dcb.Parity = NOPARITY; //Set Parity
dcb.StopBits = ONESTOPBIT; //Set stop bit
//Set the port state
error = SetCommState(hCom, &dcb);
if(error != 0)
{
printf("Error Sending Data %d \t", GetLastError());
DisplayErrorText(error);
}
else
printf("State Set OK \n");

//get and size what's gotten
printf("Enter string: ");
gets(mystring); //Get input from keyboard
len = strlen(mystring); //get true length of string
//Output to the com port
error = WriteFile(hCom, mystring, len, &cbWritten, NULL); //write mystring to port
printf("Error cbWritten = %d \n", cbWritten);
if(error == false)
{
if( cbWritten != len) printf("Error cbWritten = %d \n", cbWritten);
printf("Error Sending Data %d \t", GetLastError());
DisplayErrorText(error);
}
error = WriteFile(hCom, "\r\n", 2, &cbWritten, NULL); //write return and newline to port
if(error == false)
{
printf("Error Sending CR/LF %d \t", GetLastError());
DisplayErrorText(error);
}
if(FlushFileBuffers(hCom) == 0)
{
printf("Faied To Flush Buffers \n");
}
printf("Enter Another string: ");
gets(mystring); //Get input from keyboard
len = strlen(mystring); //get true length of string
//Output to the com port
error = WriteFile(hCom, mystring, len, &cbWritten, NULL); //write mystring to port
printf("Error cbWritten = %d \n", cbWritten);
if(error == false)
{
if( cbWritten != len) printf("Error cbWritten = %d \n", cbWritten);
printf("Error Sending Data %d \t", GetLastError());
DisplayErrorText(error);
}
error = WriteFile(hCom, "\r\n", 2, &cbWritten, NULL); //write return and newline to port
if(error == false)
{
printf("Error Sending CR/LF %d \t", GetLastError());
DisplayErrorText(error);
}

printf("Enter Key to Close: ");
gets(mystring); //Get input from keyboard
CloseHandle(hCom); //close the handle
return 0; //exit
 
It seems you have Windows spooling (not a C/C++) problem. The Windows collects app output then do planning printer queue and sends collected data to the printer.
Try disable all spooling and print directly to the port (change printer properties).
See Windows help (or MSDN articles)...
 
Thanks, I had set up the console/printer as a generic priter just to test if the port was working.

Now removed the printer, but dont get any output now.
 
As far as I know you may only set "Print directly to printer" configuration for the printer. No need to change the printer driver.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top