ecco!! ora che ho impostato tutto va un pelo meglio
nel senso che, se eseguo il programma e premo i tasti della seriale vedo che sul programma stampa degli spazi... in realtà dovrebbe stampare delle lettere.. e poi non tutti i tasti vanno...
che cosa può essere???
metto il codice. anche se sta diventando un pò lungo..
codice:
#include "windows.h"
#include "conio.h"
#include "stdio.h"
BOOL setComm(HANDLE hComm)
{
DCB dcb;
BOOL res;
FillMemory(&dcb, sizeof(dcb), 0);
res = GetCommState(hComm, &dcb); // get current DCB
if (!res)
// Error in GetCommState
return FALSE;
printf("\nBaudRate %d ByteSize %d StopBits %d\n", dcb.BaudRate, dcb.ByteSize, dcb.StopBits);
// Update DCB rate.
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.BaudRate = CBR_9600;
dcb.Parity =NOPARITY;// none
dcb.fAbortOnError = 0; // no abort
// Set new state.
res = SetCommState(hComm, &dcb);
// Error in SetCommState. Possibly a problem with the communications
// port handle or a problem with the DCB structure itself.
if (!res)
return FALSE;
DWORD err = GetLastError();
if ( err)
printf("\n ERROR % d\n", err);
return TRUE;
}
int main(int argc, char* argv[])
{
bool Ok;
HANDLE myPortH= CreateFile(
"COM3",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if (myPortH == INVALID_HANDLE_VALUE)
{
printf("error opening\n");
// error opening port; abort
getch();
return -1;
}
else
{
printf("COMM Open\n\n");
BOOL bResult;
bResult = setComm(myPortH);
for(;; )
{
char c;
DWORD NumberOfBytesRead;
Ok = ReadFile(myPortH,
&c, // pointer to buffer that receives data
1, // number of bytes to read
&NumberOfBytesRead, // ptr. to n. of bytes read
NULL
);
if(Ok==FALSE){printf("error");}
if (NumberOfBytesRead > 0)
printf("%c", c);
int keydown = kbhit();
if (keydown)
break;
}
getch();
CloseHandle(myPortH);
return 0;
}
}