wschalch
September 3rd, 2004, 10:08 AM
Hello,
I have in my application a worker thread that keeps reading the serial port through usual ReadFile() method in an infinite loop. I have also a timer that schedule some jobs to write to the serial port.
My problem:
1º) When I remove the periodic serial port reading operation ( ReadFile() ), the timer is precise in its wake-up calls;
2º) When I activate the periodic serial port reading operation, the timer goes crazy.
I don't know if the ReadFile() operation interfers in the WriteFile() inside the timer or the ReadFile() operation interfers with Windows processing, causing the timer to go crazy.
Does anyone have any clue and/or suggestions???
Thanks in advance,
Wagner
kathir4
September 5th, 2004, 10:01 PM
Hello
If u send the code i can give suggestions. It is better use Common call back functions. here is the code which works very well. it dials ISP no 172233 via modem connected to COM port 1 and gets Ok from modem
To change com port use the follwing function in the program
handle= CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE,1,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL
To change the baud rate use the following function in the program
dcb.BaudRate = 14400 ;
My email id is kathir4@sancharnet.in
PROGRAM
#include <windows.h>
#include <stdio.h>
void abcchar(unsigned char a)
{
FILE * fp;
fp=fopen("c:\\z.txt","a+");
fprintf(fp,"%x..%d..%c\n",a,a,a);
fclose(fp);
}
HDC hdc ;
HANDLE handle ;
HWND hwnd ;
int column, row, xwidth, yheight ;
long threadid, bytesrecd, byteswritten;
long _stdcall wndcallback(HWND wnd,unsigned int umsg,unsigned int wparam,long lparam);
DCB dcb;
WNDCLASS wndclass;
MSG msg;
char writestr[200];char ctemp;
long _stdcall commcallback()
{
unsigned char readstr[2];
while (1)
{ ReadFile(handle,readstr,1,(unsigned long *)&bytesrecd,0);
abcchar(readstr[0]);
if (readstr[0] == 13 )
column = 0 ;
if (readstr[0] == 10)
row++;
if (readstr[0] == 0x08)
column -- ;
if (readstr[0] != 0x0a && readstr[0] != 0x0d)
{
TextOut(hdc,column*xwidth,row*yheight,(const char *)readstr,1);
if (column < 79)
column++ ;
else
{
column = 0;
row++;
}
}
}
return 1;
}
int _stdcall WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance,LPSTR lpszcmdline, int ncmdshow )
{
wndclass.lpfnWndProc=wndcallback;
wndclass.hInstance=hinstance;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); ;
wndclass.lpszClassName="wclass";
RegisterClass(&wndclass);
hwnd=CreateWindow("wclass","hi",WS_OVERLAPPEDWINDOW,0,0,0,0,0,0,hinstance,0);
xwidth = 8;
yheight = 19;
ShowWindow( hwnd, 3 ) ;
hdc=GetDC(hwnd);
handle= CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE,1,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );
dcb.DCBlength = sizeof( DCB ) ;
GetCommState( handle, &dcb ) ;
dcb.BaudRate = 14400 ;
SetCommState( handle, &dcb ) ;
CreateThread(0,0,(LPTHREAD_START_ROUTINE) commcallback,0,0, (unsigned long *)&threadid );
strcpy(writestr,"atdt172233\r");
WriteFile(handle,writestr,strlen(writestr),(unsigned long *)&byteswritten,0) ;
while (GetMessage( &msg, 0, 0, 0 ))
{
TranslateMessage( &msg ) ;
DispatchMessage( &msg ) ;
}
return 0 ;
}
long _stdcall wndcallback(HWND wnd,unsigned int umsg,unsigned int wparam,long lparam)
{
if(umsg == WM_CHAR)
{
ctemp = wparam ;
// WriteFile(handle,writestr,1,(unsigned long *)&byteswritten,0) ;
WriteFile(handle,&ctemp,1,(unsigned long *)&byteswritten,0) ;
}
if(umsg == WM_DESTROY)
{
ReleaseDC(wnd,hdc);
PostQuitMessage( 0 ) ;
}
return( DefWindowProc( wnd, umsg, wparam, lparam ) ) ;
}