Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <io.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. //#include <iostream>
  6. #include <conio.h>
  7. #include <stdio.h>
  8. using namespace std;
  9. //#define DEBUG
  10.  
  11. void GoToXY(int column, int line);
  12.  
  13. typedef struct
  14. {
  15.     int n;
  16.     char buffer[80];
  17. } M;
  18.  
  19. int main(int argc, char** argv)
  20. {
  21.     M m;
  22.     M m2;
  23.     m.n = 0;
  24.     m2.n = 0;
  25.     //m.buffer = (char*)calloc(1, 80);
  26.     HANDLE uchwyt = CreateFile(TEXT("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  27.     //unsigned char* tab = (unsigned char*)calloc(8, sizeof(unsigned char));
  28.     unsigned char tab[9] = { 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x03, 0x00 };
  29.     DWORD byteswritten = 0; //integer 4-bajtowy
  30.     DCB dcb; //info o porcie szeregowym
  31.     SecureZeroMemory(&dcb, sizeof(DCB));
  32.     dcb.DCBlength = sizeof(DCB);
  33.     int result = GetCommState(uchwyt, &dcb); //pobiera info o danym porcie szeregowym
  34.     dcb.BaudRate = CBR_9600; //baud rate
  35.     dcb.ByteSize = 8; //data size, xmit and rcv
  36.     dcb.Parity = NOPARITY; //parity bit
  37.     dcb.StopBits = ONESTOPBIT; //stop bit
  38.     result = SetCommState(uchwyt, &dcb);
  39.     //char* string1 = (char*)calloc(80, sizeof(char));
  40.     int ifread1 = ReadFile(uchwyt, LPVOID(&m2), sizeof(M), (LPDWORD)&byteswritten, 0);
  41.     for(int i =0; i<m2.n; i++)
  42.         printf("%c", m2.buffer[i]);
  43.     printf("\n");
  44.     m2.n = 0;
  45.         char kl;
  46.         while ((kl = _getch()) != VK_ESCAPE)
  47.         {
  48.             if (kl == VK_RETURN)
  49.             {
  50.                 printf("\n");
  51.                 //scanf("%s", m.buffer);
  52.                 //m.n = strlen(m.buffer + 1);
  53.                 int ifwrite = WriteFile(uchwyt, LPCVOID(&m), sizeof(M), (LPDWORD)&byteswritten, 0);
  54.                 if (!ifwrite)
  55.                     printf("Nie zapisano!\n");
  56.                 int ifread = ReadFile(uchwyt, LPVOID(&m2), sizeof(M), (LPDWORD)&byteswritten, 0);
  57.                 if (!ifread)
  58.                     printf("Nie przeczytano!\n");
  59.                 if (m2.n)
  60.                     for( int i =0; i<m2.n; i++)
  61.                         printf("%c", m2.buffer[i]);
  62.                 printf("\n");
  63.                        
  64. #ifdef DEBUG
  65.                 printf("\n%d\t%d", m.n, sizeof(m.buffer));
  66. #endif
  67.                 m.n = 0;
  68.                 m2.n = 0;
  69.                 //memset(m.buffer, 0, 80);
  70.                 //memset(m2.buffer, 0, 80);
  71.             }
  72.             else
  73.             {  
  74.                 printf("%c", kl);
  75.                 m.buffer[m.n] = kl;
  76.                 m.n++;
  77.             }
  78.         }
  79.        
  80.         return 0;
  81.    
  82. }
  83.  
  84. void GoToXY(int column, int line)
  85. {
  86.     COORD coord;
  87.     coord.X = column;
  88.     coord.Y = line;
  89.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  90.     if (!SetConsoleCursorPosition(hConsole, coord));
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement