linux下串口发送数据为什么会延迟一步啊?? 很郁闷。。

sulemon 2004-10-13 11:27:20
写了一段linux下操作串口GSM模块的程序,读写串口分别任read和write,但是有错误。
错误为:第一次向串口(write)命令后,read过来的是-1,以后每次write后,read过来的结果是上次写到串口里面的命令的返回值 和 这次向发送的命令。

为什么会延迟一步啊??还有,为什么回把自己写到串口的命令读回来啊?
我是真的没办法了。。。 求助阿
...全文
743 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxywh 2004-10-14
  • 打赏
  • 举报
回复
用了这句话了吗?
tcflush(fd,TCIFLUSH);//clear serial online data
shushusday 2004-10-13
  • 打赏
  • 举报
回复
试试发个别的信息过去验证..然后在发读取文件的内容
tengulre 2004-10-13
  • 打赏
  • 举报
回复
你在向端口写的时候,要停止读,并清空Buffer.读的时候停止写操作.

int getsms(int sim,char* pdu) /* reads a SMS from the SIM-memory 1-10 */
{ /* returns number of SIM memory if successful */
/* on digicom the return value can be != sim */
char command[500];
char answer[1024];
char* position;
char* beginning;
char* end;
char tmp[32];
int i;

if (strcmp(mode,"digicom")==0) // Digicom reports date+time only with AT+CMGL
{
writelogfile(LOG_INFO,"Trying to get stored message");
sprintf(command,"AT+CMGL=\"ALL\"\r");
put_command(command,answer,sizeof(answer),200,0);
/* search for beginning of the answer */
for (i=1; i<11; i++)
{
sprintf(tmp,"+CMGL: %i",i);
position=strstr(answer,tmp);
if (position)
{
writelogfile(LOG_INFO,"Found a message at memory %i",i);
sim=i;
break;
}
}
}

else
{
writelogfile(LOG_INFO,"Trying to get stored message %i",sim);
sprintf(command,"AT+CMGR=%i\r",sim);
put_command(command,answer,sizeof(answer),50,0);
/* search for beginning of the answer */
position=strstr(answer,"+CMGR:");
}

if (position==0) /* no SMS received, because modem did not answer with +CMGR or +CMGL */
return 0;

beginning=position+7;
if (strstr(answer,",,0\r")) /* No SMS, because Modem answered with +CMGR: 0,,0 */
return 0;

/* After that we have the PDU or ASCII string */
end=strstr(beginning,"\r");
if ((end==0) || ((end-beginning)<4))
return 0;
end=strstr(end+1,"\r");
if ((end==0) || ((end-beginning)<4))
return 0;
/* Now we have the end of the PDU or ASCII string */
*end=0;
strcpy(pdu,beginning);
return sim;
}
sulemon 2004-10-13
  • 打赏
  • 举报
回复
验证信息应该没用的吧??
因为每次写串口后,read过来的结果是上次写到串口里面的命令的返回值 和 这次发送的命令。

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