This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:projects:tudengid11:rfid [2011/04/19 13:42] – raivo.sell | et:projects:tudengid11:rfid [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Läbipääsusüsteem ====== | ====== Läbipääsusüsteem ====== | ||
| + | ===== Meeskond ===== | ||
| + | * Vitali Lopatnjuk | ||
| + | * Ivan Podkhomutnikov | ||
| + | |||
| + | ===== Lähteülesanne ===== | ||
| + | susteem mis võimaldab läbipääsu kasutades RFID (Raadiosagedustuvastus) tehnoloogiat. | ||
| + | |||
| + | ===== Süsteemi kirjeldus ===== | ||
| + | Süsteem on ehitatud ATmega128 AVR kontrolleri baasil. Kontrolleri külge on ühendatud | ||
| + | 2x16-kohaline LCD ekraan ja RFID-lugeja. Süsteemi juhtivad kaks programmi. Üks neist jookseb kontrolleri peal, teine arvutil kus asub ka süsteemi andmebaas. Programmid suhtlevad omavahel RS232 liidese kaudu. | ||
| + | |||
| + | ===== Riistvara kokkupanduna ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== Algorütmid ===== | ||
| + | |||
| + | Allpool on toodud mõlema programmi tööalgorütmid. | ||
| + | |||
| + | ==== Kontrolleri poole algorütm ==== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ==== Arvuti poole algorütm ==== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== Tarkvara ===== | ||
| + | |||
| + | ==== Kontrolleri poole tarkvara ==== | ||
| + | |||
| + | rfid.c | ||
| + | <code c>// RFID reader with Interstudy ATmegal28 board | ||
| + | // Reader pins : Enable - PD3, Serout - PD2 (RXD1) | ||
| + | // Reads and displays ID | ||
| + | |||
| + | // Includes | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | #include " | ||
| + | |||
| + | // Configuration | ||
| + | #define RFID_BAUDRATE | ||
| + | #define RFID_BAUD_VALUE | ||
| + | #define RFID_ENABLE | ||
| + | #define RFID_RX | ||
| + | #define LED_GREEN | ||
| + | #define LED_RED | ||
| + | #define LED_YELLOW | ||
| + | #define LED_DEBUG | ||
| + | |||
| + | // | ||
| + | // Определение USART интерфейса | ||
| + | // | ||
| + | usart port = USART(0); | ||
| + | |||
| + | // | ||
| + | // Display initialization | ||
| + | // | ||
| + | void display_init(void) | ||
| + | { | ||
| + | lcd_init(LCD_DISP_ON); | ||
| + | lcd_clrscr(); | ||
| + | lcd_puts(" | ||
| + | } | ||
| + | |||
| + | char read_access() | ||
| + | { | ||
| + | // Access | ||
| + | char c; | ||
| + | |||
| + | // Wait for character | ||
| + | int nMaxIter = 10; | ||
| + | bool bResult = false; | ||
| + | while( bResult == false ) | ||
| + | { | ||
| + | bResult = usart_try_read_char( port, &c ); | ||
| + | |||
| + | // Wait for reply some time, but not forever | ||
| + | --nMaxIter; | ||
| + | _delay_ms(100); | ||
| + | if( nMaxIter <= 0 ) | ||
| + | break; | ||
| + | } | ||
| + | |||
| + | // Got result | ||
| + | if( bResult == false ) | ||
| + | { | ||
| + | c = ' | ||
| + | } | ||
| + | |||
| + | return c; | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Write ID on display | ||
| + | // | ||
| + | void display_write_id(char *id, char c) | ||
| + | { | ||
| + | lcd_gotoxy(0, | ||
| + | lcd_puts(" | ||
| + | lcd_puts(id); | ||
| + | |||
| + | lcd_gotoxy(0, | ||
| + | lcd_puts(" | ||
| + | lcd_gotoxy(8, | ||
| + | |||
| + | // Обозначение знака непосредственно на экране | ||
| + | lcd_alpha_write_char(c); | ||
| + | |||
| + | } | ||
| + | |||
| + | // | ||
| + | // UART configuring | ||
| + | // | ||
| + | void uart_setup() | ||
| + | { | ||
| + | // Setup serial interface | ||
| + | SET_BIT(UCSR1B, | ||
| + | SET_BIT(UCSR1C, | ||
| + | SET_BIT(UCSR1C, | ||
| + | |||
| + | // Set baud rate | ||
| + | UBRR1L = (RFID_BAUD_VALUE & 0xFF); | ||
| + | UBRR1H = (RFID_BAUD_VALUE >> 8); | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Wait for UART incoming data | ||
| + | // | ||
| + | void uart_wait_rx() | ||
| + | { | ||
| + | while (!IS_BIT_SET(UCSR1A, | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Read UART data | ||
| + | // | ||
| + | inline unsigned char uart_read() | ||
| + | { | ||
| + | return UDR1; | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // RFID interface initialization | ||
| + | // | ||
| + | void rfid_init(void) | ||
| + | { | ||
| + | // Setup UART | ||
| + | uart_setup(); | ||
| + | |||
| + | // Setup enable and RX pin | ||
| + | pin_setup_output(RFID_ENABLE); | ||
| + | pin_setup_input(RFID_RX); | ||
| + | } | ||
| + | |||
| + | void usart_init(void) | ||
| + | { | ||
| + | // Настройка USART интерфейса | ||
| + | usart_init_async(port, | ||
| + | USART_DATABITS_8, | ||
| + | USART_STOPBITS_ONE, | ||
| + | USART_PARITY_NONE, | ||
| + | USART_BAUDRATE_ASYNC(9600)); | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // RFID ID reading | ||
| + | // | ||
| + | void rfid_read_id(char *id) | ||
| + | { | ||
| + | enum States { BEGIN, DATA, END } state = BEGIN; | ||
| + | unsigned char data; | ||
| + | unsigned char digits = 0; | ||
| + | |||
| + | // Enable RFID with low signal | ||
| + | pin_clear(RFID_ENABLE); | ||
| + | |||
| + | // Cycle until tag ID received | ||
| + | while (1) | ||
| + | { | ||
| + | // Wait for data | ||
| + | uart_wait_rx(); | ||
| + | data = uart_read(); | ||
| + | |||
| + | // Toggle debug indicator | ||
| + | pin_toggle(LED_DEBUG); | ||
| + | |||
| + | // What's present state and what's next ? | ||
| + | switch (state) | ||
| + | { | ||
| + | // Begin state - we expect start byte (0x0A) | ||
| + | case BEGIN: | ||
| + | // Got the start ? | ||
| + | if (data == 0x0A) | ||
| + | { | ||
| + | state = DATA; | ||
| + | |||
| + | // Disable RFID | ||
| + | pin_set(RFID_ENABLE); | ||
| + | } | ||
| + | break; | ||
| + | |||
| + | // Data state - we expect 10 bytes of ID | ||
| + | case DATA: | ||
| + | // Fill ID string | ||
| + | id[digits++] = data; | ||
| + | |||
| + | // All digits arrived ? | ||
| + | if (digits == 10) state = END; | ||
| + | break; | ||
| + | |||
| + | // End state - we expect end byte (0x0D) | ||
| + | case END: | ||
| + | // Got the end ? | ||
| + | if (data == 0x0D) | ||
| + | { | ||
| + | // Terminate the string | ||
| + | id[digits] = ' | ||
| + | |||
| + | // Сказать компьютеру привет | ||
| + | usart_send_string(port, | ||
| + | |||
| + | // All done - return | ||
| + | return; | ||
| + | } | ||
| + | // Any other case - restart | ||
| + | else | ||
| + | { | ||
| + | state = BEGIN; | ||
| + | |||
| + | // Enable RFID with low signal | ||
| + | pin_clear(RFID_ENABLE); | ||
| + | } | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // | ||
| + | // Main function | ||
| + | // | ||
| + | int main(void) | ||
| + | { | ||
| + | char id[11]; | ||
| + | |||
| + | // Initialization | ||
| + | display_init(); | ||
| + | rfid_init(); | ||
| + | usart_init(); | ||
| + | |||
| + | // Setup indicator pins | ||
| + | pin_setup_output(LED_GREEN); | ||
| + | pin_setup_output(LED_RED); | ||
| + | pin_setup_output(LED_YELLOW); | ||
| + | pin_setup_output(LED_DEBUG); | ||
| + | |||
| + | // Turn off green LED | ||
| + | pin_set(LED_GREEN); | ||
| + | pin_set(LED_RED); | ||
| + | pin_set(LED_YELLOW); | ||
| + | |||
| + | // Endless loop | ||
| + | while (1) | ||
| + | { | ||
| + | // Read RFID tag ID | ||
| + | rfid_read_id(id); | ||
| + | |||
| + | // Turn off green LED | ||
| + | pin_set(LED_GREEN); | ||
| + | pin_set(LED_RED); | ||
| + | |||
| + | pin_clear(LED_YELLOW); | ||
| + | char c = read_access(); | ||
| + | |||
| + | // Display ID | ||
| + | display_write_id(id, | ||
| + | |||
| + | // Light green LED | ||
| + | if( c == ' | ||
| + | { | ||
| + | pin_clear(LED_RED); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | pin_clear(LED_GREEN); | ||
| + | } | ||
| + | |||
| + | // Delay for next try | ||
| + | _delay_ms(5000); | ||
| + | pin_set(LED_YELLOW); | ||
| + | pin_set(LED_GREEN); | ||
| + | pin_set(LED_RED); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Arvuti tarkvara ==== | ||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | main.c | ||
| + | <code c> | ||
| + | // ############################################################################ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define BTN_NEW | ||
| + | #define BTN_EDIT | ||
| + | #define BTN_CONNECT 102 | ||
| + | |||
| + | #define IDD_NEW 200 | ||
| + | #define IDC_STC1 201 | ||
| + | #define IDC_STC2 202 | ||
| + | #define IDC_TAG 203 | ||
| + | #define IDC_ACCESS 204 | ||
| + | #define IDD_EDIT 1000 | ||
| + | #define IDC_ACCESS2 1001 | ||
| + | #define IDC_TAG2 1002 | ||
| + | #define IDC_STC3 1003 | ||
| + | #define IDC_STC4 1004 | ||
| + | #define IDC_DELETE 1007 | ||
| + | |||
| + | /* Declare Windows procedure | ||
| + | LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); | ||
| + | |||
| + | /* Make the class name into a global variable | ||
| + | char szClassName[ ] = " | ||
| + | |||
| + | char *dbFile = " | ||
| + | char *logFile = " | ||
| + | |||
| + | #define LOG_LINES | ||
| + | #define LOG_COLUMNS | ||
| + | char logline[LOG_LINES][LOG_COLUMNS]; | ||
| + | size_t logpos = 0; | ||
| + | |||
| + | char bConnected = 0; | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | HMODULE hInst; | ||
| + | HANDLE | ||
| + | |||
| + | HWND hLog, hComPort; | ||
| + | HWND hNew, hEdit, hConnect; | ||
| + | HWND hCombo; | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | // Переменные которые хранят данные из диалогов New и Edit | ||
| + | char szTag[500]; | ||
| + | char szAccess[500]; | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | int WINAPI WinMain (HINSTANCE hThisInstance, | ||
| + | HINSTANCE hPrevInstance, | ||
| + | LPSTR lpszArgument, | ||
| + | int nFunsterStil) | ||
| + | |||
| + | { | ||
| + | HWND hwnd; /* This is the handle for our window */ | ||
| + | MSG messages; | ||
| + | WNDCLASSEX wincl; | ||
| + | |||
| + | /* The Window structure */ | ||
| + | wincl.hInstance = hThisInstance; | ||
| + | wincl.lpszClassName = szClassName; | ||
| + | wincl.lpfnWndProc = WindowProcedure; | ||
| + | wincl.style = CS_DBLCLKS; | ||
| + | wincl.cbSize = sizeof (WNDCLASSEX); | ||
| + | |||
| + | /* Use default icon and mouse-pointer */ | ||
| + | wincl.hIcon = LoadIcon(hThisInstance, | ||
| + | wincl.hIconSm = LoadIcon(hThisInstance, | ||
| + | wincl.hCursor = LoadCursor (NULL, IDC_ARROW); | ||
| + | wincl.lpszMenuName = NULL; /* No menu */ | ||
| + | wincl.cbClsExtra = 0; /* No extra bytes after the window class */ | ||
| + | wincl.cbWndExtra = 0; /* structure or the window instance */ | ||
| + | /* Use Windows' | ||
| + | wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; | ||
| + | |||
| + | /* Register the window class, and if it fails quit the program */ | ||
| + | if (!RegisterClassEx (& | ||
| + | return 0; | ||
| + | |||
| + | /* The class is registered, let's create the program*/ | ||
| + | hwnd = CreateWindowEx ( | ||
| + | | ||
| + | | ||
| + | " | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | ); | ||
| + | |||
| + | /* Make the window visible on the screen */ | ||
| + | ShowWindow (hwnd, nFunsterStil); | ||
| + | |||
| + | /* Run the message loop. It will run until GetMessage() returns 0 */ | ||
| + | while (GetMessage (& | ||
| + | { | ||
| + | /* Translate virtual-key messages into character messages */ | ||
| + | TranslateMessage(& | ||
| + | /* Send message to WindowProcedure */ | ||
| + | DispatchMessage(& | ||
| + | } | ||
| + | |||
| + | /* The program return-value is 0 - The value that PostQuitMessage() gave */ | ||
| + | return messages.wParam; | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | char check_id( char *id ) | ||
| + | { | ||
| + | |||
| + | size_t nIdLen = strlen(id); | ||
| + | |||
| + | int nCount = SendMessage( hCombo, CB_GETCOUNT, | ||
| + | int i; | ||
| + | for( i = 0; i < nCount; i++ ) | ||
| + | { | ||
| + | char lpItem[500]; | ||
| + | SendMessage( hCombo, CB_GETLBTEXT, | ||
| + | |||
| + | if( strncmp( lpItem, id, nIdLen ) == 0 ) | ||
| + | { | ||
| + | return lpItem[nIdLen+1]; | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | return 0; | ||
| + | |||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | char logz( char *lpData, DWORD nSize ) | ||
| + | { | ||
| + | // Open a handle to the specified com port. | ||
| + | HANDLE hFile = CreateFile( logFile, | ||
| + | GENERIC_READ | GENERIC_WRITE, | ||
| + | 0, // must be opened with exclusive-access | ||
| + | NULL, // | ||
| + | OPEN_ALWAYS, | ||
| + | 0, // not overlapped I/O | ||
| + | NULL ); // hTemplate must be NULL for comm devices | ||
| + | |||
| + | if(hFile == INVALID_HANDLE_VALUE) | ||
| + | { | ||
| + | // | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | // The starting point is the current end-of-file position. | ||
| + | SetFilePointer( hFile, 0, 0, FILE_END ); | ||
| + | |||
| + | if( nSize == 0 ) | ||
| + | nSize = strlen( lpData ); | ||
| + | |||
| + | DWORD nNumberOfBytesToWrite; | ||
| + | WriteFile( hFile, lpData, nSize, & | ||
| + | |||
| + | // End of file | ||
| + | CloseHandle( hFile ); | ||
| + | |||
| + | // Add string to logline | ||
| + | strncpy( & | ||
| + | logpos++; | ||
| + | if( logpos >= LOG_LINES ) | ||
| + | logpos = 0; | ||
| + | |||
| + | // Make a log for text box | ||
| + | char *lpResult = malloc( LOG_COLUMNS * LOG_LINES ); | ||
| + | *lpResult = 0; int i = 0; | ||
| + | for( i = logpos; i < LOG_LINES; i++ ) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | for( i = 0; i < logpos; i++ ) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | SendMessage( hLog, WM_SETTEXT, 0, (int) lpResult ); | ||
| + | free( lpResult ); | ||
| + | |||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | logzf( char *lpData, ... ) | ||
| + | { | ||
| + | char buffer[LOG_COLUMNS]; | ||
| + | va_list args; | ||
| + | va_start (args, lpData); | ||
| + | vsprintf (buffer, | ||
| + | //perror (buffer); | ||
| + | logz( buffer, strlen(buffer) ); | ||
| + | va_end (args); | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | void read_file() | ||
| + | { | ||
| + | DWORD lpNumberOfBytesRead; | ||
| + | |||
| + | // Open a handle to the specified com port. | ||
| + | HANDLE hDB = CreateFile( dbFile, | ||
| + | GENERIC_READ | GENERIC_WRITE, | ||
| + | 0, // must be opened with exclusive-access | ||
| + | NULL, // | ||
| + | OPEN_EXISTING, | ||
| + | 0, // not overlapped I/O | ||
| + | NULL ); // hTemplate must be NULL for comm devices | ||
| + | |||
| + | if(hDB == INVALID_HANDLE_VALUE) | ||
| + | { | ||
| + | // | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | // Read to memory | ||
| + | size_t nSize = GetFileSize( hDB, 0 ); | ||
| + | char *lpBuffer = (char *) malloc( nSize + 1 ); | ||
| + | ReadFile( hDB, lpBuffer, nSize, & | ||
| + | lpBuffer[lpNumberOfBytesRead] = 0; | ||
| + | |||
| + | // End of file | ||
| + | CloseHandle( hDB ); | ||
| + | |||
| + | // Compare ids | ||
| + | int i = 0; | ||
| + | char *lpstart = lpBuffer; | ||
| + | outer_loop: | ||
| + | while( lpBuffer[i] ) | ||
| + | { | ||
| + | if( lpBuffer[i] == 0 ) | ||
| + | { | ||
| + | break; | ||
| + | } | ||
| + | |||
| + | if( lpBuffer[i] == ' | ||
| + | { | ||
| + | lpBuffer[i] = 0; | ||
| + | // | ||
| + | if( *lpstart != ' | ||
| + | { | ||
| + | SendMessage( hCombo, CB_ADDSTRING, | ||
| + | } | ||
| + | lpstart = & | ||
| + | } | ||
| + | |||
| + | i++; | ||
| + | } | ||
| + | |||
| + | free( lpBuffer ); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | void write_file() | ||
| + | { | ||
| + | DWORD lpNumberOfBytesWritten; | ||
| + | |||
| + | // Open a handle to the specified com port. | ||
| + | FILE *pFile = fopen( dbFile, " | ||
| + | |||
| + | if(pFile == 0) | ||
| + | { | ||
| + | // | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | | ||
| + | int nCount = SendMessage( hCombo, CB_GETCOUNT, | ||
| + | int i; | ||
| + | for( i = 0; i < nCount; i++ ) | ||
| + | { | ||
| + | char lpItem[500]; | ||
| + | SendMessage( hCombo, CB_GETLBTEXT, | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | fwrite( & | ||
| + | fwrite( " | ||
| + | } | ||
| + | | ||
| + | // | ||
| + | fwrite( " | ||
| + | // | ||
| + | |||
| + | fclose( pFile ); | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | BOOL CALLBACK NewDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) | ||
| + | { | ||
| + | switch(Message) | ||
| + | { | ||
| + | case WM_INITDIALOG: | ||
| + | { | ||
| + | return TRUE; | ||
| + | } | ||
| + | case WM_COMMAND: | ||
| + | { | ||
| + | switch(LOWORD(wParam)) | ||
| + | { | ||
| + | case IDOK: | ||
| + | { | ||
| + | GetDlgItemText(hwnd, | ||
| + | GetDlgItemText(hwnd, | ||
| + | | ||
| + | EndDialog(hwnd, | ||
| + | break; | ||
| + | } | ||
| + | case IDCANCEL: | ||
| + | { | ||
| + | EndDialog(hwnd, | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | break; | ||
| + | } | ||
| + | default: | ||
| + | { | ||
| + | return FALSE; | ||
| + | } | ||
| + | } | ||
| + | return TRUE; | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | BOOL CALLBACK EditDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) | ||
| + | { | ||
| + | switch(Message) | ||
| + | { | ||
| + | case WM_INITDIALOG: | ||
| + | { | ||
| + | SetDlgItemText(hwnd, | ||
| + | SetDlgItemText(hwnd, | ||
| + | | ||
| + | return TRUE; | ||
| + | } | ||
| + | case WM_COMMAND: | ||
| + | { | ||
| + | switch(LOWORD(wParam)) | ||
| + | { | ||
| + | case IDOK: | ||
| + | { | ||
| + | GetDlgItemText(hwnd, | ||
| + | GetDlgItemText(hwnd, | ||
| + | | ||
| + | EndDialog(hwnd, | ||
| + | break; | ||
| + | } | ||
| + | case IDC_DELETE: | ||
| + | { | ||
| + | EndDialog(hwnd, | ||
| + | break; | ||
| + | } | ||
| + | case IDCANCEL: | ||
| + | { | ||
| + | EndDialog(hwnd, | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | break; | ||
| + | } | ||
| + | default: | ||
| + | { | ||
| + | return FALSE; | ||
| + | } | ||
| + | } | ||
| + | return TRUE; | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | void PrintCommState(DCB dcb) | ||
| + | { | ||
| + | // Print some of the DCB structure values | ||
| + | logzf( " | ||
| + | dcb.BaudRate, | ||
| + | dcb.ByteSize, | ||
| + | dcb.Parity, | ||
| + | dcb.StopBits ); | ||
| + | } | ||
| + | |||
| + | DWORD WINAPI read_com( LPVOID lpParam ) | ||
| + | { | ||
| + | // Read data | ||
| + | char lpBuffer[200]; | ||
| + | DWORD lpNumberOfBytesRead; | ||
| + | while( ReadFile(hCom, | ||
| + | { | ||
| + | lpBuffer[lpNumberOfBytesRead] = 0; | ||
| + | char id = check_id(lpBuffer); | ||
| + | |||
| + | // Send to AVR | ||
| + | DWORD nNumberOfBytesToWrite; | ||
| + | WriteFile( hCom, &id, 1, & | ||
| + | |||
| + | char szTime[100]; | ||
| + | GetTimeFormat( 0, 0, NULL, " | ||
| + | |||
| + | char szDate[100]; | ||
| + | GetDateFormat( 0, 0, NULL, " | ||
| + | |||
| + | // Log | ||
| + | logzf( "[%s %s] ID: %s, Access: %c", szDate, | ||
| + | } | ||
| + | |||
| + | logzf(" | ||
| + | | ||
| + | ExitThread( 0 ); | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | char connect_com() | ||
| + | { | ||
| + | DCB dcb; | ||
| + | //HANDLE hCom; | ||
| + | BOOL fSuccess; | ||
| + | char pcCommPort[30]; | ||
| + | |||
| + | GetWindowTextA( hComPort, pcCommPort, sizeof(pcCommPort) ); | ||
| + | |||
| + | // Open a handle to the specified com port. | ||
| + | hCom = CreateFile( pcCommPort, | ||
| + | GENERIC_READ | GENERIC_WRITE, | ||
| + | 0, // must be opened with exclusive-access | ||
| + | NULL, // | ||
| + | OPEN_EXISTING, | ||
| + | 0, // not overlapped I/O | ||
| + | NULL ); // hTemplate must be NULL for comm devices | ||
| + | |||
| + | if(hCom == INVALID_HANDLE_VALUE) | ||
| + | { | ||
| + | // | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | // Initialize the DCB structure. | ||
| + | ZeroMemory(& | ||
| + | dcb.DCBlength = sizeof(DCB); | ||
| + | |||
| + | // Build on the current configuration by first retrieving all current | ||
| + | // settings. | ||
| + | fSuccess = GetCommState(hCom, | ||
| + | |||
| + | if (!fSuccess) | ||
| + | { | ||
| + | // Handle the error. | ||
| + | logzf(" | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | PrintCommState(dcb); | ||
| + | |||
| + | // Fill in some DCB values and set the com state: | ||
| + | // 57,600 bps, 8 data bits, no parity, and 1 stop bit. | ||
| + | dcb.BaudRate = CBR_9600; | ||
| + | dcb.ByteSize = 8; // | ||
| + | dcb.Parity | ||
| + | dcb.StopBits = ONESTOPBIT; | ||
| + | |||
| + | fSuccess = SetCommState(hCom, | ||
| + | |||
| + | if (!fSuccess) | ||
| + | { | ||
| + | // Handle the error. | ||
| + | logzf(" | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | // Get the comm config again. | ||
| + | fSuccess = GetCommState(hCom, | ||
| + | |||
| + | if (!fSuccess) | ||
| + | { | ||
| + | // Handle the error. | ||
| + | logzf(" | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | PrintCommState(dcb); | ||
| + | |||
| + | hThread = CreateThread( NULL, 0, read_com, 0, 0, NULL ); | ||
| + | |||
| + | logzf( " | ||
| + | |||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | char disconnect_com() | ||
| + | { | ||
| + | TerminateThread( hThread, 0 ); | ||
| + | CloseHandle( hCom ); | ||
| + | |||
| + | logzf( "COM port disconnected" | ||
| + | | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | /* This function is called by the Windows function DispatchMessage() | ||
| + | LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) | ||
| + | { | ||
| + | switch (message) | ||
| + | { | ||
| + | case WM_CREATE: | ||
| + | { | ||
| + | hInst = GetModuleHandle(NULL); | ||
| + | |||
| + | hLog = CreateWindowEx(WS_EX_CLIENTEDGE, | ||
| + | hComPort = CreateWindowEx(WS_EX_CLIENTEDGE, | ||
| + | |||
| + | hCombo = CreateWindow(" | ||
| + | hNew = CreateWindow(" | ||
| + | hEdit = CreateWindow(" | ||
| + | hConnect = CreateWindow(" | ||
| + | |||
| + | HFONT hfDefault = GetStockObject(DEFAULT_GUI_FONT); | ||
| + | SendMessage(hLog, | ||
| + | SendMessage(hNew, | ||
| + | SendMessage(hEdit, | ||
| + | SendMessage(hConnect, | ||
| + | SendMessage(hComPort, | ||
| + | SendMessage(hCombo, | ||
| + | |||
| + | read_file(); | ||
| + | |||
| + | SendMessage( hCombo, CB_SETCURSEL, | ||
| + | |||
| + | logz( " | ||
| + | |||
| + | break; | ||
| + | } | ||
| + | | ||
| + | case WM_COMMAND: | ||
| + | switch(wParam) | ||
| + | { | ||
| + | case BTN_NEW: | ||
| + | { | ||
| + | logz( "Add new tag", 11 ); | ||
| + | int ret = DialogBox(GetModuleHandle(NULL), | ||
| + | if(ret != IDOK) | ||
| + | { | ||
| + | break; | ||
| + | } | ||
| + | | ||
| + | // Добавление нового RFID | ||
| + | char szTemp[500]; | ||
| + | snprintf( szTemp, sizeof(szTemp), | ||
| + | int nCur = SendMessage( hCombo, CB_ADDSTRING, | ||
| + | SendMessage( hCombo, CB_SETCURSEL, | ||
| + | | ||
| + | write_file(); | ||
| + | |||
| + | break; | ||
| + | } | ||
| + | case BTN_EDIT: | ||
| + | { | ||
| + | logz( "Edit new tag", 12 ); | ||
| + | int nCur = SendMessage( hCombo, CB_GETCURSEL, | ||
| + | SendMessage( hCombo, CB_GETLBTEXT, | ||
| + | | ||
| + | // Разделение RFID-тега и уровня доступа | ||
| + | char *lpSpace = strrchr( szTag, ' ' ); | ||
| + | *lpSpace = 0; | ||
| + | strcpy( szAccess, & | ||
| + | | ||
| + | int ret = DialogBox(GetModuleHandle(NULL), | ||
| + | if(ret != IDOK && ret != IDC_DELETE) | ||
| + | { | ||
| + | break; | ||
| + | } | ||
| + | | ||
| + | SendMessage( hCombo, CB_DELETESTRING, | ||
| + | | ||
| + | if(ret == IDOK) | ||
| + | { | ||
| + | char szTemp[500]; | ||
| + | snprintf( szTemp, sizeof(szTemp), | ||
| + | nCur = SendMessage( hCombo, CB_ADDSTRING, | ||
| + | SendMessage( hCombo, CB_SETCURSEL, | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | SendMessage( hCombo, CB_SETCURSEL, | ||
| + | } | ||
| + | |||
| + | write_file(); | ||
| + | |||
| + | break; | ||
| + | } | ||
| + | case BTN_CONNECT: | ||
| + | { | ||
| + | if( bConnected == 0 ) | ||
| + | bConnected = connect_com(); | ||
| + | else | ||
| + | bConnected = disconnect_com(); | ||
| + | | ||
| + | |||
| + | if( bConnected == 0 ) | ||
| + | SetWindowTextA( hConnect, " | ||
| + | else | ||
| + | SetWindowTextA( hConnect, " | ||
| + | | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | break; | ||
| + | | ||
| + | case WM_SIZE: | ||
| + | { | ||
| + | RECT rcClient; | ||
| + | GetClientRect(hwnd, | ||
| + | SetWindowPos(hLog, | ||
| + | SetWindowPos(hCombo, | ||
| + | SetWindowPos(hNew, | ||
| + | SetWindowPos(hEdit, | ||
| + | SetWindowPos(hComPort, | ||
| + | SetWindowPos(hConnect, | ||
| + | break; | ||
| + | } | ||
| + | |||
| + | case WM_DESTROY: | ||
| + | { | ||
| + | if( bConnected == 1 ) | ||
| + | bConnected = disconnect_com(); | ||
| + | |||
| + | // Выход. Сохранение. | ||
| + | write_file(); | ||
| + | |||
| + | PostQuitMessage (0); /* send a WM_QUIT to the message queue */ | ||
| + | break; | ||
| + | } | ||
| + | | ||
| + | default: | ||
| + | { | ||
| + | return DefWindowProc (hwnd, message, wParam, lParam); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | // ############################################################################ | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Kokkuvõte ja järeldused ===== | ||
| + | |||
| + | Antud projekti raames sai ehitatud toimiv läbipääsusüsteem. Praktilise kasutamise eesmärgiks süsteemi saab kergelt täiendada lisades täitemehhanismi ning väikesel määral koodi muutades. | ||
| + | |||
| + | ---- | ||
| + | Täname Meie juhendajat Raivo Selli. | ||
| + | |||
| + | ---- | ||
| + | Terve projekti saab maha laadida | ||
| + | [[https:// | ||
| + | [[http:// | ||