Witam ponowanie. Znalazlem w internecie ten kod:
#include <windows.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
using namespace std;
byte keys[256] = {0};
// otworzenie czytywania klawiatury
bool scankeys(PBYTE keys, int num=-1) {
if(num>0)
keys[num]=GetAsyncKeyState(num);
else
for(int i=0; i<256; i++) keys[i]=GetAsyncKeyState(i);
return true;
}
int main()
{
HANDLE hCom; //uchwyt portu
DCB dcb; //konfiguracja portu
BOOL fSuccess; //flaga pomocnicza
INT RS_buf; //bufor danej
DWORD RS_ile; //ilosc bitow wyslanych
int numer;
//otwarcie portu COM1 z prawami RW
hCom = CreateFile( TEXT("COM2"), GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // default security attributes
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
printf("Brak polaczenia z portem nr. %u !! Blad: %d.\n", "COM15", GetLastError());
system("PAUSE");
return 1;
}
//pobranie aktualnych ustawien portu
fSuccess = GetCommState(hCom, &dcb);
//ustawienie naszej konfiguracji
dcb.BaudRate = CBR_9600; // predkosc transmisji
dcb.ByteSize = 8; // ilosc bitow danych
dcb.Parity = NOPARITY; // brak bitu parzystosci
dcb.StopBits = ONESTOPBIT; // dwa bity stopu
fSuccess = SetCommState(hCom, &dcb);
while ( !keys[0] ) {
scankeys(keys);
// od tego momentu wpisujesz dane które ma ci wysłać np. RS_buf=107
for(int i=0;i<256;i++) if(keys[i])
{
if (i ==115) {
RS_buf=107;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==116) {
RS_buf=108;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==117) {
RS_buf=109;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==118) {
RS_buf=110;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==119) {
RS_buf=111;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==120) {
RS_buf=112;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==121) {
RS_buf=113;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==122) {
RS_buf=114;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
if (i ==123) {
RS_buf=115;
fSuccess = WriteFile( hCom, &RS_buf, 1, &RS_ile, 0);
}
}
}
return 0;
}
Program kompiluje się, jednak za kazdym razem nie moze się połączyć z portem, mógłby mi wyjaśnić ktoś dlaczego?