#include #include "recpic.h" #include #include //Functions LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM); //Global variables HINSTANCE hInst; LPCTSTR lpszAppName = "RECPIC"; HWND hwnd; DCB dcb; HANDLE h1, e1, eStart, eStop, eMsg; BOOL FileOpen; HANDLE fp; UINT t1, t2; COMMCONFIG cc; DWORD ccsize; COMMTIMEOUTS c1; /******************************************************** PROGRAM STARTS HERE!!! ********************************************************/ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow) { //Local Variables MSG msg; WNDCLASSEX wc; LPCTSTR lpszTitle = "PIC Recorder"; HANDLE hAccel; //Register Window wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND; wc.lpszMenuName = lpszAppName; wc.lpszClassName = lpszAppName; wc.cbSize = sizeof(WNDCLASSEX); wc.hIconSm = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON3)); if(!RegisterClassEx(&wc)) return(FALSE); hInst = hInstance; //Create Main Window hwnd = CreateWindow( lpszAppName, lpszTitle, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, 0, 560, 220, NULL, NULL, hInstance, NULL); if(!hwnd) return(FALSE); //Show Main Window ShowWindow(hwnd, CmdShow); //Create Child Windows e1 = CreateWindow("EDIT", NULL, WS_CHILD | ES_LEFT | WS_VISIBLE | WS_BORDER, 30, 60, 300, 25, hwnd, (HMENU)301, hInstance, NULL); CreateWindow("STATIC", "Name of File to Record", WS_CHILD | WS_VISIBLE, 30, 20, 300, 25, hwnd, NULL, hInstance, NULL); eStart = CreateWindow("BUTTON", "START", WS_CHILD | BS_DEFPUSHBUTTON | WS_VISIBLE, 360, 20, 130, 25, hwnd, (HMENU)IDD_START, hInstance, NULL); eStop = CreateWindow("BUTTON", "STOP", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 360, 60, 130, 25, hwnd, (HMENU)IDD_STOP, hInstance, NULL); eMsg = CreateWindow("STATIC", NULL, WS_CHILD | SS_LEFT | WS_VISIBLE, 60, 120, 300, 50, hwnd, NULL, hInstance, NULL); //Set Communications GetDefaultCommConfig("COM1", (LPCOMMCONFIG)&cc, (LPDWORD)&ccsize); SetDefaultCommConfig("COM1", (LPCOMMCONFIG)&cc, ccsize); cc.dcb.BaudRate = CBR_115200; cc.dcb.ByteSize=8; cc.dcb.Parity=NOPARITY; cc.dcb.StopBits=ONESTOPBIT; cc.dcb.fOutxCtsFlow=FALSE; cc.dcb.fOutxDsrFlow=FALSE; cc.dcb.fDtrControl=DTR_CONTROL_DISABLE; cc.dcb.fOutX=FALSE; cc.dcb.fInX=FALSE; cc.dcb.fRtsControl=RTS_CONTROL_DISABLE; SetDefaultCommConfig("COM1", (LPCOMMCONFIG)&cc, ccsize); //Load Accelerators hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(ACCELERATORS_1)); //Standard Windows Message Loop while(GetMessage(&msg, NULL, 0, 0)) { if(!TranslateAccelerator(hwnd, hAccel, &msg)); { TranslateMessage(&msg); DispatchMessage(&msg); } } return(msg.wParam); } /******************************************************** MAIN WINDOWS PROCESS ********************************************************/ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { //Local Variables char text[16384]; DWORD dRead, dWrite; unsigned int x; //Windows Process Entry Point switch(msg) { //Initialisation routines case WM_CREATE: FileOpen = FALSE; t1 = SetTimer(hWnd,1,100,NULL); break; case WM_TIMER: if(wParam==1) { KillTimer(hWnd, t1); SetFocus(e1); EnableWindow(eStop, FALSE); SetWindowText(eMsg, "Enter File Name to Record"); } else { ReadFile(h1, text, 16383,&dRead, NULL); for (x=0; x=0x80) text[x]-=0x80; else text[x]+=0x80; } if(FileOpen) { SetWindowText(eMsg, "Recording"); WriteFile(fp, text, dRead, &dWrite, NULL); } } break; //Closedown Routines case WM_DESTROY: GetDefaultCommConfig("COM2", (LPCOMMCONFIG)&cc, (LPDWORD)&ccsize); SetDefaultCommConfig("COM1", (LPCOMMCONFIG)&cc, ccsize); PostQuitMessage(0); break; //Commands from buttons and Menus case WM_COMMAND: switch (LOWORD (wParam)) { case IDD_ABOUT: MessageBox(hWnd,"RecPIC" "\nWritten by Cliff Powlesland (G8CQZ)" "\nto record data from PIC D/A convertors" "\n\nCopyright © 2005\n\n", "About RecPIC", MB_OK); break; case IDD_RETURN: case IDD_START: case IDD_STOP: if (!FileOpen) { GetDlgItemText(hWnd,301,text,255); fp = CreateFile(text, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (fp == INVALID_HANDLE_VALUE) { SetWindowText(eMsg, "Cannot Open File or File Exists"); SetDlgItemText(hWnd,301,""); SetFocus(e1); break; } SetWindowText(eMsg, "File Opened"); text[0]= 0x2e; text[1]= 0x73; text[2]= 0x6e; text[3]= 0x64; text[4]= 0x00; text[5]= 0x00; text[6]= 0x00; text[7]= 0x18; text[8]= 0xff; text[9]= 0xff; text[10]= 0xff; text[11]= 0xff; text[12]= 0x00; text[13]= 0x00; text[14]= 0x00; text[15]= 0x02; text[16]= 0x00; text[17]= 0x00; text[18]= 0x1f; text[19]= 0x40; text[20]= 0x00; text[21]= 0x00; text[22]= 0x00; text[23]= 0x01; text[24]= 0x00; text[25]= 0x00; text[26]= 0x00; text[27]= 0x00; WriteFile(fp, text, 24, &dWrite, NULL); FileOpen = TRUE; EnableWindow(eStop, TRUE); EnableWindow(eStart, FALSE); SetFocus(eStop); h1=CreateFile("COM1", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); GetCommState(h1, (LPDCB) &dcb); dcb.BaudRate = CBR_115200; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=ONESTOPBIT; SetCommState(h1, (LPDCB) &dcb); c1.ReadIntervalTimeout = 1; c1.ReadTotalTimeoutMultiplier = 1; c1.ReadTotalTimeoutConstant = 1; SetCommTimeouts(h1, &c1); SetupComm(h1, 65535, 65535); t2 = SetTimer(hwnd,2,1,NULL); } else { KillTimer(hWnd, t2); FileOpen = FALSE; EnableWindow(eStop, FALSE); EnableWindow(eStart, TRUE); CloseHandle(fp); CloseHandle(h1); SetWindowText(eMsg, "File Saved"); SetDlgItemText(hWnd,301,""); SetFocus(e1); } break; case IDD_HELP: WinHelp(hWnd, "recpic.hlp", HELP_INDEX, NULL); break; case IDD_EXIT: CloseHandle(fp); CloseHandle(h1); DestroyWindow(hWnd); GetDefaultCommConfig("COM2", (LPCOMMCONFIG)&cc, (LPDWORD)&ccsize); SetDefaultCommConfig("COM1", (LPCOMMCONFIG)&cc, ccsize); break; } //Default Windows Routines default: return(DefWindowProc(hWnd, msg, wParam, lParam)); } return(0L); }