2014-09-22 79 views
0

我試圖通過串行線將一些Hayes命令(AT命令)發送到我的調制解調器。 我打電話的功能是post_request::open()。在此方法中,有一個靜態命令字符串數組,其中包含用於配置連接配置文件的命令。 我的第一個問題是:我應該在創建命令列表時使用更「靈活」的方法嗎? 我總是需要將這八個命令發送到我的調制解調器。當然,url,content_type和content_length會有所不同。 也許有人可以告訴我一個更好的方法來做到這一點。立即發送完整的字符串數組

int post_request::open(const char *url, unsigned content_length, const char *content_type) 
{ 
    static const char *commands[] = 
    { 
     // Connection profile 
     "AT^SISS=0,conId,0;", 
     // HTTP 
     "AT^SISS=0,srvType,Http;", 
     // User Agent 
     "AT^SISS=0,hcUsrAgent," USER_AGENT_STRING ";", 
     // HTTP method 
     "AT^SISS=0,hcMethod,1;", 
     // Placeholder for modem type bdx80 
     "AT;", 
     // Placeholder for address 
     "AT;", 
     // Placeholder for number of bytes sent 
     "AT;", 
     // Placeholder for content type 
     "AT;", 
     // Open internet session with configured profile 
     "AT^SISO=0\r", 
     NULL 
    }; 

    // Some code... 
    if (modem->modem_type == bdx80) 
     commands[4] = "AT^SISS=0,secOpt,-1;"; 

    // Some more code... 
    commands[6] = "AT^SISS=0,hcContLen,",content_length,";"; 

    // Code for content_type settings... 

    int error = send_commands(modem, timeout, commands); 
    if (error) 
     return error; 

} 

當我完成連接配置文件設置後,我打電話給send_commands()。 我有一個第三方庫,它的uart傳輸&接收東西。 我在send_commands()內調用的函數是uart_tx()。 問題:我需要做什麼,請正確撥打uart_tx()?我希望一次發送完整的命令列表。

THX

static int 
send_commands(modem_t *modem, unsigned timeout, const char *commands[]) 
{ 
    // determine size of commands pointer array 
    unsigned len = ???; 
    // Send commands through serial line 
    if (uart_tx(modem->port, ???, &len, timeout)) 
     return TIMEOUT; 
} 


/** 
* Sends count bytes. 
* @param port  The serial port. 
* @param buf[in] Pointer to the buffer containing the bytes to be sent. 
* @param count[in,out] Pointer to the value containing the number of bytes to send 
*   (in) and the number of bytes actually sent (out). 
* @param time_to_wait The maximum amount of time the task should block waiting 
*  for count bytes to be sent should the transmit queue be full at some time. 
* @return 0 on successful transmission, 1 on timeout 
*/ 
unsigned uart_tx(SerialPort port, const void *buf, unsigned *count, unsigned time_to_wait) 
+0

您是否想將數組中的字符串連接到一個字符串,並使用一個命令發送,還是可以通過多個'uart_tx'調用一個一個地發送它們嗎? – hyde 2014-09-22 09:56:13

+0

另請注意,命令[6] =「AT^SISS = 0,hcContLen」,content_length,「;」;'不會做你認爲的事......逗號不會連接C中的字符串。緩衝區(或者是本地'char buf [足夠];'如果局部變量的生命週期對於你來說已經足夠了,或者用'char * buf = malloc(足夠的)'來分配;'然後你需要使用'snprintf(buf, ....)'來構造你的命令字符串,歡迎來到C字符串handlinng的美妙世界;) – hyde 2014-09-22 10:07:03

+0

@hyde我想連接字符串並用一個命令發送它。 Thx用於發現錯誤 – trek 2014-09-22 10:27:58

回答

0

好像你要送他們之前正好連接(或者的memcpy或的strcat什麼)中的所有命令到一個單一的緩衝區。

+0

我認爲他應該等待調制解調器發送命令之間的OK響應。 – hyde 2014-09-22 09:33:41

+0

這不是一個真正的答案 - 它應該只是一個評論。 – 2014-09-22 09:39:15

+1

@hyde:我不會將每一條命令發送到調制解調器並等待響應。我希望他們組合在一起,只接受一個單一的響應整個命令。這些命令已經準備好了。最後一個命令與'\ r'一起發送,這就是調制解調器如何識別AT序列的結尾 – trek 2014-09-22 09:44:01