// terminal.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include #include int com_putch(char in, HANDLE hCom) { DWORD Pocet=0; unsigned char Data = in; WriteFile( hCom, // Handle portu, ktory ste otvorili CreateFile &Data, // Smernik (pointer) na data, ktore chcem vysielat 1, // Pocet bytov, ktore chcem vyslat &Pocet, // Smernik (pointer) na pocet vyslanych dat NULL); // Musi byt NULL if(Pocet != 1)return 1; if(in == '\r')com_putch('\n',hCom); return 0; } #define COM_GETCH_EMPTY 0xFFFF int com_getch(HANDLE hCom) { DWORD Pocet=0; // Pocet prijatých dát int Data; // Sem ulož prijatý znak ReadFile( hCom, // Handle portu, z ktorého cítame &Data, // Pointer na buffer, kam ukladáme prijaté dáta 1, // Pocet bytov, na ktoré cakáme &Pocet, // Pointer na skutocný pocet prijatých dát NULL); // Musí byt NULL if(Pocet != 1)return COM_GETCH_EMPTY; return Data; } #define INPUT_COLOR 9 #define OUTPUT_COLOR 10 #define ERROR_COLOR 8 int main(int argc, char* argv[]) { HANDLE hCom; // Handle na objekt typu súbor HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); hCom = CreateFile( "COM1:", // Názov portu, ktorý otvárame GENERIC_READ | GENERIC_WRITE, // Otvárame na cítanie aj na zápis 0, // Zdielanie nebude žiadne NULL, // Bezpecnostné info žiadne OPEN_EXISTING, // Otvárame existujúci (port) 0, // Žiadne atribúty súboru NULL); // Musí byt NULL if ( hCom == INVALID_HANDLE_VALUE ) { printf("\n Chyba: Port sa neda otvorit.\n"); CloseHandle(hCom); return 1; }; DCB PortDCB; PortDCB.DCBlength = sizeof(DCB); // Inicializuj položku DCBlength GetCommState(hCom,&PortDCB); // Nacítaj aktuálne nastavenia PortDCB.BaudRate = CBR_9600; // Zmeníme nejaký parameter v DCB PortDCB.fParity = 0; PortDCB.ByteSize = 8; PortDCB.StopBits = ONESTOPBIT; int err; err = SetCommState(hCom,&PortDCB); // Nastav aktuálne nastavenia na port if(!err) { printf("Chyba: Port sa neda nakonfigurovat."); CloseHandle(hCom); return 2; }; COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = 1; timeouts.ReadTotalTimeoutMultiplier = 1; timeouts.ReadTotalTimeoutConstant = 1; timeouts.WriteTotalTimeoutMultiplier = 1; timeouts.ReadTotalTimeoutConstant = 1; if(!SetCommTimeouts(hCom,&timeouts)) { printf("Chyba: Problem pri nastavovani timeoutov!\n"); CloseHandle(hCom); return 3; } PurgeComm(hCom,PURGE_TXCLEAR | PURGE_RXCLEAR); fflush(stdin); int a; int in,posledne=0; while(1) { Sleep(1); if(kbhit()) { in = getch(); SetConsoleTextAttribute(hConsole, OUTPUT_COLOR); if(posledne == 0) { putch('\n'); posledne = 1; } putch(in); if(in == '\r')putch('\n'); if(in==27)break;; if(com_putch(in,hCom)) { SetConsoleTextAttribute(hConsole, ERROR_COLOR); printf("\nChyba pri posielani znaku %c",in); }; };; if((a=com_getch(hCom))!=COM_GETCH_EMPTY) { if(posledne == 1) { putch('\n'); posledne = 0; } SetConsoleTextAttribute(hConsole, INPUT_COLOR); putch(a); };; } SetConsoleTextAttribute(hConsole, ERROR_COLOR); //vypis com nastavenia: printf("Nastavenia COM portu:\n"); GetCommState(hCom,&PortDCB); printf("\nBaud rate: %d",PortDCB.BaudRate); printf("\nParita: "); switch(PortDCB.Parity) { case EVENPARITY: printf("Even parity"); break; case MARKPARITY: printf("Mark parity"); break; case NOPARITY: printf("No parity"); break; case ODDPARITY: printf("Odd parity"); break; case SPACEPARITY: printf("Space parity"); break; default: printf("Weird parity");break; } printf("\nStop bity: "); switch(PortDCB.StopBits) { case ONESTOPBIT: printf("1"); break; case ONE5STOPBITS: printf("1.5"); break; case TWOSTOPBITS: printf("2"); break; default: printf("Ine");break; } getch(); CloseHandle(hCom); return 0; }