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
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