我現在有一塊7444的開發板,想實現7444通過串口傳輸命令給7444開發板上的另一模塊cc2530(zigbee中的協調器),然後發送給給第三方開發板(只要當前cc2530能接收到命令就能直接發送數據到zigbee終端,以實現),然後終端zigbee模塊的溫度數據回傳到7444開發板上的CC2530協調器,再從串扣回傳給7445,已知我當前的串口設備文件是tty1,怎麼編寫C程序讀取改開發板上cc2530傳來的溫度數據,和通過串口給cc2530發送命令!
上圖是zigbee協議棧的串口程序,主要是
1、 接收到數據
2、 判斷長度然後給數據包 pMsg 分配內存
3、 打包發送給上層 OSAL待處理
4、 釋放內存
其中數據包中的數據部分格式是
datalen + data
本來CC2530是個獨立的zigbee模塊,直接通過RS232轉串口可以直接在串口工具中顯示終端發送來的溫度數據和寫入命令控制終端了,而現在在PC和CC2530之間加了一個arm的CPU,這個cpu和CC2530之間是通過串口連接通信的,問題就出現在這裡了,這部分我沒接觸過個,所以不會,現在求個大神幫忙解決下。
好吧 我貼個以前寫的代碼給你看看!!裡面有設置串口協議的 反正自己留著也沒用給大家看吧 這個是linux C的
int setS0(void)
{
int fd=0,ret=0;
struct termios old,new;
fd = open("/dev/ttyS0",O_RDWR); //U轉串
if(fd == -1)
{
perror("open");
//exit(0);
}
int g = tcgetattr(fd,&old);
if(g == -1)
{
printf("COM get fail\n");
//exit(0);
}
new = old;
cfsetispeed(&new,B9600);
cfsetospeed(&new,B9600);
new.c_cflag |= (CLOCAL | CREAD);
new.c_cflag &= ~PARENB;
new.c_cflag &= ~CSTOPB;
new.c_cflag &= ~CSIZE; //取消原有設置
new.c_cflag |= CS8;
tcsetattr(fd,TCSANOW,&new); //立即生效
return fd;
}
int main()
{
//char chr[350]="0891683108200945F7240D91688186929197F5000831116001516423044F60597D4F60597D";
char chr[350]={'\0'};
int a=0,b=0,ret=0;
char ch[350]="",ch1[25]="",ch2[25]="",ch3[25]="";
char *pn=NULL;
int fd=setS0(); //設置端口
char *p=NULL, *q=NULL;
write(fd,"ate0\r",5); //關閉回顯
sleep(1);
write(fd,"at+cmgf=0\r",10); //設置讀取格式
sleep(1);
tcflush(fd, TCIOFLUSH);
sleep(1);
ret = 0;
while(1)
{
//*
ret=read(fd,ch3+strlen(ch3),20-strlen(ch3));
if(ret <= 0)
continue;
printf("read===[%s]\n",ch3);
if(strstr(ch3,"+cmti:") == NULL)
{
printf("未發現新短信信息\n");
sleep(2);
continue;
}
else
{
p=strchr(ch3,',');
if(p == NULL)
continue;
q = strchr(p, '\r');
if(q == NULL)
q = strchr(p, '\n');
if(q == NULL)
continue;
memset(ch2,'\0',25);
printf("len = %d\n",q-p-1);
memcpy(ch2,p+1,(q-p-1));
write(fd,"at+cmgr=",strlen("at+cmgr="));
write(fd,ch2,strlen(ch2));
write(fd, "\r", 1);
printf("write:===[at+cmgr=%s]\n",ch2);
sleep(1);
memset(chr,'\0',sizeof(chr));
ret = read(fd,chr,350);
printf("read_ret: %d\n", ret);
printf("rcv mesg is [%s]\n",chr);
//*/
//9168
//08
//p = strstr(chr, "9168"); //查找9168
p = strstr(chr, "9168");
if(p == NULL)
{
printf("can fand 9168\n");
exit(0);
}
pn=p-2;
printf("%s\n",pn);
printf("ch2:%s\n",ch2);
memcpy(ch2,pn,2);
sscanf(ch2,"%02X",&a);
a=(a*2-4);
printf("a===%d\n",a);
memset(ch,'\0',350);
memcpy(ch,pn+6,a);
printf("ch====%s\n",ch);
change(ch);
ch[strlen(ch)-1]='\0';
printf("center num==%s\n",ch);
memset(ch2,'\0',25);
memcpy(ch2,pn+8+a,2);
//printf("0d==%s\n",ch);
sscanf(ch2,"%02X",&b);
printf("來源號碼長度=%d\n",b);
memset(ch1,'\0',25);
memcpy(ch1,(pn+(14+a)),b);
change(ch1);
ch1[strlen(ch1)-2]='\0';
printf("message is from :%s\n",ch1);
memset(ch1,'\0',25);
memcpy(ch1,(pn+(17+a+b)),14);
puts(ch1);
change(ch1);
int year=0,month=0,day=0,hour=0,mine=0,secort=0;
sscanf(ch1,"%2d%2d%2d%2d%2d%2d",&year,&month,&day,&hour,&mine,&secort);
printf("send messages time is :%02dyear%02dmonth%02dday\t%2d:%2d:%2d\n",year,month,day,hour,mine,secort);
memset(ch,'\0',350);
memcpy(ch,pn+(a+b+17+14),2); //拷貝信息長度16進制
printf("16len===%s\n",ch);
sscanf(ch,"%02X",&a);
printf("rcv message len is: %d bit\n",a);
memset(ch,'\0',350);
memcpy(ch,pn+(a+b+41),350);
printf("ch: [%s]\n", ch);
char *s=unicode_utf8(ch);
printf("rcv message is: [%s]\n",s);
}
}
}
/*___________________________changed_______________________________*/
char change(char *p)
{
int i=0,j=0;
char tem[2]={'\0'};
for(j=0;j<strlen(p)/2;j++)
{
tem[0]=(p+i);
(p+i)=(p+i+1);
*(p+i+1)=tem[0];
i+=2;
}
printf("p==%s\n",p);
}
char * unicode_utf8(char *ch) //----------------解碼函數
{
int x=0;
int n;
int unicode_len=strlen(ch); //計算傳入的字符長度
wchar_t wch[70]=L"\0";
char *utf=(char *)malloc(70);
setlocale(LC_ALL,"zh_CN.UTF-8"); //設置本地編碼格式
for(x=0;x<unicode_len-1;x++)
{
sscanf(ch+4*x,"%04X",wch+x); //將十六進制轉換成寬整形
}
*(wch+x)='\0'; //在寬整形後面追加上結束標志
n=wcstombs(utf,wch,unicode_len); //將寬整形轉換成漢字,讓utf指針指向他
*(utf+x)='\0';
puts(utf);
return utf; //返回指針
}