/*********************************************************************** 2011 (C) Alex Dobrianski Camera module controller test module This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see ************************************************************************/ /*********************************************************************** see www.adobri.com for communication protocol spec ************************************************************************/ #include "stdafx.h" #define _USE_MATH_DEFINES 1 #include "math.h" #include "cm_test.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // The one and only application object CWinApp theApp; using namespace std; BOOL WriteFileCom( HANDLE hFile, // handle to file to write to LPCVOID lpBuffer, // pointer to data to write to file DWORD nNumberOfBytesToWrite, // number of bytes to write LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written LPOVERLAPPED lpOverlapped // pointer to structure for overlapped I/O ) { if (WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped)) { FlushFileBuffers(hFile ); return TRUE; } else { //for (int iatt=0; iatt<5; iatt++) { DWORD dwErr = GetLastError(); if (PurgeComm( hFile, PURGE_TXCLEAR | PURGE_RXCLEAR )) { if (WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped)) { FlushFileBuffers(hFile ); return TRUE; } } } } return FALSE; } BOOL ReadFileCom( HANDLE hFile, // handle to file to write to LPCVOID lpBuffer, // pointer to data to write to file DWORD nNumberOfBytesToRead, // number of bytes to write LPDWORD lpNumberOfBytesRead, // pointer to number of bytes written LPOVERLAPPED lpOverlapped // pointer to structure for overlapped I/O ) { if (ReadFile(hFile, (LPVOID)lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped)) { //FlushFileBuffers(hFile ); return TRUE; } else { //for (int iatt=0; iatt<5; iatt++) { DWORD dwErr = GetLastError(); if (PurgeComm( hFile, PURGE_TXCLEAR | PURGE_RXCLEAR )) { if (ReadFile(hFile, (LPVOID)lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped)) { //FlushFileBuffers(hFile ); return TRUE; } } } } return FALSE; } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; FILE *DatOut = NULL;// = fopen("DatOut.txt","a"); int FlagFirst; int LenFile = 0; HMODULE hModule = ::GetModuleHandle(NULL); if (hModule != NULL) { // initialize MFC and print and error on failure if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { // TODO: code your application's behavior here. nRetCode = 1; int iHCounter = 0; HANDLE hComHandle; char szSwitchCom[MAX_PATH]= {"com3"}; COMMTIMEOUTS ct; DWORD EvtMask = EV_RXCHAR | EV_ERR; DCB m_dcb; HANDLE hX10ComHandle = NULL; DWORD lenwrt; char MyByte; unsigned char bBuffer[1024]; hComHandle = CreateFile(szSwitchCom, GENERIC_READ | GENERIC_WRITE, 0, // exclusive access NULL, // no security attributes OPEN_EXISTING, NULL, //FILE_FLAG_OVERLAPPED, // overlapped I/O NULL); if (INVALID_HANDLE_VALUE != hComHandle) { // set up read/write timeouts GetCommTimeouts( hComHandle, &ct ); ct.ReadIntervalTimeout = MAXDWORD; ct.ReadTotalTimeoutMultiplier = MAXDWORD; ct.ReadTotalTimeoutConstant = 100; ct.WriteTotalTimeoutMultiplier = 5; ct.WriteTotalTimeoutConstant = 1000; SetCommTimeouts( hComHandle, &ct ); // get default parameters m_dcb.DCBlength = sizeof( m_dcb ); if ( GetCommState( hComHandle, &m_dcb ) ) { // parity is configurable, but we only support 8,N,1 m_dcb.BaudRate = CBR_38400;//CBR_9600; m_dcb.ByteSize = 8; m_dcb.Parity = NOPARITY; m_dcb.StopBits = ONESTOPBIT; m_dcb.fBinary = TRUE; // binary data m_dcb.fParity = FALSE;//TRUE; // enable parity checking m_dcb.fOutxCtsFlow = FALSE;//TRUE; // cts flow control when sending data m_dcb.fOutxDsrFlow = FALSE; // DSR not required from modem m_dcb.fRtsControl = RTS_CONTROL_DISABLE;//RTS_CONTROL_ENABLE;//RTS_CONTROL_DISABLE;//RTS_CONTROL_HANDSHAKE; // rts flow control when receiving data m_dcb.fDtrControl = DTR_CONTROL_DISABLE;//DTR_CONTROL_ENABLE; // DTR required by modem m_dcb.fDsrSensitivity = FALSE; // DSR not required from modem m_dcb.fOutX = FALSE; // no xon/xoff flow controll m_dcb.fInX = FALSE; m_dcb.fTXContinueOnXoff = FALSE; m_dcb.fErrorChar = FALSE; // don't replace bad chars m_dcb.fNull = FALSE; // don't process null chars m_dcb.fAbortOnError = FALSE;//TRUE; // handle errors m_dcb.ErrorChar= 0xff; // error replacement character SetCommState( hComHandle, &m_dcb ); SetCommMask( hComHandle, EvtMask); EscapeCommFunction( hComHandle,CLRDTR); EscapeCommFunction( hComHandle,SETRTS); memset(bBuffer, 0, sizeof(bBuffer)); //#define DAC_OK "1?1" // WriteFileCom( hComHandle, DAC_OK, sizeof(DAC_OK)-1, &lenwrt, NULL ); // Sleep(100); // ReadFileCom( hComHandle, bBurrrrrrrrrrrrrrrffer, sizeof(bBuffer), &lenwrt, NULL ); // printf("=%s", &bBuffer[0]); while(1) { MyByte = getchar(); if(MyByte == 'q') { break; } else if(MyByte == 'R') { #define INIT_CAMERA "\x56\x00\x26\x00" WriteFileCom( hComHandle, INIT_CAMERA, sizeof(INIT_CAMERA)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); } else if(MyByte == 'P') { #define TAKE_PICTURE "\x56\x00\x36\x01\x00" WriteFileCom( hComHandle, TAKE_PICTURE, sizeof(TAKE_PICTURE)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); } else if(MyByte == 'L') { #define GET_PICTURE_LEN "\x56\x00\x34\x01\x00" WriteFileCom( hComHandle, GET_PICTURE_LEN, sizeof(GET_PICTURE_LEN)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); LenFile = (((unsigned int)(bBuffer[7])) << 8) | ((unsigned int)(bBuffer[8])); } else if(MyByte == 'B') { #define PICTURE_640_480 "\x56\x00\x31\x05\x04\x01\x00\x19\x00" WriteFileCom( hComHandle, PICTURE_640_480, sizeof(PICTURE_640_480)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); } else if(MyByte == 'M') { #define PICTURE_320_240 "\x56\x00\x31\x05\x04\x01\x00\x19\x11" WriteFileCom( hComHandle, PICTURE_320_240, sizeof(PICTURE_320_240)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); } else if(MyByte == 'S') { #define PICTURE_160_120 "\x56\x00\x31\x05\x04\x01\x00\x19\x22" WriteFileCom( hComHandle, PICTURE_160_120, sizeof(PICTURE_160_120)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); } else if(MyByte == 'A') { #define PICTURE_A "\x56\x00\x31\x05\x01\x01\x12\x04\x36" WriteFileCom( hComHandle, PICTURE_A, sizeof(PICTURE_A)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); } else if(MyByte == 'a') { #define PICTURE_a "\x56\x00\x31\x05\x01\x01\x12\x04\x20" WriteFileCom( hComHandle, PICTURE_a, sizeof(PICTURE_a)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); Sleep(1000); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); } else if(MyByte == 'G') { #define GET_PICTURE "\x56\x00\x32\x0c\x00\x0a\x00\x00\x00\x00\x00\x00" #define GET_PICTURE2 "\x00\x0a" //56 00 32 0C 00 0A 00 00 MH ML 00 00 KH KL XX XX FlagFirst = 1; WriteFileCom( hComHandle, GET_PICTURE, sizeof(GET_PICTURE)-1, &lenwrt, NULL ); bBuffer[0] = LenFile>>8; bBuffer[1] = LenFile&0x00ff; WriteFileCom( hComHandle, &bBuffer[0], 2, &lenwrt, NULL ); WriteFileCom( hComHandle, GET_PICTURE2, sizeof(GET_PICTURE2)-1, &lenwrt, NULL ); lenwrt = 1; while(lenwrt) { memset(bBuffer, 0, sizeof(bBuffer)); Sleep(100); ReadFileCom( hComHandle, &bBuffer, sizeof(bBuffer), &lenwrt, NULL ); if (FlagFirst == 1) { FlagFirst = 0; for (int i = 0 ; i < lenwrt; i++) { if ((bBuffer[i] == 0xff) && (bBuffer[i+1] == 0xd8)) { DatOut = fopen("DatOut.jpg","wb"); if (DatOut != NULL) { fwrite(&bBuffer[i],1,lenwrt - i,DatOut); } } } } else { int iSearch = 0; for (int i = 0 ; i < lenwrt; i++) { if ((bBuffer[i] == 0xff) && (bBuffer[i+1] == 0xd9)) { iSearch = i; break; } } if (DatOut != NULL) { if (iSearch) { fwrite(bBuffer,1,iSearch+2,DatOut); fclose(DatOut); DatOut = NULL; printf("\n =========================file done"); } else { if (lenwrt) fwrite(bBuffer,1,lenwrt,DatOut); } } } printf("\n bytes read =%d ===>\n%s\n<===",lenwrt,bBuffer); if (lenwrt == 0) { if (DatOut) { fclose(DatOut); DatOut = NULL; } } //if (DatOut) // lenwrt = 1; } } } } CloseHandle(hComHandle); hComHandle = NULL; } } } else { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: GetModuleHandle failed\n")); nRetCode = 1; } //fclose(DatOut); return nRetCode; }