I am trying to switch modem to command mode, write AT command and parse the response, but I am not too good with C and I do not understand what is wrong. The code I am calling is:
void switch_to_command_mode(int uart)
{
current_command_type = BTCommandTypeSwitchToATMode;
char switchCmd[] = "\x2F\x2F\x2F";
char emptySymbol[] = " ";
char checkAT[] = "AT\r";
ssize_t len = 0;
write(uart, emptySymbol, strlen(emptySymbol));
sleep(1);
write(uart, switchCmd, strlen(switchCmd));
sleep(1);
write(uart, checkAT, strlen(checkAT));
char buffer[255]; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes;
bufptr = buffer;
while (ioctl(uart, FIONREAD, (unsigned long)&len) == 0 && len < 2) {
warnx("waiting");
usleep(1000);
}
while ((nbytes = read(uart, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
warnx("buffer %s", buffer);
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
warnx("Final buffer: %s", buffer);
if (strncmp(buffer, "OK", 2) == 0) {
warnx("Great success!");
}
}
What I get as console output is:
waiting
waiting
buffer AT
O p �0
buffer AT
OK
�0
Final buffer: AT
OK
�0
According to documentation for the BT Module, the response should be in form of <CR><LF>OK<CR><LF>
.
Any guidance on why this is possibly happening and what can I do with that would be highly appreciated.
P.S. I also already went through Serial programming for POSIX OS.