I need help. I'm developing a easy project to test some commands, but, my program in C doesn't change information with the arduino.
Here the codes
------*-*ARDUINO CODE -----*-**-
const int LM35 = A0;
const int BUFFER_SIZE = 1000;
const float CELSIUS_BASE = 0.4887585532746823069403714565; //Base de conversão para Graus Celsius ((5/1023) * 100)
const float REFRESH_RATE = 2000;char dataInput[20]; // Aloca espaço para a string
char charInput; // Para guardar o caractere lidovoid setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
}void loop(){
if(Serial.available() > 0){ //Espera ter informação
delay(250);
for(int i=0;i<19;i++){ //Lê a informação do tamanho do espaço para a string
if(Serial.available() > 0){ //Só manipula a string se tiver informação
charInput = Serial.read(); //Lê um caractere
dataInput = charInput; //Armazena na string
dataInput[(i+1)] = '\0'; //Finaliza a string na próxima posição*
}*
}*
//Terminou de ler a string, agora processa*
if(!strcmp(dataInput,"temp")){ //Se a informação requisitada for a temperatura*
//Envia a temperatura*
Serial.print("Temperatura: ");*
Serial.println(readTemperature());*
Serial.print("Temperatura com buffer: ");*
Serial.println(readTemperatureWithBuffer());*
}*
else{ //Quando o comando é inválido, se não for temp*
Serial.print("Comando inválido: ");*
Serial.println(dataInput);*
}*
}*
delay(REFRESH_RATE);*
}
float readTemperature(){
return (analogRead(LM35) * CELSIUS_BASE);
}
float readTemperatureWithBuffer(){float buffer = 0;*
for (int i = 0; i < BUFFER_SIZE; i++){*
buffer += analogRead(LM35);*
}*
return ((buffer/BUFFER_SIZE) * CELSIUS_BASE);
}[/quote]
and the C Code
> #include <windows.h>
> #include <stdio.h>
>
> // Ler caractere
> char SerialGetc(HANDLE *hCom)
> {
> char rxchar;
> BOOL bReadRC;
> static DWORD iBytesRead;
>
> bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);
> return rxchar;
> }
>
> // Ler string
> char* SerialGets(HANDLE *hCom)
> {
> static char rxstring[256];
> char c;
> int pos = 0;
>
> while(pos <= 255)
> {
> c = SerialGetc(hCom);
> if (c==13) break;
> if(c == '\r') continue; // discard carriage return
> rxstring[pos++] = c;
> if(c == '\n') break;
>
> }
> rxstring[pos] = 0;
> return rxstring;
> }
>
> void SerialPuts(HANDLE *hCom, char *txstring)
> {
> BOOL bWriteRC;
> static DWORD iBytesWritten;
> bWriteRC = WriteFile(*hCom, txstring, strlen(txstring), &iBytesWritten,NULL);
> }
*> *
>
> int main(int argc, char *argv[])
> {
> char vetor[20];
> DCB dcb;
> HANDLE hCom;
> BOOL fSuccess;
> LPCSTR LpcCommPort = "COM3";
>
> hCom = CreateFile( LpcCommPort,
> GENERIC_READ || GENERIC_WRITE, // | GENERIC_WRITE,
> 0, // must be opened with exclusive-access
> NULL, // no security attributes
> OPEN_EXISTING, // must use OPEN_EXISTING
> 0, // not overlapped I/O
> NULL // hTemplate must be NULL for comm devices
> );
>
> if (hCom == INVALID_HANDLE_VALUE)
> {
> // Handle the error.
> printf ("CreateFile failed with error %d.\n", GetLastError());
> return (1);
> }
>
> // Build on the current configuration, and skip setting the size
> // of the input and output buffers with SetupComm.
>
> fSuccess = GetCommState(hCom, &dcb);
>
> if (!fSuccess)
> {
> // Handle the error.
> printf ("GetCommState failed with error %d.\n", GetLastError());
> return (2);
> }
>
> // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
>
> dcb.BaudRate = CBR_9600; // set the baud rate
> dcb.ByteSize = 8; // data size, xmit, and rcv
> dcb.Parity = NOPARITY; // no parity bit
> dcb.StopBits = ONESTOPBIT; // one stop bit
>
> fSuccess = SetCommState(hCom, &dcb);
>
> if (!fSuccess)
> {
> // Handle the error.
> printf ("SetCommState failed with error %d.\n", GetLastError());
> return (3);
> }
>
> printf ("Serial port successfully reconfigured.\n");
*> *
> //CÓDIGO PARA ENVIAR UMA REQUISIÇÃO E LER DO ARDUINO UMA VEZ
> printf ("Requisitando temperatura...\n");
> SerialPuts(hCom,"temp");
> printf("teste");
> Sleep(1000);
> printf ("Resposta: %c", SerialGets(hCom));
> //FIM
*> *
> CloseHandle(hCom);
> system("PAUSE");
> return (0);
> }The C program crashes when i call the SerialPuts and the SerialGets functions.