Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: 0-sim5320e-L432KC-relais-0 0-DISCO-F746NG-Sim-soufflerie-PID
Revision 0:88efe939415b, committed 2022-05-12
- Comitter:
- schnf30
- Date:
- Thu May 12 07:26:24 2022 +0000
- Commit message:
- Reception serie evenement suoivant protocole $x,.,.,.terminaison terminaison est \r, \n ou \r\n
Changed in this revision
| MySerial.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MySerial.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MySerial.cpp Thu May 12 07:26:24 2022 +0000
@@ -0,0 +1,75 @@
+#include "mbed.h"
+#include "MySerial.h"
+//
+MySerial::MySerial(PinName Txd, PinName Rxd, float TimeOut) : RawSerial(Txd,Rxd)
+{
+ baud(Baud);
+ _TimeOut = TimeOut;
+// _DataTmp = new char[maxdata + 1];
+// _Data = new char[maxdata + 1];
+ _DataPtr = 0;
+ _DataReady = false;
+ _Canal1Ok = false;
+ _Canal2Ok = false;
+ attach(callback(this,&MySerial::receive), RawSerial::RxIrq);
+ if (_TimeOut != NULL) TGarde.attach(callback(this,&MySerial::Tevent),_TimeOut); // pour chien de garde
+}
+void MySerial::Tevent(void) // chien de garde
+{
+ if (!_Canal1Ok) printf("$Y\r\n");
+ if (!_Canal2Ok) printf("$y\r\n");
+ _Canal1Ok = false;
+ _Canal2Ok = false;
+}
+char * MySerial::read(void)
+{
+ _DataReady = false;
+ return _Data;
+}
+
+bool MySerial::dataready(void)
+{
+ return _DataReady;
+}
+
+// reception phrase de type '$' "....' "\r\n"
+void MySerial::receive(void)
+{
+ bool _PhraseComplete = false;
+ char inChar;
+ while (readable() && (_PhraseComplete == false)) {
+ inChar = getc();
+ switch (inChar) {
+ case '$' :
+ _DataPtr = 0;
+ _DataTmp[_DataPtr++] = inChar;
+ _DataTmp[_DataPtr] = 0;
+ break;
+ case '\r' :
+ ;
+ case '\n' :
+ if (_DataPtr > 1) {
+ strcpy(_Data,_DataTmp);
+ _PhraseComplete = true;
+ if (_TimeOut!=NULL) {
+ if ((_Data[1] >> 5)==2) _Canal1Ok = true;
+ if ((_Data[1] >> 5)==3) _Canal2Ok = true;
+ }
+ }
+ _DataTmp[0] = 0;
+ _DataPtr = 0;
+ break;
+ default :
+ if (_DataPtr > 0) {
+ _DataTmp[_DataPtr++] = inChar;
+ _DataTmp[_DataPtr] = 0;
+ }
+ }
+ if (_DataPtr >= maxdata) { // si phrase trop longue vide phrase
+ _DataTmp[0] = 0;
+ _DataPtr = 0;
+ _PhraseComplete = false;
+ }
+ }
+ _DataReady = _PhraseComplete;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MySerial.h Thu May 12 07:26:24 2022 +0000
@@ -0,0 +1,74 @@
+#ifndef _MySerial_
+#define _MySerial_
+// driver interface serie
+// cette class permet de recevoir et transmettre des donnees serie suivant un protocole
+// $x,.,.,.,.terminaison // terminaison peut etre "\r","\n" ou "\r\n"
+// les donnees sont acquises en tache de fond.
+//
+// Les méthodes :
+// - MySerial(PinName Txd, PinName Rxd,float _TimeOut=NULL); // param txd et rxd. Timeout pas obligatoire
+// - bool dataready(void); : retourne true lorsquune phrase est acquise
+// - char * read(void); : retourne phrase lue sans la terminaison
+//
+// exemple
+// dans main.cpp
+/*
+#include "mbed.h"
+#include "MySerial.h"
+//
+//
+
+MySerial PC(USBTX,USBRX); // liaison PC
+char PCPhrase[maxdata]; // pour stocker phrase PC
+
+void main (void)
+{
+ // recevoir des donnees
+ if (PC.dataready()) {
+ strcpy(PCPhrase,PC.read()); // PCPhrase contient la phrase recu
+ switch (PCPhrase[1]) {
+ case 'B': // consigne de flux air
+ ConsVitFluxAir = atof(PCPhrase+2); //
+ datachange=true; // on indique qune donne a change
+ break;
+ case 'L' : // param pid
+ if (sscanf(PCPhrase,"$L,%f,%f,%f",&coefp,&coefi,&coefd)>=1) datachange=true;
+ break;
+ default :
+ ;
+ }
+ }
+
+ if ((EnvoiePC!=0)&&(PCOk==true)) {
+ while (!PC.writeable());
+ PC.printf("$V,%0.1f,%0.1f\r\n",VitFluxAir,Frequencehelice);
+ }
+}
+*/
+// taille maximum d'une phrase recue
+#ifndef maxdata
+#define maxdata 99
+#endif
+#define Baud 921600
+class MySerial : public RawSerial
+{
+public:
+ MySerial(PinName Txd, PinName Rxd,float _TimeOut=NULL); // _TimeOute pour chien de garde en s
+ bool dataready(void);
+ char * read(void);
+private:
+// reception phrase Serial
+ char _DataTmp[maxdata + 1]; // tableau de donnees pour stocker pendant reception
+ char _Data[maxdata + 1]; // tableau de donnees lorsque toutes les donnees sont recues
+// char *_DataTmp; // tableau de donnees pour stocker pendant reception
+// char *_Data; // tableau de donnees lorsque toutes les donnees sont recues
+ int _DataPtr; // position de stockage de la prochaine donnee a recevoir
+ bool _Canal1Ok;
+ bool _Canal2Ok;
+ Ticker TGarde;
+ bool _DataReady;
+ float _TimeOut;
+ void receive(); //recoit les data gps et les stocks puis produit les donnees en cas de reception
+ void Tevent(void);
+};
+#endif