Codice PHP:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
void stampa(void *Rx,FILE *fp); //stampa dei dati ricevuti IN: handle della porta OUT:niente
void inizializza(HANDLE Rx); // setting della porta IN: handle della porta OUT:niente
int main() //inizio main
{
FILE *fp;
char *NomePorta={"COM1:"};
OVERLAPPED osWrite = {0};
DWORD dwNumBytesWritten,zero=0;
HANDLE Rx;
int i=1,scelta=0;
// apertura file dove scrivere i dati
fp=fopen("Dati.txt","w");
// Creazione dell handle relativo all apertura della porta in lettura/scrittura(COM1)
Rx = CreateFile(NomePorta,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
zero,
0);
if (Rx == INVALID_HANDLE_VALUE)
{exit(0);}// error opening port; abort
inizializza(Rx); // inizializzazione porta COM1
printf("Inserire numero di misure da fare\n");
scanf("%d",&scelta);
for(i=0;i<=scelta;i++)
{
fprintf(fp,"Misura %d\n\n",i);
WriteFile(Rx,"S A\n",4,&dwNumBytesWritten,&osWrite); // invio stringa S A, misura su tutti i sensori
stampa(Rx,fp); // stampa su file dei risultati
}
fclose(fp);
CloseHandle(Rx);
return 0;
} // FINE MAIN
void stampa(void * Rx,FILE *fp) // INIZIO FUNZIONE STAMPA
{
DWORD dwBytesTransferred=0;
char prova;
int conteggio=0,numero=3;
fprintf(fp,"Sensore %d\n",numero);
numero--;
while(prova!='>')
{
ReadFile (Rx, // Port handle
&prova, // Pointer to data to read
1, // Number of bytes to read
&dwBytesTransferred, // Pointer to number of bytes read
NULL // Must be NULL for Windows CE
);
if(dwBytesTransferred!=0)
{
if(isdigit(prova)||prova=='\n') // stampa solo i numeri o i \n
{
if(prova=='\n')
conteggio++;
fprintf(fp,"%c",prova);
if(conteggio==2&&numero>=0)
{
fprintf(fp,"Sensore %d\n",numero);
conteggio=0;
numero--;
}
}
}
}
} // FINE FUNZIONE STAMPA
void inizializza(HANDLE Rx) //INIZIO FUNZIONE INIZIALIZZA
{
DWORD timeout=1000;
DCB PortDCB;
GetCommState(Rx,&PortDCB); // Preleva i setting attuali della porta
// setting dei paramentri (9600 8N1)
PortDCB.BaudRate = 9600; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = FALSE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = FALSE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
SetCommState(Rx,&PortDCB); // Setting della porta
} // FINE FUNZIONE INIALIZZA