Добавил:
Mymnan
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Сборка Часть1 / avmis_labs / неразобрано / 501 / Лаб_СОМ / Солодуха / COM_1
.cpp#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#define PORT "COM1"
int Write(HANDLE H,char c)
{
DWORD Byte;
if(!WriteFile(H, &c, 1, &Byte, NULL))
{
puts("Error write");
_getch();
return 0;
}
return 1;
}
int Read(HANDLE H, char &c)
{
DWORD Byte;
if(!ReadFile(H, &c, 1,&Byte,0))
{
puts("Error read");
_getch();
return 0;
}
return 1;
}
int main()
{
HANDLE handle;
handle = CreateFileA(PORT,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0,NULL);
if(handle == INVALID_HANDLE_VALUE)
{
puts("Error1");
_getch();
return 0;
}
char c;
int flag=0;
while(1)
{
system("cls");
printf("Press 1 - sinhron\nPress 2 - asinhron");
c = _getch();
//printf("%d",c);
if(!Write(handle,c))
return 0;
if(c=='1'||c=='2')
break;
}
if(c=='1')
flag=1;
system("cls");
puts("Vvedite text: \n");
char *ms;
ms=(char*)calloc(5,sizeof(char));//с отчиской
ms[5]='\n';
while(1)
{
c = _getch();
ms[0]=ms[1];
ms[1]=ms[2];
ms[2]=ms[3];
ms[3]=c;
if(c==13)
printf("\n");
else
printf("%c",c);
if(!Write(handle,c))
return 0;
if(!strcmp(ms,"exit"))
{
_getch();
break;
}
if(flag)
{
char a;
while(1)
{
if(Read(handle,a))
{
if(a==c)
break;
}
if(a==c)
break;
}
}
}
return 0;
}