#include // definire constante, variabile, structuri, functii #define ID_PTT_ON 0 #define ID_PTT_OFF 1 #define ID_CW_ON 2 #define ID_CW_OFF 3 #define ID_TX 4 #define IDM_SYS_ABOUT 6 int cxChar, cyChar, iL; int iT=0; int i=0; static BOOL TX = FALSE; TCHAR szAppName[]=TEXT("SerialPort"); TCHAR szCall[16]= TEXT("- . . . . - "); HWND hwnd; HWND hwndButton[5]; HINSTANCE hInstance; DCB dcb; HANDLE hCom; DWORD dwError; BOOL fSuccess; PAINTSTRUCT ps; HDC hdc; RECT rect; HBRUSH hBrush; HMENU hMenu; MSG msg; void LED_ON(void); void LED_OFF(void); int AskConfirmation (HWND hwnd) { return MessageBox (hwnd, "Close ?", szAppName, MB_YESNO | MB_ICONQUESTION); } LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); // intrarea in program int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int iCmdShow) { // definirea clasei wndclass WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor( NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndclass.lpszMenuName = szAppName; wndclass.lpszClassName = szAppName ; // inregistrarea clasei if (!RegisterClass (&wndclass)) { MessageBox ( NULL, TEXT ("Windows NT !"), szAppName, MB_ICONERROR); return 0; } // crearea ferestrei hwnd = CreateWindow (szAppName, TEXT ("Serial Port COM1"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, hInstance, NULL); // extensie meniu sistem hMenu = GetSystemMenu(hwnd, FALSE); AppendMenu(hMenu, MF_SEPARATOR, 0, NULL); AppendMenu(hMenu, MF_STRING, IDM_SYS_ABOUT, TEXT ("Mesaj")); // afisarea ferestrei ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); // deschidere port COM1 hCom = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(hCom == INVALID_HANDLE_VALUE) { MessageBox (hwnd, "Port not available !", szAppName, MB_YESNO); } fSuccess = GetCommState(hCom, &dcb); if(!fSuccess) { MessageBox (hwnd, "Port problem !", szAppName, MB_YESNO); } //setarea iesirilor RTS si DTS pe 0 dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fDtrControl = DTR_CONTROL_DISABLE; SetCommState(hCom, &dcb); iL= strlen(szCall); // lungimea mesajului TEST // bucla de mesaje while (msg.message != WM_QUIT) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage (&msg); DispatchMessage (&msg); } else { // testeaza daca e activata secventa CW if((iT<=0) && (TX == TRUE)) { if('.' == szCall[i]) { iT=1; dcb.fDtrControl = DTR_CONTROL_ENABLE; SetCommState(hCom, &dcb); } if('-' == szCall[i]) { iT=3; dcb.fDtrControl = DTR_CONTROL_ENABLE; SetCommState(hCom, &dcb); } if(' ' == szCall[i]) { iT=1; dcb.fDtrControl = DTR_CONTROL_DISABLE; SetCommState(hCom, &dcb); } i=i+1; if(i > iL) // testeaza daca secventa CW sa terminat { TX = FALSE; i=0; dcb.fRtsControl = RTS_CONTROL_DISABLE; SetCommState(hCom, &dcb); LED_OFF(); } } } } return 0 ; } //functia fereastra LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: cxChar = LOWORD (GetDialogBaseUnits ()); cyChar = HIWORD (GetDialogBaseUnits ()); hwndButton[0] = CreateWindow ( TEXT("button"), "PTT_ON", WS_CHILD | WS_VISIBLE | BS_CENTER | BS_PUSHBUTTON, 2* cxChar, 2* cyChar, 9 * cxChar, 7 * cyChar / 4, hwnd, (HMENU) ID_PTT_ON, hInstance, NULL); hwndButton[1] = CreateWindow ( TEXT("button"), "PTT_OFF", WS_CHILD | WS_VISIBLE | BS_CENTER | BS_PUSHBUTTON, 12* cxChar, 2* cyChar, 9 * cxChar, 7 * cyChar / 4, hwnd, (HMENU) ID_PTT_OFF, hInstance, NULL); hwndButton[2] = CreateWindow ( TEXT("button"), "CW_ON", WS_CHILD | WS_VISIBLE | BS_CENTER | BS_PUSHBUTTON, 2* cxChar, 5* cyChar, 9 * cxChar, 7 * cyChar / 4, hwnd, (HMENU) ID_CW_ON, hInstance, NULL); hwndButton[3] = CreateWindow ( TEXT("button"), "CW_OFF", WS_CHILD | WS_VISIBLE | BS_CENTER | BS_PUSHBUTTON, 12* cxChar, 5* cyChar, 9 * cxChar, 7 * cyChar / 4, hwnd, (HMENU) ID_CW_OFF, hInstance, NULL); hwndButton[4] = CreateWindow ( TEXT("button"), "TEST", WS_CHILD | WS_VISIBLE | BS_CENTER | BS_PUSHBUTTON, 22* cxChar, 5* cyChar, 9 * cxChar, 7 * cyChar / 4, hwnd, (HMENU) ID_TX, hInstance, NULL); // activeaza primul timer SetTimer (hwnd, 1, 40, NULL); return 0; case WM_TIMER: iT=iT-1; return 0; case WM_PAINT: hdc= BeginPaint (hwnd, &ps); SelectObject (hdc, GetStockObject ( SYSTEM_FIXED_FONT)); TextOut (hdc, 22*cxChar, 7*cyChar, "SPACE Key", 9); Rectangle(hdc,39,14,66,23); EndPaint (hwnd, &ps); return 0; case WM_COMMAND: // sa transmit TEST ? if(LOWORD(wParam) == ID_TX) { TX = TRUE; dcb.fRtsControl = RTS_CONTROL_ENABLE; SetCommState(hCom, &dcb); LED_ON(); } // activez PTT-ul ? if(LOWORD(wParam) == ID_PTT_ON) { dcb.fRtsControl = RTS_CONTROL_ENABLE; fSuccess = SetCommState(hCom, &dcb); LED_ON(); } if(LOWORD(wParam) == ID_PTT_OFF) { dcb.fRtsControl = RTS_CONTROL_DISABLE; fSuccess = SetCommState(hCom, &dcb); LED_OFF(); } if(LOWORD(wParam) == ID_CW_ON) { dcb.fDtrControl = DTR_CONTROL_ENABLE; fSuccess = SetCommState(hCom, &dcb); } if(LOWORD(wParam) == ID_CW_OFF) { dcb.fDtrControl = DTR_CONTROL_DISABLE; fSuccess = SetCommState(hCom, &dcb); } return 0; // sa apasat tasta SPATIU ? case WM_KEYDOWN: switch(wParam) { case VK_SPACE: TX = TRUE; dcb.fRtsControl = RTS_CONTROL_ENABLE; SetCommState(hCom, &dcb); LED_ON(); break; } return 0; // sa ales din meniu 'mesaj' case WM_SYSCOMMAND: switch(LOWORD(wParam)) { case IDM_SYS_ABOUT: MessageBox(hwnd, TEXT("Vy 73's de YO2CMI\nBerni"),szAppName, MB_OK); return 0; } break; case WM_CLOSE: if (IDYES == AskConfirmation(hwnd)) { DestroyWindow(hwnd); } return 0; // iesire din program case WM_DESTROY: KillTimer(hwnd, 1); CloseHandle(hCom); hCom = NULL; PostQuitMessage (0); return 0; } return DefWindowProc ( hwnd, message, wParam,lParam); } // functiile LED void LED_ON(void) { hdc=GetDC(hwnd); SetRect(&rect, 40, 15, 65, 22); hBrush = CreateSolidBrush(RGB(240,50,50)); FillRect(hdc, &rect, hBrush); ReleaseDC(hwnd, hdc); } void LED_OFF(void) { hdc=GetDC(hwnd); SetRect(&rect, 40, 15, 65, 22); hBrush = CreateSolidBrush(RGB(255,255,255)); FillRect(hdc, &rect, hBrush); ReleaseDC(hwnd, hdc); }