#include "stdafx.h" #include "widget.h" #include "IRScopeWnd.h" static HANDLE comm_handle=NULL; int OpenPort(CString port) { port="\\\\.\\"+port; comm_handle=CreateFile(port,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if(comm_handle==INVALID_HANDLE_VALUE) { const DWORD err=GetLastError(); TRACE2("Error opening port: %s (%i)\n",port,err); comm_handle=NULL; return (int)err; } TRACE2("Port % s open - handle: %08X\n",port,comm_handle); // SetupComm(m_hComm,RxSize,TxSize); // Setup Rx/Tx buffer sizes (optional) DCB dcb; dcb.DCBlength=sizeof(dcb); GetCommState(comm_handle,&dcb); dcb.BaudRate=115200; // bit rate dcb.ByteSize=8; // number of bits/byte, 4-8 dcb.Parity=0; // 0-4=no,odd,even,mark,space dcb.StopBits=0; // 0,1,2 = 1, 1.5, 2 dcb.fBinary=1; // binary mode, no EOF check dcb.fParity=0; // enable parity checking dcb.fDtrControl=DTR_CONTROL_DISABLE; // DTR flow control type dcb.fRtsControl=RTS_CONTROL_DISABLE; // RTS flow control dcb.fOutxCtsFlow=0; // CTS output flow control dcb.fOutxDsrFlow=0; // DSR output flow control dcb.fDsrSensitivity=0; // DSR sensitivity dcb.fTXContinueOnXoff=0;// XOFF continues Tx dcb.fOutX=0; // XON/XOFF out flow control dcb.fInX=0; // XON/XOFF in flow control dcb.fNull=0; // enable null stripping dcb.fAbortOnError=0; // abort reads/writes on error dcb.fErrorChar=0; // enable error replacement dcb.ErrorChar=(char)0xDE; // error replacement character //dcb.XonLim; // transmit XON threshold //dcb.XoffLim; // transmit XOFF threshold //dcb.XonChar; // Tx and Rx XON character //dcb.XoffChar; // Tx and Rx XOFF character //dcb.EofChar; // end of input character //dcb.EvtChar; // received event character SetCommState(comm_handle,&dcb); COMMTIMEOUTS to; GetCommTimeouts(comm_handle,&to); to.ReadIntervalTimeout=50; to.ReadTotalTimeoutMultiplier=1; to.ReadTotalTimeoutConstant=3000; //to.WriteTotalTimeoutMultiplier=; //to.WriteTotalTimeoutConstant=; SetCommTimeouts(comm_handle,&to); SetCommMask(comm_handle,0); Sleep(500); EscapeCommFunction(comm_handle,SETDTR); Sleep(200); EscapeCommFunction(comm_handle,SETRTS); return 0; } void ClosePort(void) { CloseHandle(comm_handle); comm_handle=NULL; } UINT ReadPort(UINT len, BYTE* data) { PurgeComm(comm_handle,PURGE_RXCLEAR); DWORD err; ClearCommError(comm_handle,&err,NULL); TRACE1("Comm error: %08X\n",err); /* winbase.h CE_RXOVER 0x0001 // Receive Queue overflow CE_OVERRUN 0x0002 // Receive Overrun Error CE_RXPARITY 0x0004 // Receive Parity Error CE_FRAME 0x0008 // Receive Framing error CE_BREAK 0x0010 // Break Detected CE_TXFULL 0x0100 // TX Queue is full */ DWORD read; #if 0 ReadFile(comm_handle,data,len,&read,NULL); #else DWORD total_read=0; BYTE* px=data; while(total_read < len) { read=len-total_read; if(read > 4096) read=4096; ReadFile(comm_handle,px,read,&read,NULL); if(read==0) break; //TRACE1("%i ",read); px+=read; total_read+=read; } read=total_read; #endif ClearCommError(comm_handle,&err,NULL); TRACE1("Comm error: %08X\n",err); return read; } int ProcessData(UINT len, BYTE* data, int& count, int*& times, short int*& counts, UINT& freq) { // Find start of signal for(UINT n=0;nhigh) high=b; } TRACE1("High: %i\n",high); if(high<3) { delete[] pc; return 2; } --high; // Histogram #if 0 UINT hist[256]; memset(hist,0,sizeof(hist)); for(n=0;n=high) { // Full on s+=b; ++t; if(!state) { // Off to on transition ++tc; state=TRUE; } } else { // Split on/off ++tc; state=!state; } } else { // Full off if(state) { // On to off transition ++tc; state=FALSE; } } } TRACE1("Transitions: %i\n",tc); TRACE2("Sum of high counts: %i Time periods of high counts: %i\n",s,t); // Calculate carrier frequency in Hertz const UINT p=100; // 100 uS sample period freq=(p&&t)?static_cast(static_cast(s)*1000000/(p*t)):0; TRACE1("Frequency: %i Hz\n",freq); // Calculate carrier period in microseconds const int period=s?t*p/s:0; TRACE1("Period: %i uS\n",period); // Allocate memory for times and counts times=new int[tc]; counts=new short int[tc]; if(times==NULL || counts==NULL) { delete[] pc; delete[] times; delete[] counts; return -2; } UINT i=0; state=(pc[0]!=0); int ti=0; int c=0; //UINT f=0; //UINT g=0; for(n=0;n=high) { // Full on //f+=b; ++g; if(state) { // Still on c+=b; ti+=p; } else { // Off to on transition ASSERT(c==0); counts[i]=c; times[i]=-ti; ++i; state=TRUE; c=b; ti=p; } } else { // Split on/off //const int on=b*p/(state?high:high-1); //const int on=b*p/high; //const int on=b*pulse_time; const int on=s?t*p*b/s:p/2; if(n) { const int off=p-on; if(state) { // On to off transition counts[i]=c+b; times[i]=ti+on; ++i; state=FALSE; c=0; ti=off; } else { // Off to on transition ASSERT(c==0); counts[i]=c; times[i]=-(ti+off); ++i; state=TRUE; c=b; ti=on; } } else { ASSERT(ti==0); ti=on; } } } else { // b==0 // Full off if(state) { // On to off transition counts[i]=c; times[i]=ti; ++i; state=FALSE; c=0; ti=p; } else { // Still off ti+=p; } } } counts[i]=c; times[i]=state?ti:-ti; ASSERT(i==tc-1); count=tc; delete[] pc; return 0; } UINT WriteFile(CString file_name, int count, int* times, short int* counts) { if(!count) return 0; CFile file; if(file.Open(file_name,CFile::modeWrite | CFile::modeCreate)) { CString s; s.Format("%i\r\n",count); file.Write(s,s.GetLength()); for(int i=0;i