bool BTIsPresent() { UART_SEND(BT_EOC); UART_SEND("AT"); UART_SEND(BT_EOC); return BTCheckRestponseOk(); }
bool BTSetDataBaudrate(unsigned long baudrate) { char cmd[MAX_CMD_LEN]; UART_SEND(BT_EOC); snprintf( cmd, MAX_CMD_LEN, "AT+UART=38400,0,0"); UART_SEND(cmd); UART_SEND(BT_EOC); return true; }
bool BTSetRole(char mode) { char cmd[MAX_CMD_LEN]; int cmd_len; UART_SEND(BT_EOC); cmd_len = snprintf( cmd, MAX_CMD_LEN, "AT+ROLE=%d", mode); UART_SEND(cmd); UART_SEND(BT_EOC); return true; }
bool BTSetName(char * name) { char cmd[MAX_CMD_LEN]; int cmd_len; UART_SEND(BT_EOC); cmd_len = snprintf( cmd, MAX_CMD_LEN, "AT+NAME=%s", name); UART_SEND(cmd); UART_SEND(BT_EOC); return true; }
// the isr's... static void uart_tx_irq( void ) { unsigned char c; { uart_get_tx_data_type handler; BSP_CRITICAL_STATEMENT( handler = uart_tx_handler ); // if not currently in suspend mode and a handler exists if( uart_tx_suspend == false && handler != NULL ) { if( (*handler)( &c ) != false ) // if data available, reset the interrupt { UART_SEND( UART_NUMBER, UART_LOCATION, c ); // send the byte } else // if no data suspend transmission { uart_tx_message_suspend( handler ); UART_IRQ_FLAG_SET( UART_NUMBER, UART_LOCATION, TX ); } } else { bspIState_t istate; BSP_ENTER_CRITICAL_SECTION( istate ); // if we are in suspended mode or we just sent an xon or xoff character // while transmits were disabled or the message has been completely sent, // then simply disable irq's so we don't get stuck in an infinite loop UART_IRQ_DISABLE( UART_NUMBER, UART_LOCATION, TX ); UART_IRQ_FLAG_SET( UART_NUMBER, UART_LOCATION, TX ); BSP_EXIT_CRITICAL_SECTION( istate ); } } return; }
/****************************************************************************** * @fn uart1_tx_irq * * @brief TX interrupt service routine * * input parameters * * output parameters * * @return */ void uart0_tx_irq( void ) { unsigned char c; uart_get_tx_data_type handler; BSP_CRITICAL_STATEMENT( handler = uart0_tx_handler ); /* if a handler exists */ if( handler != NULL ) { if( (*handler)( &c ) != false ) /* if this is not the last byte to send */ { bspIState_t intState; BSP_ENTER_CRITICAL_SECTION( intState ); /* only reset the interrupt flag if we have additional data to send * that way, if we are done then the interrupt is still pending and * will be immediately entered upon re-enabling it.*/ UART_IRQ_FLAG_CLR( UART_NUMBER_0, UART0_LOCATION, TX ); /* eset the interrupt */ BSP_EXIT_CRITICAL_SECTION( intState ); } else { bspIState_t intState; BSP_ENTER_CRITICAL_SECTION( intState ); /* we're done sending data. since we left the interrupt pending, * disable it so we don't re-enter the isr. the interrupt will be * re-enabled when there is another message to send. */ UART_IRQ_DISABLE( UART_NUMBER_0, UART0_LOCATION, TX ); /* no more data to send, reset the handler to flag not busy */ uart0_tx_handler = NULL; BSP_EXIT_CRITICAL_SECTION( intState ); } UART_SEND( UART_NUMBER_0, UART0_LOCATION, c ); /* send the byte */ } else /* if no handler exists?!?!?!? */ /* something went wrong, disable interrupts so we don't get stuck here */ UART_IRQ_DISABLE( UART_NUMBER_0, UART0_LOCATION, TX ); return; }
/*主函数体*/ int main(int argc, char **argv) { int fd; //文件描述符 int err; //返回调用函数的状态 int len; int i; char rcv_buf[200]; char send_buf[30]="**This's a GPS Locator!**\n"; int listen_fd,accept_fd; struct sockaddr_in client_addr; int n; // int nbytes; if(argc != 3){ printf("Usage: %s /dev/ttyS* s-Send/r-Receive\n",argv[0]); return FALSE; } fd = UART_OPEN(fd,argv[1]); //打开串口,返回文件描述符 do{ err = UART_DEFAULT(fd); }while(FALSE == err || FALSE == fd); printf("Port is READY!\n"); if((listen_fd=socket(AF_INET,SOCK_STREAM,0))<0) //创建socket { printf("Socket Error:%s\n\a",strerror(errno)); return FALSE; } bzero(&client_addr,sizeof(struct sockaddr_in)); client_addr.sin_family=AF_INET; client_addr.sin_port=htons(MY_PORT); client_addr.sin_addr.s_addr=htonl(INADDR_ANY); n=1; setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(int)); //如果服务器终止后,服务器可以第二次快速启动而不用等待一段时间 if(bind(listen_fd,(struct sockaddr *)&client_addr,sizeof(client_addr))<0) //Bind { printf("Bind Error:%s\n\a",strerror(errno)); return FALSE; } listen(listen_fd,MAX_CONN_NUM); if(0 == strcmp(argv[2],"s")) { for(i = 0;i < 50;i++) { len = UART_SEND(fd,send_buf,30); if(len > 0) printf("Count.%d :Send data successful!\n",i); else printf("Count.%d :Send data failed!\n",i); sleep(1); } } else if(0 == strcmp(argv[2],"r")) { while (1) //循环读取数据 { accept_fd=accept(listen_fd,NULL,NULL); if((accept_fd<0)&&(errno==EINTR)) continue; else if(accept_fd<0) { printf("Accept Error:%s\n\a",strerror(errno)); continue; } if((n=fork())==0) { /* 子进程处理客户端的连接 */ unsigned int i; for(i=0;i<200;i++){ len = UART_RECV(fd, rcv_buf,MAX_REC_BUFF); if(len > 0) write(accept_fd,rcv_buf,len); else printf("(Time limit)No data now!\n"); } close(accept_fd); exit(0); } else if(n<0) printf("Fork Error:%s\n\a",strerror(errno)); } } else { fprintf(stderr,"Unsupported char:'s' or 'r'\n"); return (FALSE); } UART_CLOSE(fd); close(listen_fd); return 0; }