Yes searching for a few books on win32 serial port communication i was able to get a suitable replacement for the bios.h header
But here arises another problem.I'm currently using win 7 64 bit.My program runs fine on my pc and other 64 bit pcs.But when i run the same program on a 32 bit system ,i am able to only send data...but not receive.Why so?
Code:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <string.h>
int main(){
int i; //looping variable
char port[10];
// printf("Enter the com port.");
// scanf("%s",&port);
DCB dcbSerialParams = {0};DWORD dwBytesRead = 0;
char szBuff[25]; //variable to send data
char rzBuff[25] = {0}; //variable to read data
///Code for opening port
HANDLE hSerial;
hSerial = CreateFile("COM1", //enter com port value
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//serial port does not exist. Inform user.
printf("Serial Port does not exist.\n");
}
//some other error occurred. Inform user.
printf("Some other error occured.\n");
}
else{
///End of Code for opening port
///Setting parameters
//DCB dcbSerialParams = {0}; //declared on top
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
//error getting state
printf("error getting state.\n");
}
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams)){
//error setting serial port state
printf("error setting serial port state.\n");
}
///End of setting parameters
///Setting timeouts
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts)){
//error occureed. Inform user
printf("error occured.");
}
///End of Setting timeouts
for(i=1;i <= 9;i++){
///Sending data
//char szBuff[25] = {0}; Declared on top
//printf("Sent data: %s \n",szBuff);
printf("Sent data: \n");
gets(szBuff);
//szBuff[strlen(szBuff)]='\n';
strcat(szBuff,"\n");
if(!WriteFile(hSerial, szBuff, 25, &dwBytesRead, NULL)){
//error occurred. Report to user.
printf("Error occured");
}
///End of sending data
///Reading Data
//char rzBuff[25] = {0}; Declared on top
if(!ReadFile(hSerial, rzBuff, 25, &dwBytesRead, NULL)){
//error occurred. Report to user.
printf("Error occured");
}
else
printf("Read data: %s\n\n",rzBuff);
memset(rzBuff,0,25);
///End of Reading data
}
CloseHandle(hSerial);
}
getch();
return 0;
}