Hi,
I`m trying to read Hexnumbers of a text file, bytewise, and send them through a serial port, one after the other. with the same routine I want to receive a message.
The following routine works, reading out the textfile and sending it, but I cant get it to receive.
I also dont understand how it works, it goes through the serialport setup sequence every time and also closes the port after each sent letter.
Despite all that, the terminal that I`m sending the numbers to , reads them correctly.
That`s not the case when I only place the WriteFile() func. in the loop.
Can anyone maybe straighten out my code?
Working code, but doesn`t receive:
Code:
int main()
{
DCB dcb;
HANDLE hCom;
DWORD dwError;
DWORD dwWritten;
BOOL fSuccess;
int rows;
int i=0,j=0;
int x[28];
char buff[8]; // BUFSIZE
unsigned char Msg[128], Data;
FILE *HexCode;
HexCode=fopen("hex.txt","r"); //reads hex.txt
clrscr();
//---------------reading first collumb of text file-------------------------------------
while(i<zeilanz && fgets(buff,sizeof(buff),HexCode)!=NULL )
{
if (sscanf(&buff[0],"%x",&x[i])==1){
// printf("sscanf=%d i=%d x=%x\n", sscanf(buff, "%x", &x[i]), i, x[i]);
i++;
}
}
//------------------ x[] is available ---------------------------------
for(j=0;j<4;j++) // and will be sent through WritwFile()
{
// cout<<"j="<<j<<" x="<<x[j]<<"\n";
//------------------------------------------------------------------------------
hCom=CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,NULL);
//Buffert erst ab hier
//------------------------------------------------------------------------------
if ( hCom == INVALID_HANDLE_VALUE ) // If COMM port can't be opened, then ...
{
dwError = GetLastError(); // Display error code, and abort.
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, dwError, 0, (char *)Msg, 124, 0 );
printf("Cannot Open Port COM1 !\nError Code: %d, - %s.\n", dwError, Msg );
return 1;
}
//------------------------------------------------------------------------------
fSuccess = GetCommState(hCom, &dcb);
dcb.BaudRate=CBR_2400;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
dcb.fDtrControl=DTR_CONTROL_DISABLE;
dcb.fOutxCtsFlow=FALSE;
dcb.fOutxDsrFlow=FALSE;
dcb.fRtsControl=RTS_CONTROL_DISABLE;
fSuccess=SetCommState(hCom, &dcb);
//---------------------- Sendfunc -----------------------------------------
WriteFile(hCom, &x[j], 4, &dwWritten, NULL);
//------------------------------------------------------------------------------
CloseHandle(hCom);
} // close loop
getch(); //wait for display
}