2013-05-01 1 views
2

그렇게하고 싶습니다. 난 이미 예를 들어, 완벽하게 작동 시리얼 포트에 데이터를 쓸 수있는이 일을 몇 가지 기능을 가지고 :C++를 사용하여 Arduino에서 시리얼 데이터 읽기

bool WriteData(char *buffer, unsigned int nbChar) 
{ 
    DWORD bytesSend; 

    //Try to write the buffer on the Serial port 
    if(!WriteFile(hSerial, (void *)buffer, nbChar, &bytesSend, 0)) 
    { 
     return false; 
    } 
    else 
     return true; 
} 

읽기 기능은 다음과 같이입니다 : 아무리 내가 함께 무엇을

int ReadData(char *buffer, unsigned int nbChar) 
{ 
//Number of bytes we'll have read 
DWORD bytesRead; 
//Number of bytes we'll really ask to read 
unsigned int toRead; 

ClearCommError(hSerial, NULL, &status); 
//Check if there is something to read 
if(status.cbInQue>0) 
{ 
    //If there is we check if there is enough data to read the required number 
    //of characters, if not we'll read only the available characters to prevent 
    //locking of the application. 
    if(status.cbInQue>nbChar) 
    { 
     toRead = nbChar; 
    } 
    else 
    { 
     toRead = status.cbInQue; 
    } 

    //Try to read the require number of chars, and return the number of read bytes on success 
    if(ReadFile(hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0) 
    { 
     return bytesRead; 
    } 

} 

//If nothing has been read, or that an error was detected return -1 
return -1; 

} 

과 arduino,이 함수는 항상 -1을 반환합니다. 직렬 포트에 문자를 지속적으로 쓰는 코드를로드하려고 시도했지만 아무것도하지 않았습니다.

는 여기에서 기능을 가지고 :

http://playground.arduino.cc/Interfacing/CPPWindows 그래서 내 기능은 기본적으로 동일합니다. 방금 클래스 객체로 사용하는 대신 코드를 내 코드로 복사했습니다.

내 문제는 시리얼에 데이터를 쓸 수는 있지만 읽을 수 없다. 무엇을 시도 할 수 있는가?

+0

직렬 포트가 열리는 라인은 무엇입니까? 그리고'GetLastError'에 무엇이 있습니까? 장치가 실제로 당신에게 읽을 거리를 제공하고 있습니까? –

+0

@MatsPetersson hSerial = CreateFile이 (wText, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); hSerial은 HANDLE hSerial로 정의 된 변수입니다. 이게 네가 말하는거야? – MyUserIsThis

+0

@MatsPetersson 어디에서 문제가 발생하는지 파악하는 방법을 모른다. Arduino IDE의 직렬 모니터를 열면 기기에서 확실히 데이터를 전송합니다. 프로그램도 데이터를 보낼 수 있습니다. 문제는 그것을 읽는 것입니다. status.cbInQue 변수는 0이며 읽을 바이트 수 여야합니다. – MyUserIsThis

답변

0

관심있는 사람에게 이미 해결했고 바보 같은 실수였습니다. Arduino를 프로그래밍하여 아무것도 보내지 않고 시리얼 입력을 기다립니다. 컴퓨터 프로그램은 한 줄의 코드를 작성하고 보냅니다. i7이 Atmel보다 빠르다고 생각합니다. 분명히 데이터에는 시간이 걸립니다.

수면 추가 (10); 컴퓨터에서 포트를 reding하기 전에 데이터를 마지막으로 읽는 데 충분했습니다.

@Matts에게 감사드립니다.

관련 문제