init

Committer:
gert_lauritsen
Date:
Mon Feb 29 16:15:15 2016 +0000
Revision:
3:9fda5c68d0fd
Parent:
2:c1599cad82c3
Child:
4:1679a9439241
?ndret i consol type

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 0:c9f3777fe0b4 1 //------------------------------------------------------------------------------------
gert_lauritsen 2:c1599cad82c3 2 // :
gert_lauritsen 2:c1599cad82c3 3 // Title : Comlink
gert_lauritsen 0:c9f3777fe0b4 4 // Filename : Queue.h
gert_lauritsen 0:c9f3777fe0b4 5 // Author : Gert Lauritsen
gert_lauritsen 0:c9f3777fe0b4 6 // Origin Date : 26/05/2015
gert_lauritsen 0:c9f3777fe0b4 7 // Version : 1.000
gert_lauritsen 0:c9f3777fe0b4 8 // Compiler : Keil
gert_lauritsen 2:c1599cad82c3 9 // Target :
gert_lauritsen 0:c9f3777fe0b4 10 // Notes : None
gert_lauritsen 0:c9f3777fe0b4 11 //
gert_lauritsen 0:c9f3777fe0b4 12 //------------------------------------------------------------------------------------
gert_lauritsen 0:c9f3777fe0b4 13
gert_lauritsen 0:c9f3777fe0b4 14 #include "ComLink.h"
gert_lauritsen 0:c9f3777fe0b4 15 #define Frameheader 3
gert_lauritsen 2:c1599cad82c3 16 //#define TxIrq
gert_lauritsen 0:c9f3777fe0b4 17
gert_lauritsen 2:c1599cad82c3 18 ComLink::ComLink(PinName tx, PinName rx) : _com(tx,rx)
gert_lauritsen 0:c9f3777fe0b4 19 {
gert_lauritsen 0:c9f3777fe0b4 20 //_com=new Serial(tx,rx);
gert_lauritsen 0:c9f3777fe0b4 21 init_q();
gert_lauritsen 0:c9f3777fe0b4 22 _com.baud(19200);
gert_lauritsen 0:c9f3777fe0b4 23 TxAktiv=0; //Signal om at der ikke sendes data for øjeblikket
gert_lauritsen 2:c1599cad82c3 24 _Dtype=0;
gert_lauritsen 2:c1599cad82c3 25 #ifndef TxIrq
gert_lauritsen 0:c9f3777fe0b4 26 _com.attach(this,&ComLink::txCallback, Serial::TxIrq); //Interrupt rutiner
gert_lauritsen 2:c1599cad82c3 27 #endif
gert_lauritsen 0:c9f3777fe0b4 28 _com.attach(this,&ComLink::rxCallback, Serial::RxIrq);
gert_lauritsen 0:c9f3777fe0b4 29 }
gert_lauritsen 0:c9f3777fe0b4 30
gert_lauritsen 2:c1599cad82c3 31 bool ComLink::CRC()
gert_lauritsen 2:c1599cad82c3 32 {
gert_lauritsen 2:c1599cad82c3 33 unsigned char CRCin=0;
gert_lauritsen 2:c1599cad82c3 34 int framesize=inbuff[1];
gert_lauritsen 2:c1599cad82c3 35 for (int i=0; i<(framesize+Frameheader); i++) CRCin^=inbuff[i];
gert_lauritsen 2:c1599cad82c3 36 //return CRCin==inbuff[framesize+Frameheader];
gert_lauritsen 2:c1599cad82c3 37 return 1;
gert_lauritsen 2:c1599cad82c3 38 }
gert_lauritsen 0:c9f3777fe0b4 39
gert_lauritsen 0:c9f3777fe0b4 40 //------------------------------------------------------------------------------------
gert_lauritsen 0:c9f3777fe0b4 41 // STX Size type Data CRC
gert_lauritsen 0:c9f3777fe0b4 42 // 0 1 2 3..Dsize Dsize+3
gert_lauritsen 0:c9f3777fe0b4 43 //------------------------------------------------------------------------------------
gert_lauritsen 0:c9f3777fe0b4 44
gert_lauritsen 0:c9f3777fe0b4 45 void ComLink::rxCallback() //modtager data, når frame er modtaget sende besked
gert_lauritsen 0:c9f3777fe0b4 46 {
gert_lauritsen 0:c9f3777fe0b4 47 static unsigned char InPointer;
gert_lauritsen 0:c9f3777fe0b4 48 static bool Incoming;
gert_lauritsen 0:c9f3777fe0b4 49 unsigned char framesize;
gert_lauritsen 2:c1599cad82c3 50
gert_lauritsen 0:c9f3777fe0b4 51
gert_lauritsen 0:c9f3777fe0b4 52 inbuff[InPointer]=_com.getc();
gert_lauritsen 0:c9f3777fe0b4 53 if ((!Incoming) && (inbuff[InPointer]==stx)) {
gert_lauritsen 0:c9f3777fe0b4 54 Incoming=1;
gert_lauritsen 0:c9f3777fe0b4 55 }
gert_lauritsen 0:c9f3777fe0b4 56 if (Incoming) {
gert_lauritsen 0:c9f3777fe0b4 57 if (InPointer>Frameheader) {
gert_lauritsen 3:9fda5c68d0fd 58 framesize=inbuff[1]+Frameheader;
gert_lauritsen 3:9fda5c68d0fd 59 if (framesize<=InPointer) { //Vi har modtaget det hele
gert_lauritsen 0:c9f3777fe0b4 60 if (CRC()) {
gert_lauritsen 3:9fda5c68d0fd 61 strncpy(&Data[0],&inbuff[3],framesize-Frameheader);
gert_lauritsen 0:c9f3777fe0b4 62 InPointer=0;
gert_lauritsen 0:c9f3777fe0b4 63 Incoming=0;
gert_lauritsen 2:c1599cad82c3 64 _Dtype=inbuff[2];
gert_lauritsen 3:9fda5c68d0fd 65 DSize=framesize-Frameheader;
gert_lauritsen 0:c9f3777fe0b4 66 } //if crc
gert_lauritsen 2:c1599cad82c3 67 else {
gert_lauritsen 0:c9f3777fe0b4 68 InPointer=0;
gert_lauritsen 0:c9f3777fe0b4 69 Incoming=0;
gert_lauritsen 2:c1599cad82c3 70 }
gert_lauritsen 2:c1599cad82c3 71
gert_lauritsen 0:c9f3777fe0b4 72 } //if framesize..
gert_lauritsen 0:c9f3777fe0b4 73 } //if (Inpo..
gert_lauritsen 0:c9f3777fe0b4 74 if (Incoming) InPointer++; //
gert_lauritsen 0:c9f3777fe0b4 75 } //if incoming
gert_lauritsen 0:c9f3777fe0b4 76 }
gert_lauritsen 0:c9f3777fe0b4 77
gert_lauritsen 2:c1599cad82c3 78 int ComLink::Work()
gert_lauritsen 2:c1599cad82c3 79 {
gert_lauritsen 2:c1599cad82c3 80 int tmp;
gert_lauritsen 2:c1599cad82c3 81 tmp=_Dtype;
gert_lauritsen 2:c1599cad82c3 82 _Dtype=0;
gert_lauritsen 2:c1599cad82c3 83 return tmp;
gert_lauritsen 2:c1599cad82c3 84 }
gert_lauritsen 0:c9f3777fe0b4 85
gert_lauritsen 0:c9f3777fe0b4 86 void ComLink::txCallback() //Skal tage den næste byt i rækken og sende den
gert_lauritsen 0:c9f3777fe0b4 87 {
gert_lauritsen 2:c1599cad82c3 88 unsigned char NextCh=0;
gert_lauritsen 2:c1599cad82c3 89 if (Get(&NextCh)==0) TxAktiv=0;
gert_lauritsen 2:c1599cad82c3 90 else
gert_lauritsen 2:c1599cad82c3 91 _com.putc((int) NextCh); //sender næste char fra q
gert_lauritsen 0:c9f3777fe0b4 92 }
gert_lauritsen 2:c1599cad82c3 93 #ifndef TxIrq
gert_lauritsen 2:c1599cad82c3 94 void ComLink::Senddata(unsigned char Dtype,unsigned char Dsize, char *str)
gert_lauritsen 2:c1599cad82c3 95 {
gert_lauritsen 2:c1599cad82c3 96 unsigned char outbuf[50];
gert_lauritsen 2:c1599cad82c3 97 int i;
gert_lauritsen 2:c1599cad82c3 98 if (Dsize<(50-4)) {
gert_lauritsen 2:c1599cad82c3 99 outbuf[0]=stx;
gert_lauritsen 2:c1599cad82c3 100 outbuf[1]=Dsize;
gert_lauritsen 2:c1599cad82c3 101 outbuf[2]=Dtype;
gert_lauritsen 2:c1599cad82c3 102 for (i=0; i<Dsize; i++) { //Datasektion
gert_lauritsen 2:c1599cad82c3 103 outbuf[3+i]=*str;
gert_lauritsen 2:c1599cad82c3 104 str++;
gert_lauritsen 2:c1599cad82c3 105 }
gert_lauritsen 2:c1599cad82c3 106 Q_crc=0;
gert_lauritsen 2:c1599cad82c3 107 for (i=0; i<Dsize+3; i++) Q_crc^=outbuf[i]; //CRC
gert_lauritsen 2:c1599cad82c3 108 outbuf[Dsize+3]=Q_crc;
gert_lauritsen 2:c1599cad82c3 109 for (i=0; i<Dsize+4; i++) {
gert_lauritsen 2:c1599cad82c3 110 _com.putc((int) outbuf[i]);
gert_lauritsen 2:c1599cad82c3 111 while (!_com.writeable()) {} //wait
gert_lauritsen 2:c1599cad82c3 112 }
gert_lauritsen 2:c1599cad82c3 113 }
gert_lauritsen 2:c1599cad82c3 114 }
gert_lauritsen 2:c1599cad82c3 115 #endif
gert_lauritsen 2:c1599cad82c3 116 #ifdef TxIrq
gert_lauritsen 0:c9f3777fe0b4 117
gert_lauritsen 0:c9f3777fe0b4 118 void ComLink::Senddata(unsigned char Dtype,unsigned char Dsize, char *str)
gert_lauritsen 0:c9f3777fe0b4 119 {
gert_lauritsen 2:c1599cad82c3 120 unsigned char NextCh=0;
gert_lauritsen 2:c1599cad82c3 121 Q_crc=0;
gert_lauritsen 2:c1599cad82c3 122 //printf("N=%d %s\r\n",Dsize, &str[2]);
gert_lauritsen 2:c1599cad82c3 123 Put(stx); //Start Frame
gert_lauritsen 2:c1599cad82c3 124 Put(Dsize); //FrameSize
gert_lauritsen 2:c1599cad82c3 125 Put(Dtype); //Frame type
gert_lauritsen 2:c1599cad82c3 126 for (int i=0; i<Dsize; i++) { //Datasektion
gert_lauritsen 2:c1599cad82c3 127 Put(*str);
gert_lauritsen 2:c1599cad82c3 128 str++;
gert_lauritsen 2:c1599cad82c3 129 }
gert_lauritsen 2:c1599cad82c3 130 Put(Q_crc); //CRC
gert_lauritsen 2:c1599cad82c3 131 if (!TxAktiv) { //Hvis vi ikke sender noget for tiden startes tømningen fra Q
gert_lauritsen 2:c1599cad82c3 132 Get(&NextCh);
gert_lauritsen 2:c1599cad82c3 133 _com.putc((int) NextCh); //sender næste char fra q
gert_lauritsen 2:c1599cad82c3 134 TxAktiv=1;
gert_lauritsen 2:c1599cad82c3 135 }
gert_lauritsen 0:c9f3777fe0b4 136 }
gert_lauritsen 2:c1599cad82c3 137 #endif
gert_lauritsen 0:c9f3777fe0b4 138 //---------------------------------------------------------------------------------------------------
gert_lauritsen 0:c9f3777fe0b4 139 //Tx Frames
gert_lauritsen 0:c9f3777fe0b4 140 //Public functions:
gert_lauritsen 2:c1599cad82c3 141 void ComLink::TxFloats(unsigned char Ftype, float *Fvalue, int Size)
gert_lauritsen 2:c1599cad82c3 142 {
gert_lauritsen 2:c1599cad82c3 143 char OutStr[40];
gert_lauritsen 2:c1599cad82c3 144 union {
gert_lauritsen 0:c9f3777fe0b4 145 char bytes[4];
gert_lauritsen 0:c9f3777fe0b4 146 float val;
gert_lauritsen 2:c1599cad82c3 147 } Float2Byte;
gert_lauritsen 2:c1599cad82c3 148 for (int i=0; i<Size; i++) {
gert_lauritsen 2:c1599cad82c3 149 Float2Byte.val=*Fvalue;
gert_lauritsen 2:c1599cad82c3 150 memcpy(&OutStr[i*4],&Float2Byte.bytes[0],4);
gert_lauritsen 2:c1599cad82c3 151 }
gert_lauritsen 2:c1599cad82c3 152 Senddata(Ftype,Size*4,OutStr);
gert_lauritsen 2:c1599cad82c3 153 }
gert_lauritsen 0:c9f3777fe0b4 154
gert_lauritsen 2:c1599cad82c3 155 void ComLink::TxCalRecord(float *value, int Size)
gert_lauritsen 2:c1599cad82c3 156 {
gert_lauritsen 2:c1599cad82c3 157 TxFloats(2,value,Size);
gert_lauritsen 2:c1599cad82c3 158 }
gert_lauritsen 0:c9f3777fe0b4 159
gert_lauritsen 0:c9f3777fe0b4 160
gert_lauritsen 2:c1599cad82c3 161 void ComLink::TxStatus(float *value, int Size)
gert_lauritsen 2:c1599cad82c3 162 {
gert_lauritsen 2:c1599cad82c3 163 TxFloats(3,value,Size);
gert_lauritsen 2:c1599cad82c3 164 }
gert_lauritsen 2:c1599cad82c3 165
gert_lauritsen 2:c1599cad82c3 166 void ComLink::TxErrStateArr(float *value, int Size)
gert_lauritsen 2:c1599cad82c3 167 {
gert_lauritsen 2:c1599cad82c3 168 TxFloats(4,value,Size);
gert_lauritsen 2:c1599cad82c3 169 }
gert_lauritsen 2:c1599cad82c3 170
gert_lauritsen 2:c1599cad82c3 171 void ComLink::TxSetStateArr(float *value, int Size)
gert_lauritsen 2:c1599cad82c3 172 {
gert_lauritsen 2:c1599cad82c3 173 TxFloats(5,value,Size);
gert_lauritsen 2:c1599cad82c3 174 }
gert_lauritsen 0:c9f3777fe0b4 175
gert_lauritsen 2:c1599cad82c3 176 void ComLink::TxMessData(unsigned char Ftype, void *SValue, int Size)
gert_lauritsen 2:c1599cad82c3 177 {
gert_lauritsen 2:c1599cad82c3 178 char OutStr[40];
gert_lauritsen 2:c1599cad82c3 179 memcpy(&OutStr[0],SValue,Size);
gert_lauritsen 2:c1599cad82c3 180 Senddata(Ftype,Size,OutStr);
gert_lauritsen 2:c1599cad82c3 181 }
gert_lauritsen 0:c9f3777fe0b4 182
gert_lauritsen 2:c1599cad82c3 183 void ComLink::WriteConsol(char *str)
gert_lauritsen 2:c1599cad82c3 184 {
gert_lauritsen 2:c1599cad82c3 185 //Bare til at blive skrevet på consol
gert_lauritsen 2:c1599cad82c3 186 char outstr[50];
gert_lauritsen 2:c1599cad82c3 187 if (strlen(str)<48) {
gert_lauritsen 2:c1599cad82c3 188 strcpy(&outstr[0],str);
gert_lauritsen 3:9fda5c68d0fd 189 Senddata(8,strlen(str),outstr);
gert_lauritsen 2:c1599cad82c3 190 }
gert_lauritsen 2:c1599cad82c3 191 }
gert_lauritsen 0:c9f3777fe0b4 192
gert_lauritsen 0:c9f3777fe0b4 193 void ComLink::WriteLCD(unsigned char x,unsigned char y, char *str)
gert_lauritsen 0:c9f3777fe0b4 194 {
gert_lauritsen 2:c1599cad82c3 195 char outstr[50];
gert_lauritsen 2:c1599cad82c3 196 if (strlen(str)<48) {
gert_lauritsen 2:c1599cad82c3 197 outstr[0]=x;
gert_lauritsen 2:c1599cad82c3 198 outstr[1]=y;
gert_lauritsen 2:c1599cad82c3 199 memcpy(&outstr[2],str,strlen(str));
gert_lauritsen 2:c1599cad82c3 200 Senddata(6,2+strlen(str),outstr);
gert_lauritsen 2:c1599cad82c3 201 }
gert_lauritsen 0:c9f3777fe0b4 202 }
gert_lauritsen 0:c9f3777fe0b4 203
gert_lauritsen 0:c9f3777fe0b4 204 void ComLink::cls()
gert_lauritsen 0:c9f3777fe0b4 205 {
gert_lauritsen 0:c9f3777fe0b4 206 Senddata(7,0,"");
gert_lauritsen 0:c9f3777fe0b4 207 }