Using echo at+b TEST >/dev/ttys1 to write data to the serial port what's going on?

Source: Internet
Author: User

Today, directly through the ADB shell Echo to the serial to write at data, obviously write echo at+b TEST >/dev/ttys1 went in, but the 3g module did not respond, this is what is going on. I use the wave quanta, as if there is data, but the module received is the normal response, to now do not know what is going on, super depressed AH. Prawns, look what's going on.

1. First post the initialization of the serial port:

void setcommattr (int rate) {
printf ("Enter setdefaultcommattr \ r \ n");
struct Termios iOS;
unsigned long i;


if (M_hdev = 0 | | m_hdev = = 1) {
printf ("M_hdev error");
Return
}

if (Tcgetattr (M_hdev, &ios) = = 1) {
printf ("tcgetattr \ r \ n");
Return
}
Cfmakeraw (&ios);
printf ("Baud rate is%d\r\n", rate);
i = B115200;


Cfsetispeed (&ios, i);
Cfsetospeed (&ios, i);


Ios.c_cflag |= (clocal | Cread);
Ios.c_cflag &= ~csize;




/*stop bits setting*/
Ios.c_cflag &= ~CSTOPB;


/*byte size setting*/
Ios.c_cflag |= CS8;

/*flow Control setting*/
Ios.c_cflag &= ~crtscts;


Ios.c_lflag &= ~ (Icanon | Iexten | Isig | ECHO);
Ios.c_oflag &= ~opost;
Ios.c_iflag &= ~ (ICRNL | INPCK | Istrip | Ixon | Brkint);
Ios.c_cc[vmin] = 1;
Ios.c_cc[vtime] = 0;


if (tcsetattr (M_hdev, Tcsanow, &ios) = = 1) {
printf ("Tcsetattr 2 \ r \ n");
Return
}
Tcflush (M_hdev, Tcioflush);
printf ("Leave setdefaultcommattr\r\n");

}

Feel this is no problem, because the serial port is OK to collect data.

2. Open the serial port as:

int Openport ()
{
if (!m_sem_thread_init) {
M_sem_thread_init = 1;
int isbaudratereset = 0;
if (M_hdev!= 0)
{
return port_still_opening;
printf ("M_hdev!= 0, close a and reopen \ r \ n");
Closeport ();
}
Char uartval[50]={"/dev/ttys1"};
if (strlen (uartval) <= 2)
{
printf ("uartsetting read error \ r \ n");
return-1;
}
printf ("Port is%s", uartval);
M_hdev = open (Uartval, O_RDWR | O_noctty);
if (M_hdev < 0) {
printf ("Open%s failed\r\n", uartval);
M_hdev = 0;
return-1;
}
Setcommattr (0);
Isbaudratereset = 0;


Sem_init (&m_sem_data_thread_start, 0, 0);
Sem_init (&m_sem_data_exit, 0, 0);
Sem_init (&m_sem_data_thread, 0, 0);
Sem_init (&m_sem_baudrate_set, 0, 0);
Sem_init (&m_sem_wait_data,0, 0);




pthread_attr_t thread_attr;
pthread_t Newthread;

Pthread_attr_init (&AMP;THREAD_ATTR);
Pthread_attr_setstacksize (&thread_attr,128*1024);
int ret = Pthread_create (&newthread, &thread_attr, Rxdatathread, NULL);
if (ret!= 0) {
printf ("Pthread_create _rxdatathread () err!\r\n");
Close (M_hdev);
return-1;
}
Pthread_detach (Newthread);
Sem_wait (&m_sem_data_thread_start);
unsigned char send_buf[50]= "at+b inqu 1\r";
Writeport (Send_buf,strlen (const char *) SEND_BUF);
Add File Office
Sem_wait (&m_sem_wait_data);
}
return 0;
}

3. Add the following in the red section below but it is OK, why. Everybody take a look.

