A class for using MU2.

Dependents:   EM_Mission FM_integration_copy FM_integration_off FM_integration

Committer:
ATKINZ117
Date:
Tue Jul 02 13:13:48 2013 +0000
Revision:
0:6c7ba361f738
Child:
1:c60752b4acb9
This is the first ver. of MU2Class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ATKINZ117 0:6c7ba361f738 1 #include "MU2Class.h"
ATKINZ117 0:6c7ba361f738 2 #include "mbed.h"
ATKINZ117 0:6c7ba361f738 3
ATKINZ117 0:6c7ba361f738 4 MU2Class::MU2Class(PinName tx,PinName rx){
ATKINZ117 0:6c7ba361f738 5 _tx=tx;
ATKINZ117 0:6c7ba361f738 6 _rx=rx;
ATKINZ117 0:6c7ba361f738 7 }
ATKINZ117 0:6c7ba361f738 8
ATKINZ117 0:6c7ba361f738 9 uint8_t MU2Class::Store_Str(char data_str[], int index, char Message[]){
ATKINZ117 0:6c7ba361f738 10 uint8_t j=0;
ATKINZ117 0:6c7ba361f738 11 while(Message[j]!='\0'&&Message[j]!='^') data_str[index++]=Message[j++];//store char in message into data_str and returns the last index.
ATKINZ117 0:6c7ba361f738 12 return index;
ATKINZ117 0:6c7ba361f738 13 }
ATKINZ117 0:6c7ba361f738 14
ATKINZ117 0:6c7ba361f738 15 void MU2Class::send(const char Send_Message[]){
ATKINZ117 0:6c7ba361f738 16 Serial MyMu2(_tx,_rx);
ATKINZ117 0:6c7ba361f738 17 Serial pc(USBTX,USBRX);
ATKINZ117 0:6c7ba361f738 18
ATKINZ117 0:6c7ba361f738 19 int i=4, str_length=0,send_index=0;
ATKINZ117 0:6c7ba361f738 20 char send_data[255];
ATKINZ117 0:6c7ba361f738 21
ATKINZ117 0:6c7ba361f738 22 MyMu2.baud(19200);
ATKINZ117 0:6c7ba361f738 23 pc.baud(19200);
ATKINZ117 0:6c7ba361f738 24
ATKINZ117 0:6c7ba361f738 25 send_index=0;//initializing the variant.
ATKINZ117 0:6c7ba361f738 26 send_index=Store_Str(send_data, send_index, "@DT00^");//this variant is the last index of the string.The reason why char "^" here is that for the case of the data size 10,20,A0 or something . Store_Str copies the string of its last variant into its first variant .
ATKINZ117 0:6c7ba361f738 27
ATKINZ117 0:6c7ba361f738 28 while(Send_Message[str_length]!='\0'){
ATKINZ117 0:6c7ba361f738 29 send_data[send_index++]=Send_Message[str_length++];//storing the string we wanna send into the send_data until null terminater appers.
ATKINZ117 0:6c7ba361f738 30 }
ATKINZ117 0:6c7ba361f738 31 send_data[send_index++]='\r';//insert carrige return at the last of send_data.
ATKINZ117 0:6c7ba361f738 32 send_data[send_index++]='\n';
ATKINZ117 0:6c7ba361f738 33
ATKINZ117 0:6c7ba361f738 34 //str_length-=2; //the code block below is for making data size proper.
ATKINZ117 0:6c7ba361f738 35
ATKINZ117 0:6c7ba361f738 36 for(i=4; i>2; i--){
ATKINZ117 0:6c7ba361f738 37 if(str_length%16<10){
ATKINZ117 0:6c7ba361f738 38 send_data[i]='0'+str_length%16;
ATKINZ117 0:6c7ba361f738 39 }else{
ATKINZ117 0:6c7ba361f738 40 switch(str_length%16){
ATKINZ117 0:6c7ba361f738 41 case 10:send_data[i]='A'; break;
ATKINZ117 0:6c7ba361f738 42 case 11:send_data[i]='B'; break;
ATKINZ117 0:6c7ba361f738 43 case 12:send_data[i]='C'; break;
ATKINZ117 0:6c7ba361f738 44 case 13:send_data[i]='D'; break;
ATKINZ117 0:6c7ba361f738 45 case 14:send_data[i]='E'; break;
ATKINZ117 0:6c7ba361f738 46 case 15:send_data[i]='F'; break;
ATKINZ117 0:6c7ba361f738 47 }
ATKINZ117 0:6c7ba361f738 48 }
ATKINZ117 0:6c7ba361f738 49 str_length/=16;
ATKINZ117 0:6c7ba361f738 50 }
ATKINZ117 0:6c7ba361f738 51 for(int j=0;j<=send_index;j++){
ATKINZ117 0:6c7ba361f738 52 MyMu2.putc(send_data[j]);
ATKINZ117 0:6c7ba361f738 53 pc.putc(send_data[j]); //for debugging.Comment out when U use this in practice.
ATKINZ117 0:6c7ba361f738 54 }
ATKINZ117 0:6c7ba361f738 55 }