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.
SerialCom.cpp
00001 /* 00002 * SerialServer.cpp 00003 * Copyright (c) 2017, ZHAW 00004 * All rights reserved. 00005 * 00006 * Created on: 05.06.2017 00007 * Author: Marcel Honegger 00008 */ 00009 00010 00011 #include <mbed.h> 00012 #include <iostream> 00013 #include <string> 00014 #include <sstream> 00015 #include <vector> 00016 #include "SerialCom.h" 00017 00018 using namespace std; 00019 00020 const float SerialCom::PERIOD = 0.0001f; // the period of the timer interrupt, given in [s] 00021 const char SerialCom::DELIM = ','; 00022 00023 /** 00024 * Create a serial server object. 00025 00026 00027 00028 SerialServer::SerialServer(RawSerial& serial, StateMachine& stateMachine) : serial(serial), stateMachine(stateMachine), thread(osPriorityRealtime, STACK_SIZE) { 00029 */ 00030 SerialCom::SerialCom(RawSerial& serial) : serial(serial), thread(osPriorityRealtime, STACK_SIZE) { 00031 // initialize local values 00032 00033 input.clear(); 00034 00035 // start thread and ticker timer interrupt service routine 00036 dataRecived = false; 00037 00038 gainG = 1.1f; 00039 gain_dG = 1.1f; 00040 00041 thread.start(callback(this, &SerialCom::run)); 00042 ticker.attach(callback(this, &SerialCom::sendSignal), PERIOD); 00043 } 00044 00045 /** 00046 * Delete the serial server object and release all allocated resources. 00047 */ 00048 SerialCom::~SerialCom() { 00049 00050 ticker.detach(); 00051 } 00052 00053 /** 00054 * This method is called by the ticker timer interrupt service routine. 00055 * It sends a signal to the thread to make it run again. 00056 */ 00057 void SerialCom::sendSignal() { 00058 00059 thread.signal_set(signal); 00060 } 00061 00062 /** 00063 * This <code>run()</code> method contains an infinite loop with the run logic. 00064 */ 00065 void SerialCom::run() { 00066 00067 //Serial pc(PA_2, PA_3); // tx, rx 00068 //pc.baud(9600); 00069 00070 Serial pc1(USBTX, USBRX); // tx, rx 00071 pc1.baud(100000); 00072 00073 while (true) { 00074 00075 // wait for the periodic signal 00076 00077 thread.signal_wait(signal); 00078 00079 00080 00081 // read received characters while buffer is full 00082 00083 while (serial.readable()) { 00084 int32_t c = serial.getc(); 00085 //if (input.size() >= MESSAGE_SIZE) input.erase(0, 1); 00086 input += static_cast<uint8_t>(c); 00087 } 00088 00089 // try to decode a message 00090 00091 if (input[input.size()-1] != '&') { 00092 00093 // message is too short, keep reading... 00094 00095 } else { 00096 //pc.printf("command: %s\r\n", input); 00097 splitString(); 00098 if (tokens[0] == "M") { 00099 if (tokens.size() == 45+2) { 00100 float newK[3][15]; 00101 int y = 0; 00102 int x = 0; 00103 float checksum = 0; 00104 for (int i=1; i < tokens.size()-1; i++) { 00105 newK[y][x] = strtof((tokens[i]).c_str(), 0); 00106 //pc.printf("Element %d , %d: %.10f\r\n", y, x, newK[y][x]); 00107 x++; 00108 checksum += newK[y][x]; 00109 if (x == 15){ 00110 x = 0; 00111 y++; 00112 } 00113 } 00114 } 00115 else{ 00116 //pc.printf("Data to short od long\r\n"); 00117 } 00118 00119 } 00120 else if (tokens[0] == "G") { 00121 //pc.printf("Send data to pc\r\n"); 00122 if(sendData == false) { 00123 sendData = true; 00124 runCount = 0; 00125 } 00126 else{ 00127 sendData = false; 00128 } 00129 } 00130 00131 else if (tokens[0] == "N") { 00132 //pc.printf("\r\nN\r\n"); 00133 float data[4] = {}; 00134 for (int i=1; i < tokens.size(); i++) { 00135 data[i-1] = strtof((tokens[i]).c_str(), 0); 00136 //pc.printf("Element %d %.2f\r\n", i,data[i-1]); 00137 } 00138 this->gainG = data[0]; 00139 this->gain_dG = data[1]; 00140 this->offsetX = data[2]; 00141 this->offsetY = data[3]; 00142 this->dataRecived = true; 00143 //pc1.printf("\r\ %d %.2f %.2f\r\n",dataRecived,gainG,gain_dG); 00144 00145 } 00146 else{ 00147 //pc.printf("Command not understood\r\n"); 00148 } 00149 00150 00151 tokens.clear(); 00152 input.clear(); 00153 00154 00155 } 00156 if(sendData == true) { 00157 if (output.size() == 0 && runCount >= 1000){ 00158 output += "Hello nicolas\n"; 00159 runCount = 0; 00160 } 00161 } 00162 runCount++; 00163 while (serial.writeable() && (output.size() > 0)) { 00164 serial.putc(output[0]); 00165 output.erase(0, 1); 00166 } 00167 00168 } 00169 00170 } 00171 00172 00173 void SerialCom::splitString() { 00174 stringstream ss(input); 00175 string item; 00176 while (getline(ss, item, DELIM)) { 00177 tokens.push_back(item); 00178 } 00179 return; 00180 } 00181 00182
Generated on Tue Jul 12 2022 18:18:48 by
1.7.2