// dac_adc.cpp : Defines the entry point for the console application. // #include "stdafx.h" #define _USE_MATH_DEFINES 1 #include "math.h" #include "dac_adc.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 = fopen("DatOut.txt","a"); 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_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 == ';') { #define INIT_DAC_ADC "1wcr2wcrR2RVC2C" WriteFileCom( hComHandle, INIT_DAC_ADC, sizeof(INIT_DAC_ADC)-1, &lenwrt, NULL ); } else if(MyByte == '\'') { #define INIT_DAC_ADC_DONE "1wcr2wcrR2RVC2C" WriteFileCom( hComHandle, INIT_DAC_ADC_DONE, sizeof(INIT_DAC_ADC_DONE)-1, &lenwrt, NULL ); } else if(MyByte == 'i') { //#define INIT_DAC "1w1c1r1R1C" //#define INIT1_DAC "1w1c1r" //#define INIT2_DAC "1C1R" #define INIT_DAC "wcrRC" #define INIT1_DAC "wcr" #define INIT2_DAC "RC" WriteFileCom( hComHandle, INIT1_DAC, sizeof(INIT1_DAC)-1, &lenwrt, NULL ); //Sleep(1000); WriteFileCom( hComHandle, INIT2_DAC, sizeof(INIT2_DAC)-1, &lenwrt, NULL ); } else if(MyByte == 'I') { iHCounter =0; #define INIT_ADC "wcrRVC" #define INIT1_ADC "wcr" #define INIT2_ADC "RVC" WriteFileCom( hComHandle, INIT1_ADC, sizeof(INIT1_ADC)-1, &lenwrt, NULL ); //Sleep(1000); WriteFileCom( hComHandle, INIT2_ADC, sizeof(INIT2_ADC)-1, &lenwrt, NULL ); } else if(MyByte == '1') { iHCounter = 0; //#define OSC1_DAC "1o1O" #define ONE_DAC "1" WriteFileCom( hComHandle, ONE_DAC, sizeof(ONE_DAC)-1, &lenwrt, NULL ); } else if(MyByte == '2') { //#define OSC1_DAC "1o1O" #define TWO_DAC "2" WriteFileCom( hComHandle, TWO_DAC, sizeof(TWO_DAC)-1, &lenwrt, NULL ); } else if(MyByte == 'o') { //#define OSC1_DAC "1o1O" #define OSC1_DAC "oO" WriteFileCom( hComHandle, OSC1_DAC, sizeof(OSC1_DAC)-1, &lenwrt, NULL ); } else if(MyByte == 'w') { //#define WR_LOW_DAC "1w" #define WR_LOW_DAC "w" WriteFileCom( hComHandle, WR_LOW_DAC, sizeof(WR_LOW_DAC)-1, &lenwrt, NULL ); } else if(MyByte == 'W') { //#define WR_HIGH_DAC "1W" #define WR_HIGH_DAC "W" WriteFileCom( hComHandle, WR_HIGH_DAC, sizeof(WR_HIGH_DAC)-1, &lenwrt, NULL ); } else if(MyByte == 'p') { //#define OSC2_DAC "1o1O" #define OSC2_DAC "oO" for(int ip = 0; ip <4*1024; ip++) { WriteFileCom( hComHandle, OSC2_DAC, sizeof(OSC2_DAC)-1, &lenwrt, NULL ); //Sleep(100); //printf("%07d ",ip); //if (ip%16 ==0) // printf("\n"); if (ip%1024 ==0) printf("."); } } else if(MyByte == '+') { if (DatOut) { fclose(DatOut); DatOut = fopen("DatOut.txt","a"); fprintf(DatOut, "\n===%c==== ", MyByte); } //#define OSC2_DAC "1o1O" #define OSC2_DAC "-+" for(int ip = 0; ip <4*1024; ip++) { WriteFileCom( hComHandle, OSC2_DAC, sizeof(OSC2_DAC)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); int ipointer = 0; for(int iAtt = 0; iAtt < 5; iAtt++) { ReadFileCom( hComHandle, &bBuffer[ipointer], sizeof(bBuffer), &lenwrt, NULL ); ipointer += lenwrt; if (ipointer == 2) break; } if (ipointer == 2) { int OutByte = ((bBuffer[0]&0xf)<<4) + (bBuffer[1]&0xf); if (DatOut) { if (ip%16 ==0) { fprintf(DatOut,"\n %04x ", ip); } fprintf(DatOut, "%02x ", OutByte); } } else printf("\n error read =%d ip= %d read =%s",ipointer, ip,bBuffer); //Sleep(100); //printf("%07d ",ip); //if (ip%16 ==0) // printf("\n"); if (ip%1024 ==0) printf("."); } if (DatOut) { fclose(DatOut); DatOut = fopen("DatOut.txt","a"); } } else if(MyByte == 'h') { if (DatOut) { fclose(DatOut); DatOut = fopen("DatOut.txt","a"); fprintf(DatOut, "\n=fromADC==%c==%d== ", MyByte,iHCounter++); } lenwrt = 1; while (lenwrt!=0) { memset(bBuffer, 0, sizeof(bBuffer)); ReadFileCom( hComHandle, &bBuffer[0], sizeof(bBuffer), &lenwrt, NULL ); if (lenwrt) fprintf(DatOut, "skip=%s= ", bBuffer); } #define READ_ADC "hc" for(int ip = 0; ip <1024; ip++) { WriteFileCom( hComHandle, READ_ADC, sizeof(READ_ADC)-1, &lenwrt, NULL ); WriteFileCom( hComHandle, READ_ADC, sizeof(READ_ADC)-1, &lenwrt, NULL ); WriteFileCom( hComHandle, READ_ADC, sizeof(READ_ADC)-1, &lenwrt, NULL ); WriteFileCom( hComHandle, READ_ADC, sizeof(READ_ADC)-1, &lenwrt, NULL ); memset(bBuffer, 0, sizeof(bBuffer)); int ipointer = 0; for(int iAtt = 0; iAtt < 5; iAtt++) { ReadFileCom( hComHandle, &bBuffer[ipointer], 8-ipointer, &lenwrt, NULL ); ipointer += lenwrt; if (ipointer == 8) break; } if (ipointer == 8) { if (ip%4 ==0) { fprintf(DatOut,"\n %04x ", ip*4); } for (int iOutpB = 0; iOutpB >4; unsigned int iui2 = iui&0x0f; //if ((0 <= iui1) && (iui1 <=9)) MyValue[1] = '@'+iui1; //else // MyValue[1] = '@'+iui1-0x0a; //if ((0 <= iui2) && (iui2 <=9)) MyValue[2] = '@'+iui2; //else // MyValue[2] = '@'+iui2-0x0a; #define DAC_VAL "d80" //WriteFileCom( hComHandle, DAC_VAL, sizeof(DAC_VAL)-1, &lenwrt, NULL ); WriteFileCom( hComHandle, &MyValue[0], 4, &lenwrt, NULL ); //Sleep(100); //printf("%07d ",ip); if (ip%1024 ==0) printf("."); } //printf("\nDone."); } else if(MyByte == '7') { //printf("\nStoring"); for(int ip = 0; ip <4*1024; ip++) { #define DAC_VAL "dH@" WriteFileCom( hComHandle, DAC_VAL, sizeof(DAC_VAL)-1, &lenwrt, NULL ); //Sleep(100); //printf("%07d ",ip); if (ip%1024 ==0) printf("."); } //printf("\nDone."); } else if(MyByte == 'm') { //printf("\nStoring"); for(int ip = 0; ip <512; ip++) { //#define DAC_OUT "i" WriteFileCom( hComHandle, INIT_DAC, sizeof(INIT_DAC)-1, &lenwrt, NULL ); Sleep(50); //printf("%07d ",ip); if (ip%64 ==0) printf("."); } //printf("\nDone."); } else if(MyByte == 'd') { //for(int ip = 0; ip <1024; ip++) { #define DAC_VAL "dH@" WriteFileCom( hComHandle, DAC_VAL, sizeof(DAC_VAL)-1, &lenwrt, NULL ); //Sleep(100); //printf("%07d ",ip); //if (ip%1024 ==0) // printf("."); } //printf("\nDone."); } else if(MyByte == 'r') { //#define RLOW_DAC "1r" #define RLOW_DAC "r" WriteFileCom( hComHandle, RLOW_DAC, sizeof(RLOW_DAC)-1, &lenwrt, NULL ); } else if(MyByte == 'R') { //#define RHIGH_DAC "1R" #define RHIGH_DAC "R" WriteFileCom( hComHandle, RHIGH_DAC, sizeof(RHIGH_DAC)-1, &lenwrt, NULL ); } else if(MyByte == '.') { #define ABRAKADABRA_DAC "abrakadabra" WriteFileCom( hComHandle, ABRAKADABRA_DAC, sizeof(ABRAKADABRA_DAC)-1, &lenwrt, NULL ); } } } 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; }