{

while (1)
{
int ret;
memcpy (Send_buf, "\ r", strlen (const char *) send_buf) +1;//memcpy (Send_buf, "At+b test\r", strlen (const char *) Send_ BUF) +1);
ret = Writeport (send_buf,strlen (const char *) SEND_BUF));
Logmsg (1, "liuxd:sendstr=%s", send_buf);
Logmsg (1, "liuxd:sendack=%d", ret);
Sleep (1);
}
}


4. In order to ensure the integrity of the code will be rxdatathread paste on it.

void * Rxdatathread (void* lParam)
{
unsigned char* buf = m_rxd_buf + m_bytes_in_buf;
int count = 0;
int remain, r, RC;
struct Timeval TV;
Fd_set Readfds;
int MAX_FD = M_hdev + 1;

printf ("Enter rxdatathread \ r \ n");
Sem_post (&m_sem_data_thread_start);


while (Sem_trywait (&m_sem_data_exit)) {
Fd_zero (&readfds);
Fd_set (M_hdev, &readfds);
Tv.tv_sec = 1;
tv.tv_usec = 0;
rc = Select (MAX_FD, &readfds, NULL, NULL, &AMP;TV);
if (rc <= 0 | |! Fd_isset (M_hdev, &readfds)) {
Continue
}


memset (M_rxd_buf,0,m_rxd_len);
if ((count = Read (M_hdev, M_rxd_buf, m_rxd_len)) = = 1) {
printf ("Rxdatathread return-1.\n");
Logmsg (1, "Liuxd:rxdatathread return-1.\n");
Break
}
if (count = = 0) {
Continue
}


{
#define At_raw_len (m_rxd_len+72)
int i;
static unsigned char at_raw_data[at_raw_len]={0};
static unsigned char tmp_buf[at_raw_len]={0};
static int totol = 0;
unsigned char *p1=0;
unsigned char *p2=0;
Push data;
memcpy (&at_raw_data[totol],m_rxd_buf,count);
Totol + = count;
if (Totol > At_raw_len)
{
Logmsg (1, "liuxd:sorry_overflow,%s,%d", __file__,__line__);
Process Gabig
Totol = 0;
memset (At_raw_data,0,at_raw_len);
Continue
}
while (1)
{
p1= (unsigned char *) strstr ((char *) at_raw_data, "At-b");
p1= (unsigned char *) strstr ((char *) at_raw_data, "\ r");
if (p1)
{
Logmsg (1, "liuxd:match-up");
}
Else
{break;}
p2= (unsigned char *) strstr ((char *) at_raw_data, "\ r");
p2= (unsigned char *) strstr ((char *) p1, "\ r");
if (p2)
{
Logmsg (1, "Liuxd:match-down");
if (P2&LT;=P1)
{
Logmsg (1, "liuxd:match-down-error=%d", P1-P2);
{
P1=at_raw_data;
int len=p2-p1+1;
Logmsg (1, "liuxd:unknown,len=%d", Len);
memset (Tmp_buf,0,at_raw_len);
memcpy (Tmp_buf,at_raw_data,len);
tmp_buf[len]=0;
Logmsg (1, "liuxd:unknown=%d####, str=%s", len,tmp_buf);
if (tmp_buf[0] = = ' A ' && tmp_buf[3] = = ' B ' && tmp_buf[len-1] = = ' \ r ')
{Bti50eapp (Tmp_buf,len);}
Else
{logmsg (1, "Liuxd:un-bti50eapp");}
Totol-= Len;
memset (Tmp_buf,0,at_raw_len);
memcpy (Tmp_buf,p2+1,totol);
memcpy (At_raw_data,tmp_buf,at_raw_len);
}
Continue
}
}
Else
{break;}
}
}
}

Sem_post (&m_sem_data_thread);
Sem_post (&m_sem_wait_data);

printf ("Leave rxdatathread \ r \ n");
return NULL;
}



The reason for this problem is believed that we all know, is not send a carriage return, so we please remember each instruction at the end of the add \ r

Echo-r-N "at\r" >/dev/ttyusb1

Of course this is something;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.