init
This commit is contained in:
commit
a1d7369096
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
46
.idea/inspectionProfiles/Project_Default.xml
Normal file
46
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="ignoredPackages">
|
||||||
|
<value>
|
||||||
|
<list size="24">
|
||||||
|
<item index="0" class="java.lang.String" itemvalue="blinker" />
|
||||||
|
<item index="1" class="java.lang.String" itemvalue="autopep8" />
|
||||||
|
<item index="2" class="java.lang.String" itemvalue="Flask-Cors" />
|
||||||
|
<item index="3" class="java.lang.String" itemvalue="Werkzeug" />
|
||||||
|
<item index="4" class="java.lang.String" itemvalue="Flask-HTTPAuth" />
|
||||||
|
<item index="5" class="java.lang.String" itemvalue="SQLAlchemy" />
|
||||||
|
<item index="6" class="java.lang.String" itemvalue="cryptography" />
|
||||||
|
<item index="7" class="java.lang.String" itemvalue="MarkupSafe" />
|
||||||
|
<item index="8" class="java.lang.String" itemvalue="click" />
|
||||||
|
<item index="9" class="java.lang.String" itemvalue="Flask-SQLAlchemy" />
|
||||||
|
<item index="10" class="java.lang.String" itemvalue="casbin" />
|
||||||
|
<item index="11" class="java.lang.String" itemvalue="pip-review" />
|
||||||
|
<item index="12" class="java.lang.String" itemvalue="Authlib" />
|
||||||
|
<item index="13" class="java.lang.String" itemvalue="filelock" />
|
||||||
|
<item index="14" class="java.lang.String" itemvalue="platformdirs" />
|
||||||
|
<item index="15" class="java.lang.String" itemvalue="certifi" />
|
||||||
|
<item index="16" class="java.lang.String" itemvalue="virtualenv" />
|
||||||
|
<item index="17" class="java.lang.String" itemvalue="charset-normalizer" />
|
||||||
|
<item index="18" class="java.lang.String" itemvalue="casbin-sqlalchemy-adapter" />
|
||||||
|
<item index="19" class="java.lang.String" itemvalue="gevent" />
|
||||||
|
<item index="20" class="java.lang.String" itemvalue="Flask" />
|
||||||
|
<item index="21" class="java.lang.String" itemvalue="typing_extensions" />
|
||||||
|
<item index="22" class="java.lang.String" itemvalue="greenlet" />
|
||||||
|
<item index="23" class="java.lang.String" itemvalue="Flask-APScheduler" />
|
||||||
|
</list>
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<option name="ignoredErrors">
|
||||||
|
<list>
|
||||||
|
<option value="N803" />
|
||||||
|
<option value="N802" />
|
||||||
|
<option value="N806" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/serialt.iml" filepath="$PROJECT_DIR$/.idea/serialt.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
2
.idea/serialt.iml
Normal file
2
.idea/serialt.iml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module classpath="CIDR" type="CPP_MODULE" version="4" />
|
140
include/serial.h
Normal file
140
include/serial.h
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
#ifndef SERIAL_H
|
||||||
|
#define SERIAL_H
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <chrono>
|
||||||
|
#include <optional>
|
||||||
|
#include <thread>
|
||||||
|
#include <functional>
|
||||||
|
#include "../third/serialib.h"
|
||||||
|
|
||||||
|
using namespace std::literals::chrono_literals;
|
||||||
|
|
||||||
|
template<typename T >
|
||||||
|
concept SupportString = requires{
|
||||||
|
std::is_same_v<T, const char*>;
|
||||||
|
std::is_same_v<T, std::string>;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class
|
||||||
|
[[maybe_unused]]
|
||||||
|
ErrorCode{
|
||||||
|
SUCCESS,
|
||||||
|
TIMEOUT,
|
||||||
|
SETTIMEOUTERROR,
|
||||||
|
WRITEINGERROR,
|
||||||
|
READINGERROR,
|
||||||
|
};
|
||||||
|
|
||||||
|
class Serial
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
serialib ser;
|
||||||
|
const char* endChar = "\r\n";
|
||||||
|
std::function<void(const std::string&)> logCallBack;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Serial() = default;
|
||||||
|
|
||||||
|
std::string GetTimeNow(){
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
auto now_c = std::chrono::system_clock::to_time_t(now);
|
||||||
|
return std::ctime(&now_c);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<SupportString T>
|
||||||
|
bool OpenDevice(T PortName, unsigned int bauds)
|
||||||
|
{
|
||||||
|
int code;
|
||||||
|
if constexpr (std::is_same_v<T, std::string>){
|
||||||
|
code = ser.openDevice(PortName.c_str(), bauds);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
code = ser.openDevice(PortName, bauds);
|
||||||
|
}
|
||||||
|
if(code == 1){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log(const std::string& log){
|
||||||
|
if(logCallBack){
|
||||||
|
auto msg = GetTimeNow() + " "+ log + "\n";
|
||||||
|
logCallBack(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetLogCallBack(std::function<void(const std::string&)> callBack){
|
||||||
|
logCallBack = callBack;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CloseDevice(){
|
||||||
|
ser.closeDevice();
|
||||||
|
}
|
||||||
|
|
||||||
|
~Serial() = default;
|
||||||
|
|
||||||
|
|
||||||
|
template<SupportString T>
|
||||||
|
std::optional<std::string> GetAtResponse(T command, int timeout = 50){
|
||||||
|
ser.flushReceiver();
|
||||||
|
std::string reallyCommand;
|
||||||
|
std::string response;
|
||||||
|
if constexpr (std::is_same_v<T, std::string>){
|
||||||
|
reallyCommand = command + endChar;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
reallyCommand = std::string(command) + endChar;
|
||||||
|
}
|
||||||
|
ser.writeString(reallyCommand.c_str());
|
||||||
|
Log("Send: " + reallyCommand);
|
||||||
|
std::this_thread::sleep_for(10ms);
|
||||||
|
// char buffer[ser.available()] = {0};
|
||||||
|
char* buffer = (char*)malloc(sizeof(char)*ser.available());
|
||||||
|
std::cout<<sizeof(buffer)<<std::endl;
|
||||||
|
auto size = ser.readBytes(buffer, sizeof(buffer), timeout);
|
||||||
|
if(size > 0){
|
||||||
|
response = std::string(buffer);
|
||||||
|
Log("Receive: " + response);
|
||||||
|
delete[] buffer;
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
delete[] buffer;
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<SupportString T>
|
||||||
|
bool GetAtUntil(T command, T expect = "OK",int timeout = 50){
|
||||||
|
auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(timeout);
|
||||||
|
ser.flushReceiver();
|
||||||
|
std::string reallyCommand;
|
||||||
|
if constexpr (std::is_same_v<T, std::string>){
|
||||||
|
reallyCommand = command + endChar;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
reallyCommand = std::string(command) + endChar;
|
||||||
|
}
|
||||||
|
ser.writeString(reallyCommand.c_str());
|
||||||
|
Log("Send : " + reallyCommand);
|
||||||
|
while(std::chrono::system_clock::now() < endTime){
|
||||||
|
std::this_thread::sleep_for(10ms);
|
||||||
|
auto buffer = new char[ser.available()];
|
||||||
|
auto size = ser.readBytes(buffer, sizeof(buffer), timeout);
|
||||||
|
auto str = std::string(buffer);
|
||||||
|
delete[] buffer;
|
||||||
|
if(size > 0)
|
||||||
|
Log("Receive: "+str);
|
||||||
|
if(str.find(expect) != std::string::npos){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SERIAL_H
|
24
main.cc
Normal file
24
main.cc
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "include/serial.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
using namespace std::literals::chrono_literals;
|
||||||
|
|
||||||
|
void PrintLog(const std::string& msg){
|
||||||
|
std::cout<<msg<<std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** const argv){
|
||||||
|
Serial serial;
|
||||||
|
serial.SetLogCallBack(PrintLog);
|
||||||
|
if(!serial.OpenDevice(R"(\\.\COM11)", 115200)){
|
||||||
|
std::cout<<"Open device failed"<<std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
auto startTime = std::chrono::system_clock::now();
|
||||||
|
// auto res = serial.GetAtUntil("AT+CFUN=1","OK", 200);
|
||||||
|
// std::cout<<res.value_or("ERROR")<<std::endl;
|
||||||
|
auto res = serial.GetAtResponse("AT+ECICCID", 200);
|
||||||
|
std::cout<<res.value_or("ERROR")<<std::endl;
|
||||||
|
auto endTime = std::chrono::system_clock::now();
|
||||||
|
std::cout<<std::chrono::duration_cast<std::chrono::milliseconds>(endTime-startTime).count();
|
||||||
|
}
|
5983
third/ctre.hpp
Normal file
5983
third/ctre.hpp
Normal file
File diff suppressed because it is too large
Load Diff
1134
third/serialib.cpp
Normal file
1134
third/serialib.cpp
Normal file
File diff suppressed because it is too large
Load Diff
269
third/serialib.h
Normal file
269
third/serialib.h
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
/*!
|
||||||
|
\file serialib.h
|
||||||
|
\brief Header file of the class serialib. This class is used for communication over a serial device.
|
||||||
|
\author Philippe Lucidarme (University of Angers)
|
||||||
|
\version 2.0
|
||||||
|
\date december the 27th of 2019
|
||||||
|
This Serial library is used to communicate through serial port.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM,
|
||||||
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
This is a licence-free software, it can be used by anyone who try to build a better world.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef SERIALIB_H
|
||||||
|
#define SERIALIB_H
|
||||||
|
|
||||||
|
#if defined(__CYGWIN__)
|
||||||
|
// This is Cygwin special case
|
||||||
|
#include <sys/time.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Include for windows
|
||||||
|
#if defined (_WIN32) || defined (_WIN64)
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
// This is MinGW special case
|
||||||
|
#include <sys/time.h>
|
||||||
|
#else
|
||||||
|
// sys/time.h does not exist on "actual" Windows
|
||||||
|
#define NO_POSIX_TIME
|
||||||
|
#endif
|
||||||
|
// Accessing to the serial port under Windows
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Include for Linux
|
||||||
|
#if defined (__linux__) || defined(__APPLE__)
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sys/time.h>
|
||||||
|
// File control definitions
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*! To avoid unused parameters */
|
||||||
|
#define UNUSED(x) (void)(x)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of serial data bits
|
||||||
|
*/
|
||||||
|
enum SerialDataBits {
|
||||||
|
SERIAL_DATABITS_5, /**< 5 databits */
|
||||||
|
SERIAL_DATABITS_6, /**< 6 databits */
|
||||||
|
SERIAL_DATABITS_7, /**< 7 databits */
|
||||||
|
SERIAL_DATABITS_8, /**< 8 databits */
|
||||||
|
SERIAL_DATABITS_16, /**< 16 databits */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of serial stop bits
|
||||||
|
*/
|
||||||
|
enum SerialStopBits {
|
||||||
|
SERIAL_STOPBITS_1, /**< 1 stop bit */
|
||||||
|
SERIAL_STOPBITS_1_5, /**< 1.5 stop bits */
|
||||||
|
SERIAL_STOPBITS_2, /**< 2 stop bits */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* type of serial parity bits
|
||||||
|
*/
|
||||||
|
enum SerialParity {
|
||||||
|
SERIAL_PARITY_NONE, /**< no parity bit */
|
||||||
|
SERIAL_PARITY_EVEN, /**< even parity bit */
|
||||||
|
SERIAL_PARITY_ODD, /**< odd parity bit */
|
||||||
|
SERIAL_PARITY_MARK, /**< mark parity */
|
||||||
|
SERIAL_PARITY_SPACE /**< space bit */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*! \class serialib
|
||||||
|
\brief This class is used for communication over a serial device.
|
||||||
|
*/
|
||||||
|
class serialib
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
//_____________________________________
|
||||||
|
// ::: Constructors and destructors :::
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Constructor of the class
|
||||||
|
serialib ();
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~serialib ();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//_________________________________________
|
||||||
|
// ::: Configuration and initialization :::
|
||||||
|
|
||||||
|
|
||||||
|
// Open a device
|
||||||
|
char openDevice(const char *Device, const unsigned int Bauds,
|
||||||
|
SerialDataBits Databits = SERIAL_DATABITS_8,
|
||||||
|
SerialParity Parity = SERIAL_PARITY_NONE,
|
||||||
|
SerialStopBits Stopbits = SERIAL_STOPBITS_1);
|
||||||
|
|
||||||
|
// Check device opening state
|
||||||
|
bool isDeviceOpen();
|
||||||
|
|
||||||
|
// Close the current device
|
||||||
|
void closeDevice();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//___________________________________________
|
||||||
|
// ::: Read/Write operation on characters :::
|
||||||
|
|
||||||
|
|
||||||
|
// Write a char
|
||||||
|
int writeChar (char);
|
||||||
|
|
||||||
|
// Read a char (with timeout)
|
||||||
|
int readChar (char *pByte,const unsigned int timeOut_ms=0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//________________________________________
|
||||||
|
// ::: Read/Write operation on strings :::
|
||||||
|
|
||||||
|
|
||||||
|
// Write a string
|
||||||
|
int writeString (const char *String);
|
||||||
|
|
||||||
|
// Read a string (with timeout)
|
||||||
|
int readString ( char *receivedString,
|
||||||
|
char finalChar,
|
||||||
|
unsigned int maxNbBytes,
|
||||||
|
const unsigned int timeOut_ms=0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// _____________________________________
|
||||||
|
// ::: Read/Write operation on bytes :::
|
||||||
|
|
||||||
|
|
||||||
|
// Write an array of bytes
|
||||||
|
int writeBytes (const void *Buffer, const unsigned int NbBytes);
|
||||||
|
|
||||||
|
// Read an array of byte (with timeout)
|
||||||
|
int readBytes (void *buffer,unsigned int maxNbBytes,const unsigned int timeOut_ms=0, unsigned int sleepDuration_us=100);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// _________________________
|
||||||
|
// ::: Special operation :::
|
||||||
|
|
||||||
|
|
||||||
|
// Empty the received buffer
|
||||||
|
char flushReceiver();
|
||||||
|
|
||||||
|
// Return the number of bytes in the received buffer
|
||||||
|
int available();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// _________________________
|
||||||
|
// ::: Access to IO bits :::
|
||||||
|
|
||||||
|
|
||||||
|
// Set CTR status (Data Terminal Ready, pin 4)
|
||||||
|
bool DTR(bool status);
|
||||||
|
bool setDTR();
|
||||||
|
bool clearDTR();
|
||||||
|
|
||||||
|
// Set RTS status (Request To Send, pin 7)
|
||||||
|
bool RTS(bool status);
|
||||||
|
bool setRTS();
|
||||||
|
bool clearRTS();
|
||||||
|
|
||||||
|
// Get RI status (Ring Indicator, pin 9)
|
||||||
|
bool isRI();
|
||||||
|
|
||||||
|
// Get DCD status (Data Carrier Detect, pin 1)
|
||||||
|
bool isDCD();
|
||||||
|
|
||||||
|
// Get CTS status (Clear To Send, pin 8)
|
||||||
|
bool isCTS();
|
||||||
|
|
||||||
|
// Get DSR status (Data Set Ready, pin 9)
|
||||||
|
bool isDSR();
|
||||||
|
|
||||||
|
// Get RTS status (Request To Send, pin 7)
|
||||||
|
bool isRTS();
|
||||||
|
|
||||||
|
// Get CTR status (Data Terminal Ready, pin 4)
|
||||||
|
bool isDTR();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Read a string (no timeout)
|
||||||
|
int readStringNoTimeOut (char *String,char FinalChar,unsigned int MaxNbBytes);
|
||||||
|
|
||||||
|
// Current DTR and RTS state (can't be read on WIndows)
|
||||||
|
bool currentStateRTS;
|
||||||
|
bool currentStateDTR;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if defined (_WIN32) || defined( _WIN64)
|
||||||
|
// Handle on serial device
|
||||||
|
HANDLE hSerial;
|
||||||
|
// For setting serial port timeouts
|
||||||
|
COMMTIMEOUTS timeouts;
|
||||||
|
#endif
|
||||||
|
#if defined (__linux__) || defined(__APPLE__)
|
||||||
|
int fd;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*! \class timeOut
|
||||||
|
\brief This class can manage a timer which is used as a timeout.
|
||||||
|
*/
|
||||||
|
// Class timeOut
|
||||||
|
class timeOut
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
timeOut();
|
||||||
|
|
||||||
|
// Init the timer
|
||||||
|
void initTimer();
|
||||||
|
|
||||||
|
// Return the elapsed time since initialization
|
||||||
|
unsigned long int elapsedTime_ms();
|
||||||
|
|
||||||
|
private:
|
||||||
|
#if defined (NO_POSIX_TIME)
|
||||||
|
// Used to store the previous time (for computing timeout)
|
||||||
|
LONGLONG counterFrequency;
|
||||||
|
LONGLONG previousTime;
|
||||||
|
#else
|
||||||
|
// Used to store the previous time (for computing timeout)
|
||||||
|
struct timeval previousTime;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // serialib_H
|
Loading…
Reference in New Issue
Block a user