/* c:\l.bat の内容を修正する set PATH=%PATH%;c:\meadow\1.15\bin;c:\cygwin\bin;c:\borland\bcc55\bin; ~~~~~~~~~~~~~~~~~~~~~~ 追加 コンパイル bcc32 -tW -tWM -Ic:\borland\BCC55\include -Lc:\borland\bcc55\LIB test.c */ #include #include #include void _USERENTRY ComLoop( void* a ) ; static void InitCritical( void ) ; void EnterCritical( void ) ; void LeaveCritical( void ) ; int _Cdecl printf(const char* fmt, ...) ; static LRESULT CALLBACK winMainProc(HWND hWnd, UINT msg, UINT wParam, LONG lParam) ; void PurgeCommBuffer( void ); static int CommSet( int ComChannel, int BaudRate ) ; int Send232c( HANDLE Comm, char* Str ) ; char* ReadCommPort( HANDLE CommHandle, int MaxSize ) ; static unsigned char* DeleteWhiteCode( unsigned char* Buffer ) ; int ReadFirstChar( HANDLE CommHandle ) ; short AskAboutSave (HWND hwnd, char *szTitleName) ; void DrawEllipse(HDC hdc, int x1, int y1, int x2, int y2, int Color ) ; void DrawString( HDC hdc, int x, int y, char* String ) ; void RepaintData( HDC hdc, int Data[], int Paint ) ; int isSameData( int LastData[], int Data[] ) ; void CopyData( int Dst[], int Src[] ) ; static CRITICAL_SECTION Cs ; static int isCriticalValidForDebug = 0 ; HANDLE Comm ; OVERLAPPED WOp; OVERLAPPED ROp; int Data[ 3 ] ; /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE dummy_hPrevInst, LPSTR dummy_lpCmdLine, int nCmdShow) { char *app_name = "1st program"; /*プログラムの名前*/ WNDCLASS wndclass; HWND hWnd; MSG msg; /* 多重起動防止. */ CreateMutex(NULL, 1, app_name); if (GetLastError() == ERROR_ALREADY_EXISTS) return 0; /* ウィンドウクラスの登録 */ wndclass.lpszClassName = app_name; wndclass.hCursor = LoadCursor(NULL, IDI_APPLICATION); wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); wndclass.lpszMenuName = NULL; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hInstance = hInst; wndclass.style = 0; wndclass.lpfnWndProc = (WNDPROC)winMainProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; if (RegisterClass(&wndclass) == 0) return 0; InitCritical() ; _beginthread( ComLoop, 16000, NULL ) ; /* メインウィンドウの作成 */ hWnd = CreateWindowEx( 0 , app_name , app_name , WS_OVERLAPPEDWINDOW , CW_USEDEFAULT , CW_USEDEFAULT , 800 , 600 , NULL , NULL , hInst , NULL ); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); /* メインループ */ while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } /* 終了 */ return 0; } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ static LRESULT CALLBACK winMainProc(HWND hWnd, UINT msg, UINT wParam, LONG lParam) { static RECT rect ; static int cxChar, cyChar ; static HDC hdc ; static PAINTSTRUCT ps ; static int i ; static TEXTMETRIC tm ; static HINSTANCE hInst ; static int LastData[ 3 ] ; static int ReadData[ 3 ] ; /* ウィンドウ・メッセージ処理 */ switch (msg) { case WM_CREATE : hInst = ((LPCREATESTRUCT) lParam) -> hInstance ; printf( "WM_CREATE"); hdc = GetDC (hWnd) ; SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight + tm.tmExternalLeading ; ReleaseDC (hWnd, hdc) ; SetTimer(hWnd, 50, 30, NULL); return( 0 ) ; case WM_TIMER: CopyData( ReadData, Data ) ; if( isSameData( LastData, ReadData ) ) { return( 0 ) ; } hdc = GetDC (hWnd) ; RepaintData( hdc, LastData, 0 ) ;//最後のデータを消す RepaintData( hdc, ReadData, 1 ) ;//最新のデータにする CopyData( LastData, Data ) ; ReleaseDC (hWnd, hdc) ; return( 0 ) ; case WM_PAINT : hdc = BeginPaint (hWnd, &ps) ; CopyData( ReadData, Data ) ; RepaintData( hdc, ReadData, 1 ) ;//最新のデータにする //EntryHDC( hdc,"1" ) ; //DisplayOnePage( &Info, DshInfo, Page ) ; //ReleaseHDC("1") ; EndPaint(hWnd, &ps ) ; return( 0 ) ; case WM_CLOSE : if( AskAboutSave (hWnd, NULL) == IDCANCEL ) { DestroyWindow (hWnd) ; } break ; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); } int isSameData( int LastData[], int Data[] ) { int i ; for( i = 0 ; i < 3 ; i ++ ) { if( LastData[ i ] != Data[ i ] ) { return( 0 ) ; } } return( 1 ) ; } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ void _USERENTRY ComLoop( void* a ) { char* Buffer ; char* Param ; int ReadData[ 3 ] ; int i ; static char Token[] = ", \t" ; CommSet( 1, 38400 ) ; for(;;) { Buffer = ReadCommPort( Comm, 100) ; printf( "-->[%s]", Buffer ) ; Param = strtok( Buffer, Token ) ; if( Param == NULL ) { continue ; } ReadData[ 0 ] = atoi( Param ) ; Param = strtok( NULL, Token ) ; if( Param == NULL ) { continue ; } ReadData[ 1 ] = atoi( Param ) ; Param = strtok( NULL, Token ) ; if( Param == NULL ) { continue ; } ReadData[ 2 ] = atoi( Param ) ; CopyData( Data, ReadData ) ; printf( "%d %d %d", Data[ 0 ], Data[ 1 ], Data[ 2 ] ) ; } } void CopyData( int Dst[], int Src[] ) { int i ; EnterCritical() ; for( i = 0 ; i < 3 ; i ++ ) { Dst[ i ] = Src[ i ] ; } LeaveCritical() ; } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ static int CommSet( int ComChannel, int BaudRate ) { DCB dcb; COMMTIMEOUTS comtime; char sPort[ 30 ] ; sprintf( sPort, "\\\\.\\com%d", ComChannel ) ; //printf( "com=[%s]\n", sPort ) ; ZeroMemory(&WOp, sizeof(WOp)); ZeroMemory(&ROp, sizeof(ROp)); WOp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); ROp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); Comm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(Comm <= 0) { fprintf(stderr, "open error!!\n" ); return -1; } SetupComm(Comm,1024,1024); dcb.DCBlength = sizeof(DCB); GetCommState(Comm, &dcb); dcb.BaudRate = BaudRate; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fRtsControl =RTS_CONTROL_DISABLE; dcb.fOutX = 0; dcb.fInX = 0; dcb.fParity = FALSE ; dcb.fBinary = TRUE ; dcb.fOutxCtsFlow = FALSE ; dcb.fOutxDsrFlow = FALSE ; SetCommState(Comm, &dcb); GetCommTimeouts(Comm,&comtime); comtime.ReadIntervalTimeout=100; comtime.ReadTotalTimeoutMultiplier=100; comtime.ReadTotalTimeoutConstant=100; comtime.WriteTotalTimeoutMultiplier= 100 ; comtime.WriteTotalTimeoutConstant=200; SetCommTimeouts(Comm,&comtime); return(0); } int Send232c( HANDLE Comm, char* Str ) { int i ; DWORD cnt ; i = WriteFile( Comm, Str, strlen( Str ), &cnt, &WOp ) ; return( i ) ; } /****************************************************************************** 機能要約 外部タスクが電圧値を読み取る 機能 引き数 戻り値 注意 ******************************************************************************/ char* ReadCommPort( HANDLE CommHandle, int MaxSize ) { static char tmpBuf[ 1000 ] ; DWORD cnt ; int i ; char Data ; tmpBuf[ 0 ] = ReadFirstChar( CommHandle ) ; for( i = 1 ; i < MaxSize ; ) { ReadFile(CommHandle, &Data, 1 , &cnt, &ROp ) ; if( cnt == 0 ) { Sleep( 2 ) ; continue ; } if( ( Data == 10 ) || ( Data == 13 ) ) { // LF tmpBuf[ i ] = 0 ; return( tmpBuf ) ; } tmpBuf[ i ] = Data ; i ++ ; } tmpBuf[ i ] = 0 ; return( tmpBuf ) ; } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ static unsigned char* DeleteWhiteCode( unsigned char* Buffer ) { int i ; //頭のコントロールコード等を取り除く for( i = 0 ; i < 10 ; i ++ ) { if( *Buffer > ' ' ) { break ; } if( *Buffer == 0 ) { return( Buffer ) ; } Buffer ++ ; } //尻尾のコントロールコード等を取り除く for( i = 0 ; i < 10 ; i ++ ) { if( Buffer[ i ] < ' ' ) { Buffer[ i ] = 0 ; return( Buffer ) ; } } return( Buffer ) ; } int ReadFirstChar( HANDLE CommHandle ) { char tmpBuf[ 10 ] ; int Data ; DWORD cnt ; int i ; while( 1 ) { ReadFile(CommHandle, tmpBuf, 1 , &cnt, &ROp ) ; if( cnt ) { Data = *tmpBuf ; Data &= 0xff ; if( Data > ' ' ) { return( Data ) ; } } else { Sleep( 2 ) ; } } } void PurgeCommBuffer( void ) { PurgeComm( Comm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ; } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ static void InitCritical( void ) { isCriticalValidForDebug = 1 ; InitializeCriticalSection( &Cs ) ; } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ void EnterCritical( void ) { if( isCriticalValidForDebug ) { EnterCriticalSection( &Cs ) ; } } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ void LeaveCritical( void ) { if( isCriticalValidForDebug ) { LeaveCriticalSection( &Cs ) ; } } /****************************************************************************** 機能要約 機能 引き数 戻り値 注意 ******************************************************************************/ int _Cdecl printf(const char* fmt, ...) { static char Buffer[ 1000 ] ; va_list argptr; int cnt; EnterCritical() ; va_start(argptr, fmt ); cnt = vsprintf( Buffer, fmt, argptr ) ; va_end(argptr); OutputDebugString( Buffer ) ; LeaveCritical() ; return(cnt); } short AskAboutSave (HWND hwnd, char *szTitleName) { int iReturn ; iReturn = MessageBox (hwnd, "終了しますか?", "test" , MB_YESNOCANCEL | MB_ICONQUESTION) ; if (iReturn == IDYES) { return( IDCANCEL ) ; } else { return( !IDCANCEL ) ; } } void RepaintData( HDC hdc, int Data[], int Paint ) { int x1, y1, x2, y2 ; char Buffer[ 100 ] ; x1 = Data[ 0 ] + 100 ; y1 = 300 + 100 ; x2 = Data[ 0 ] + ( Data[ 1 ] / 2 ) ; y2 = y1 + 100 ; DrawEllipse( hdc, x1, y1, x2, y2, Paint ) ; sprintf( Buffer, "%d,%d,%d ", Data[ 0 ], Data[ 1 ], Data[ 2 ] ) ; DrawString( hdc, 0, 0, Buffer ) ; } void DrawEllipse(HDC hdc, int x1, int y1, int x2, int y2, int Color ) { HPEN hPen, hOldPen; HBRUSH hBrush, hOldBrush; if( Color ) { hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); hBrush = GetStockObject(BLACK_BRUSH); } else { hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255)); hBrush = GetStockObject(WHITE_BRUSH); } //printf( "%d, %d, %d, %d", x1, y1, x2, y2 ) ; hOldPen = SelectObject(hdc, hPen); hOldBrush = SelectObject(hdc, hBrush); Ellipse(hdc, x1, y1, x2, y2); SelectObject(hdc, hOldPen); SelectObject(hdc, hOldBrush); DeleteObject(hPen); return; } void DrawString( HDC hdc, int x, int y, char* String ) { HPEN hPen, hOldPen; int Length ; SetBkMode (hdc, OPAQUE) ; hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); hOldPen = SelectObject(hdc, hPen); SelectObject(hdc, hOldPen); Length = strlen( String ) ; TextOut( hdc, x, y, String, Length ) ; }