2013-05-23 6 views
0

내 모바일에 SM5100b GSM (Rasberry Pi에 연결된 GSM)에서 메시지를 보내려는 다음 코드를 작성했습니다. 그것은 작동하지만 Okcomm, Ok, + CME ERROR : 4, Ok Cutecom 에뮬레이터를 열었을 때만 AT 명령의 결과를 확인할 수 있습니다. 이 코드에 "read"함수를 써서 한 줄씩 컴파일하는 동안 어떻게 결과를 얻을 수 있습니까? 나는 out = read (fd, n, sizeof (n))와 같은 것을 시도했지만 결과가 없었습니다. 나는 Raspian을 데비안 OS와 Codeblocks로 사용하고 있습니다.리눅스에서 C++ 코드로 AT 명령을 읽는 방법

#include <stdio.h> /* Standard input/output definitions */ 
#include <string.h> /* String function definitions */ 
#include <unistd.h> /* UNIX standard function definitions */ 
#include <fcntl.h> /* File control definitions */ 
#include <errno.h> /* Error number definitions */ 
#include <termios.h> /* POSIX terminal control definitions */ 


int open_port(void) 
{ 
int fd; /* File descriptor for the port */ 
int n,d,e,f,o,out; 
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); 
if (fd == -1) 
    { 
    /* Could not open the port. */ 
    perror("open_port: Unable to open /dev/ttyAMA0"); 
    } 
else 
    fcntl(fd, F_SETFL, 0); 
    sleep(2); 
    n = write(fd, "AT\r\n", 4); 
    if (n < 0) 
    fputs("write() of 4 bytes failed!\n", stderr); 
    sleep(2); 

    d = write(fd, "AT+CMGF=1\r", 10); 
    if (d < 0) 
    fputs("write() of 10 bytes failed!\n", stderr); 
    sleep(2); 

    e = write(fd, "AT+CMGS=\"6034****\"\r", 20); 
    if (e < 0) 
    fputs("write() of 20 bytes failed!\n", stderr); 
    sleep(2); 

    f = write(fd, "hello\r\x1A", 10); 
    if (f < 0) 
    fputs("write() of 10 bytes failed!\n", stderr); 
    sleep(2); 

    return (fd); 
    } 

    int main(void) 
    { 
    open_port(); 
    } 
+0

"컴파일하는 동안"- 컴파일 타임에 런타임 정보를 검사하려면 어떻게합니까? –

+0

또는 다른 사람에게 위와 같이 결과를 제공합니다. Ok, Ok, Ok, 컴파일 완료시 등. – dali1985

답변

0

당신은 같은 것을 할 것 sendAT처럼라는 함수를 만들 수 있습니다 : 당신은 모든 명령에 대해 같은 일을 중복 코드를 많이 가지고있는 순간

int sendAT(char* command,int fd) { 
    size_t cmdlen = strlen(command); 

    int n = write(fd,command, cmdlen); 
    if (n != cmdlen) 
     return -1 
    char reply[40]; 
    n = read(fd,reply,sizeof(reply)); 
    reply[n] = 0; //Terminate the string 
    if (strcmp(reply, "OK")==0) 
     return 0; //All went well 
    else 
     return -1; //Some error occurred 
} 

을 당신을 전화를 보냅니다.

+0

고마워요. 테스트를 위해 코드를 사용하겠습니다. 한 가지 더 궁금한 점은이 코드가 나에게 0이나 -1뿐만 아니라 결과를 OK, CME Error 등의 어떤 식으로 보낼 것인가? 어쩌면 내가 가질 수있는 오류를 정확하게 보내 주려고합니다. – dali1985

+0

매개 변수로 버퍼를 전달하여 읽기에 사용하면 호출 함수가 마지막 명령의 결과를 볼 수 있지만 모든 경우에 strcmp (buf, "OK")를 수행하므로 정수 반환 코드를 유지해야합니다. 명령은 다시 코드를 복제합니다. – LtWorf

관련 문제