kjhjhkj
Dependencies: NetServices mbed C027_Supports mbed-rpcx
Revision 0:64967b7043c2, committed 2019-08-06
- Comitter:
- irsanjul
- Date:
- Tue Aug 06 12:59:05 2019 +0000
- Commit message:
- testes
Changed in this revision
diff -r 000000000000 -r 64967b7043c2 AruChannel.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AruChannel.cpp Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" +#include "AruChannel.h" + +extern Serial dbg; + +AruChannel::AruChannel() +{ + dbg.printf("<AruChannel>"); + + COM.connect("127.0.0.1:123"); + commId = 0; +} + +AruChannel::~AruChannel() +{ + +} + +int AruChannel::ReceiveCount() +{ + return Frm.RCV_QUE.size(); +} + +AruFrm AruChannel::Receive() +{ + AruFrm res = Frm.RCV_QUE.front(); + Frm.RCV_QUE.pop(); + return res; +} + +void AruChannel::Transmit(const AruFrm &f) +{ + XMT_QUE.push(f); +} + +void AruChannel::Process() +{ + if(COM.dataAvailable() > 0) + { + std::vector<int> r = COM.read(); + + Frm.ProcessFromAru(r); + } + + while(XMT_QUE.size() > 0) + { + AruFrm t = XMT_QUE.front(); + XMT_QUE.pop(); + + // TCP/IP + std::vector<int> x = AruFraming::CreateFrame(t); + COM.write(x); + + commId++; + } +} + +void AruChannel::Tick10ms() +{ + COM.Tick10ms(); +}
diff -r 000000000000 -r 64967b7043c2 AruChannel.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AruChannel.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,32 @@ +#ifndef ARUCHANNEL_H +#define ARUCHANNEL_H + +#include "AruFraming.h" +#include "CommChannel.h" + +class AruChannel +{ +public: + AruChannel(); + ~AruChannel(); + + int ReceiveCount(); + AruFrm Receive(); + + void Transmit(const AruFrm &f); + + void Process(); + + void Tick10ms(); + +private: + + CommChannel COM; + + AruFraming Frm; + std::queue<AruFrm> XMT_QUE; + + int commId; +}; + +#endif // ARUCHANNEL_H
diff -r 000000000000 -r 64967b7043c2 AruFraming.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AruFraming.cpp Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,156 @@ +#include "mbed.h" +#include "AruFraming.h" +//#include <stdio.h> +//#include <algorithm> + +AruFraming::AruFraming() +{ + +} + +AruFraming::~AruFraming() +{ + +} + +std::string ConvertInt(int data) +{ + char c[8]; + sprintf(c,"%X", data); + return c; +} + +void AruFraming::ProcessFromAru(const std::vector<int> &stream) +{ + AruFrm f; + if(stream[0] != 0x02)return; + + f.Identifier = stream[1]; + + switch(f.Identifier) + { + case 'e': + case 'i': + case 'r': + case 'v': + case 'n': { + unsigned int byteCnt = find(stream.begin(), stream.end(), /*'*'*/0x03) - stream.begin(); + + if (byteCnt < stream.size()) + { + for(unsigned int i=2; i<byteCnt; i++) + { + f.Data.push_back(stream[i]); + } + } + else + { + return; + } + break; + } + case 'a': + case 'c': + case 't': + case 's':{ + f.Data.clear(); + break; + } + default : + return; + } + RCV_QUE.push(f); +} + +std::vector<int> AruFraming::CreateFrame(const AruFrm &f) +{ + std::vector<int> result; + + result.push_back(0x02); + result.push_back(f.Identifier); + + int sz = f.Data.size(); + if(sz > 0) + { + for(int i=0; i<sz; i++) + { + result.push_back(f.Data[i] & 0x0FF); + } + } + + result.push_back(0x03); + + unsigned int LRC = CRC16(result); + std::string ret = ConvertInt(LRC); + std::string res = ConvertInt(LRC); + + result.push_back((int)res[2] & 0x0FF); + result.push_back((int)res[3] & 0x0FF); + result.push_back((int)res[0] & 0x0FF); + result.push_back((int)res[1] & 0x0FF); + + return result; +} + +/* Table of CRC values for high–order byte */ +unsigned int AruFraming::auchCRCHi[] = { +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40 +} ; + +/* Table of CRC values for low–order byte */ +unsigned int AruFraming::auchCRCLo[] = { +0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, +0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, +0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, +0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, +0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, +0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, +0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, +0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, +0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, +0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, +0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, +0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, +0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, +0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, +0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, +0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, +0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, +0x40 +} ; + +unsigned int AruFraming::CRC16(const std::vector<int> &puchMsg) +{ + unsigned int uchCRCHi = 0x0FF ; /* high byte of CRC initialized */ + unsigned int uchCRCLo = 0x0FF ; /* low byte of CRC initialized */ + unsigned int uIndex ; /* will index into CRC lookup table */ + + int ptr = 0; + int usDataLen = (int)puchMsg.size(); + while (usDataLen > 0) /* pass through message buffer */ + { + usDataLen--; + + uIndex = uchCRCHi ^ (unsigned int)puchMsg[ptr++] ; /* calculate the CRC */ + uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex]; + uchCRCLo = auchCRCLo[uIndex]; + } + return (uchCRCHi << 8 | uchCRCLo); +}
diff -r 000000000000 -r 64967b7043c2 AruFraming.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AruFraming.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,94 @@ +#ifndef ARUFRAMING_H +#define ARUFRAMING_H + +#include <vector> +#include <queue> +#include <string> + +//------------------------------------------------------------------------------ +class AruFrm; + +//----------------------------------------------------- +class AruFraming +{ +public: + AruFraming(); + ~AruFraming(); + + void ProcessFromAru(const std::vector<int> &stream); + + static std::vector<int> CreateFrame(const AruFrm &f); + + std::queue<AruFrm> RCV_QUE; +private: + static unsigned int auchCRCHi[]; + static unsigned int auchCRCLo[]; + + static unsigned int CRC16(const std::vector<int> &puchMsg); +}; + +//----------------------------------------------------- +class AruFrm +{ +public: + int Identifier; + std::vector<int> Data; +}; + +//----------------------------------------------------- +class Ack : public AruFrm +{ +public: + Ack(int Identifier_) + { + Identifier = Identifier_; + Data.clear(); + } +}; + +//----------------------------------------------------- +class Can : public AruFrm +{ +public: + Can(int Identifier_) + { + Identifier = Identifier_; + Data.clear(); + } +}; + +//----------------------------------------------------- +class Tef : public AruFrm +{ +public: + Tef(int Identifier_) + { + Identifier = Identifier_; + Data.clear(); + } +}; + +//----------------------------------------------------- +class Eot : public AruFrm +{ +public: + Eot(int Identifier_, std::string &data) + { + Identifier = Identifier_; + std::copy(data.begin(), data.end(), back_inserter(Data)); + } +}; + +//----------------------------------------------------- +class Eor : public AruFrm +{ +public: + Eor(int Identifier_, std::string &data) + { + Identifier = Identifier_; + std::copy(data.begin(), data.end(), Data.begin()); + } +}; + +//----------------------------------------------------- +#endif // ARUFRAMING_H
diff -r 000000000000 -r 64967b7043c2 C027_Support.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C027_Support.lib Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/irsanjul/code/C027_Supports/#305e520c136e
diff -r 000000000000 -r 64967b7043c2 CommChannel.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CommChannel.cpp Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,81 @@ +#include "mbed.h" +#include "CommChannel.h" + +extern Serial dbg; + +//----------------------------------------------------------------- +extern std::vector<int> RCV_BUFF; // temporary buffer receiving progress +std::vector<int> RCVD; // buffer completed receiving +extern int timerRxTOutCnt; + +//----------------------------------------------------------------- +// Mechanism for receive timeout +// +extern int timerRxTOutCnt; + +//----------------------------------------------------------------- +// Serial definition +// +extern Serial sock; + +//----------------------------------------------------------------- +// Wrapper class +// +CommChannel::CommChannel() +{ + dbg.printf("<CommChannel>"); +} + +CommChannel::~CommChannel() +{ + +} + +void CommChannel::connect(const std::string &host) +{ +} + +int CommChannel::dataAvailable() +{ + return RCVD.size(); +} + +std::vector<int> CommChannel::read() +{ + std::vector<int> result; + + for(int i=0; i<RCVD.size(); i++) + result.push_back(RCVD[i]); + + RCVD.clear(); + return result; +} + +void CommChannel::write(const std::vector<int> &s) +{ + for(int i=0; i<(int)s.size(); i++) + { + sock.putc(s[i] & 0x0FF); + } +} + +void CommChannel::Tick10ms() +{ + timerRxTOutCnt++; + if(timerRxTOutCnt >= 10) + { + timerRxTOutCnt = 0; + + if(RCV_BUFF.size() > 0) + { + for(int i=0; i<RCV_BUFF.size(); i++) + { + RCVD.push_back(RCV_BUFF[i]); + dbg.printf("[%02X]", RCV_BUFF[i] & 0x0FF); + } + RCV_BUFF.clear(); + } + } +} + +//-----------------------------------------------------------------
diff -r 000000000000 -r 64967b7043c2 CommChannel.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CommChannel.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,26 @@ +#ifndef COMMCHANNEL_H +#define COMMCHANNEL_H + +#include <vector> +#include <string> + +class CommChannel +{ +public: + CommChannel(); + ~CommChannel(); + + void connect(const std::string &host); + + int dataAvailable(); + std::vector<int> read(); + + void write(const std::vector<int> &s); + + void Tick10ms(); + +private: + +}; + +#endif // COMMCHANNEL_H
diff -r 000000000000 -r 64967b7043c2 DataProcess.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DataProcess.cpp Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,344 @@ +#include "mbed.h" +#include "DataProcess.h" + +extern Serial dbg; + +//----------------------------------------------------------- +// FTL Message +//----------------------------------------------------------- +FtlMessage::FtlMessage() +{ +} + +FtlMessage::~FtlMessage() +{ +} + +FtlMessage::msg FtlMessage::checkMessage(const int &lastIdentifier, const int &identifier_) +{ + switch(lastIdentifier){ + + case 'A': + if(identifier_ == 'a')return FtlMessage::OK; + else if(identifier_ == 'n') return FtlMessage::PROERROR; + else if(identifier_ == 's') return FtlMessage::NOTANSWER; + break; + + case 'C': + if(identifier_ == 'c')return FtlMessage::OK; + else if(identifier_ == 'n') return FtlMessage::PROERROR; + else if(identifier_ == 's') return FtlMessage::NOTANSWER; + break; + + case 'E': + case 'I': + if(identifier_ == 'e')return FtlMessage::OK; + else if(identifier_ == 'i')return FtlMessage::OK; + else if(identifier_ == 'v')return FtlMessage::OK; + else if(identifier_ == 'n') return FtlMessage::PROERROR; + else if(identifier_ == 's') return FtlMessage::NOTANSWER; + break; + + case 'R': + case 'V': + if(identifier_ == 'r')return FtlMessage::OK; + else if(identifier_ == 'v')return FtlMessage::OK; + else if(identifier_ == 'n') return FtlMessage::PROERROR; + else if(identifier_ == 's') return FtlMessage::NOTANSWER; + break; + + default : return FtlMessage::FAILED; + } + return FtlMessage::FAILED; +} + +//----------------------------------------------------------- +// Data Message +//----------------------------------------------------------- +DataMessage::DataMessage() +{ +} + +DataMessage::~DataMessage() +{ +} + +DataMessage::msg DataMessage::processFromSlave(int identifier_,std::string buff, _CALLBACKPTR cb, void* param) +{ + int res = 0; + switch(identifier_){ + case 'e': + case 'i': + case 'v': + res = cb(buff.c_str(), buff.length(), param); + break; + } + if(res) return DataMessage::OK; + else return DataMessage::FAILED; +} + +int DataMessage::_cbstate(const char* buf, int len, MeterState* param) +{ + if (param) + { + if(sscanf(buf, "%*[^= ]=%*d,%*[^, ],%*[^, ],%d", param->state) ==1) + { + return 1; + } + } + return 0; +} + +DataMessage::msg DataMessage::checkStateMessage(const int &identifier_, const std::string &stream, int *pStep) +{ + MeterState mystate; + mystate.state = pStep; + if(processFromSlave(identifier_, stream, _cbstate, &mystate) == DataMessage::OK) + { + dbg.printf("SUKSES BRO\r\n"); + return DataMessage::OK; + } + else + { + dbg.printf("GAGAL BRO\r\n"); + return DataMessage::FAILED; + } +} + +int DataMessage::_cbOstate(const char* buf, int len, OrderState* param) +{ + if (param) + { + if(sscanf(buf, "%*[^= ]=%d", param->state) ==1) + { + return 1; + } + } + return 0; +} + +DataMessage::msg DataMessage::checkOrderState(const int &identifier_, const std::string &stream, int *pStep) +{ + OrderState mystate; + mystate.state = pStep; + if(processFromSlave(identifier_, stream, _cbOstate, &mystate) == DataMessage::OK) + { + dbg.printf("SUKSES BRO\r\n"); + return DataMessage::OK; + } + else + { + dbg.printf("GAGAL BRO\r\n"); + return DataMessage::FAILED; + } +} + +int DataMessage::_cbInterlock(const char* buf, int len, InterlockState* param) +{ + if (param) + { + if(sscanf(buf, "%*[^= ]=%*d,%*d,%d,%*s", param->state) ==1) + { + return 1; + } + } + return 0; +} + +DataMessage::msg DataMessage::checkInterlock(const int &identifier_, const std::string &stream, int *pStep) +{ + InterlockState mystate; + mystate.state = pStep; + if(processFromSlave(identifier_, stream, _cbInterlock, &mystate) == DataMessage::OK) + { + dbg.printf("SUKSES BRO\r\n"); + return DataMessage::OK; + } + else + { + dbg.printf("GAGAL BRO\r\n"); + return DataMessage::FAILED; + } +} + +//----------------------------------------------------------- +DataProcess::DataProcess() +{ + stateTmp = 0; + lastState = 0; + OrderStateTmp = 0; + LastOrderState = 0; + InterlockState = 0; +} + +DataProcess::~DataProcess() +{ +} + +void DataProcess::CheckFtlMassage(const int &lastIdentifier, const AruFrm &frm) +{ + FtlMsg = FtlMessage::checkMessage(lastIdentifier, frm.Identifier); + return; +} + +FtlMessage::msg DataProcess::getFtlMessage() +{ + return FtlMsg; +} + +DataProcess::state_ DataProcess::checkState() +{ + DataProcess::state_ ret; +// int res = stateTmp - lastState; + + switch(lastState) + { + case 0: + if(stateTmp == 0)ret = DataProcess::STAYNOL; + else if(stateTmp == 1)ret = DataProcess::STEPUP; + else if(stateTmp == 2)ret = DataProcess::STEPJUMP; + else if(stateTmp == 3)ret = DataProcess::STEPJUMP; + else if(stateTmp == 4)ret = DataProcess::STEPEND; + else if(stateTmp == 5)ret = DataProcess::FINISH; + break; + case 1: + if(stateTmp == 0)ret = DataProcess::STEPDOWN; + else if(stateTmp == 1)ret = DataProcess::STAY; + else if(stateTmp == 2)ret = DataProcess::STEPUP; + else if(stateTmp == 3)ret = DataProcess::STEPJUMP; + else if(stateTmp == 4)ret = DataProcess::STEPEND; + else if(stateTmp == 5)ret = DataProcess::FINISH; + break; + case 2: + if(stateTmp == 0)ret = DataProcess::STEPEND; + else if(stateTmp == 1)ret = DataProcess::STEPDOWN; + else if(stateTmp == 2)ret = DataProcess::STAY; + else if(stateTmp == 3)ret = DataProcess::STEPUP; + else if(stateTmp == 4)ret = DataProcess::STEPEND; + else if(stateTmp == 5)ret = DataProcess::ERROR; + break; + case 3: + if(stateTmp == 0)ret = DataProcess::STEPEND; + else if(stateTmp == 1)ret = DataProcess::ERROR; + else if(stateTmp == 2)ret = DataProcess::STEPDOWN; + else if(stateTmp == 3)ret = DataProcess::STAY; + else if(stateTmp == 4)ret = DataProcess::STEPEND; + else if(stateTmp == 5)ret = DataProcess::ERROR; + break; + case 4: + if(stateTmp == 0)ret = DataProcess::STEPEND; + else if(stateTmp == 1)ret = DataProcess::ERROR; + else if(stateTmp == 2)ret = DataProcess::STEPDOWN; + else if(stateTmp == 3)ret = DataProcess::STEPDOWN; + else if(stateTmp == 4)ret = DataProcess::STAY; + else if(stateTmp == 5)ret = DataProcess::FINISH; + break; + case 5: + if(stateTmp == 0)ret = DataProcess::STEPEND; + else if(stateTmp == 1)ret = DataProcess::ERROR; + else if(stateTmp == 2)ret = DataProcess::ERROR; + else if(stateTmp == 3)ret = DataProcess::ERROR; + else if(stateTmp == 4)ret = DataProcess::STEPEND; + else if(stateTmp == 5)ret = DataProcess::FINISH; + break; + default : ret = DataProcess::ERROR; break; + } + + lastState = stateTmp; + return ret; +} + +DataProcess::state_ DataProcess::checkOrderState() +{ + DataProcess::state_ ret; + switch(OrderStateTmp) + { + case 0: + ret = DataProcess::NOORDER; + break; + + case 2: + ret = DataProcess::ORDERRCV; + break; + + case 3: + ret = DataProcess::PLANORDERPROC; + break; + + case 4: + ret = DataProcess::UNPLANORDERPROC; + break; + + case 9: + ret = DataProcess::ORDERFINERROR; + break; + + case 10: + ret = DataProcess::ORDERFINNOERROR; + break; + } + + return ret; +} + +DataProcess::state_ DataProcess::chackInterlock() +{ + DataProcess::state_ ret; + if(InterlockState == 1) + { + ret = DataProcess::ACCCLOSED; + } + else if (InterlockState == 2) + { + ret = DataProcess::ACCOPEN; + } + else + { + ret = DataProcess::ERROR; + } + + return ret; +} + +void DataProcess::CheckDtMassage(const AruFrm &frm) +{ + std::string input(frm.Data.begin(), frm.Data.end()); + + switch(frm.Identifier){ + case 'e': + case 'i': + case 'v': + { + if(strstr(input.c_str(), "Meter_State")) + { + DataMsg = DataMessage::checkStateMessage(frm.Identifier, input, &stateTmp); + } + else if(strstr(input.c_str(), "ORDER,State")) + { + DataMsg = DataMessage::checkOrderState(frm.Identifier, input, &OrderStateTmp); + } + else if(strstr(input.c_str(), "Interlock")) + { + DataMsg = DataMessage::checkInterlock(frm.Identifier, input, &InterlockState); + } + else if(strstr(input.c_str(), "Meter_Info")) + { + DataMsg = DataMessage::OK; + } + else + { + DataMsg = DataMessage::FAILED; + } + } + break; + } + + return; +} + +DataMessage::msg DataProcess::getDtMessage() +{ + return DataMsg; +} + +//-----------------------------------------------------------
diff -r 000000000000 -r 64967b7043c2 DataProcess.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DataProcess.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,125 @@ +#ifndef DATAPROCESS_H +#define DATAPROCESS_H + +#include <vector> +#include <queue> +#include <string> +#include "AruFraming.h" + +class FtlMessage +{ +public: + FtlMessage(); + ~FtlMessage(); + + enum msg + { + OK = 0, + FAILED = -1, + PROERROR = -2, + NOTANSWER = -3 + }ftl_msg; + + msg checkMessage(const int &lastIdentifier, const int &identifier_); + +private: + +}; + +//------------------------------------------------------------- +class DataMessage +{ +public: + DataMessage(); + ~DataMessage(); + + enum msg + { + OK = 0, + FAILED = -1, + CHANGE = 1, + + STATECHANGE = 2, + + }data_msg; + + typedef int (*_CALLBACKPTR)(const char* buf, int len, void* param); + msg processFromSlave(int identifier_=0, std::string buf="", _CALLBACKPTR cb = NULL, void* param = NULL); + + template<class T> + inline msg processFromSlave(int identifier_, std::string buf, int (*cb)(const char* buf, int len, T* param), + T* param) + { + return processFromSlave(identifier_, buf, (_CALLBACKPTR)cb, (void*)param); + } + + msg checkStateMessage(const int &identifier_, const std::string &stream, int *pStep); + msg checkOrderState(const int &identifier_, const std::string &stream, int *pStep); + msg checkInterlock(const int &identifier_, const std::string &stream, int *pStep); +//private: + +protected: + + typedef struct{ int *state;} MeterState; + static int _cbstate(const char* buf, int len, MeterState* param); + + typedef struct{ int *state;} OrderState; + static int _cbOstate(const char* buf, int len, OrderState* param); + + typedef struct{int *state;} InterlockState; + static int _cbInterlock(const char* buf, int len, InterlockState* param); +}; + +//------------------------------------------------------------- +class DataProcess : public FtlMessage, public DataMessage +{ +public: + DataProcess(); + ~DataProcess(); + + typedef enum state_ + { + OK = 0, + ERROR = -1, + + STAY = 0, + STEPUP = 1, + STEPDOWN = 2, + STEPJUMP = 3, + STEPEND = -1, + STAYNOL = -2, + FINISH = 4, + + NOORDER = 0, + ORDERRCV = 2, + PLANORDERPROC = 3, + UNPLANORDERPROC = 4, + ORDERFINERROR = 9, + ORDERFINNOERROR = 10, + + ACCCLOSED = 0, + ACCOPEN = 1 + }STATE; + + void CheckFtlMassage(const int &lastIdentifier, const AruFrm &frm); + FtlMessage::msg getFtlMessage(); + + void CheckDtMassage(const AruFrm &frm); + DataMessage::msg getDtMessage(); + state_ checkState(); + state_ checkOrderState(); + state_ chackInterlock(); + +private: + FtlMessage::msg FtlMsg; + + DataMessage::msg DataMsg; + int stateTmp; + int lastState; + int OrderStateTmp; + int LastOrderState; + int InterlockState; +}; + +//------------------------------------------------------------- +#endif // DATAPROCESS_H
diff -r 000000000000 -r 64967b7043c2 Modem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modem.cpp Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,136 @@ +#include "MDM.h" +#include "mbed.h" +#include "Modem.h" +#include <sstream> + +extern Serial dbg; + +void SetModem(MDMSerial &mdm) +{ + MDMParser::DevStatus devStatus = {}; + MDMParser::NetStatus netStatus = {}; + bool mdmOk = mdm.init(SIMPIN, &devStatus); + mdm.dumpDevStatus(&devStatus); + if (mdmOk) { + mdmOk = mdm.registerNet(&netStatus); + mdm.dumpNetStatus(&netStatus); + } + if (mdmOk) + { + MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); + if (ip != NOIP) + { + mdm.dumpIp(ip); + dbg.printf("\r\n<Modem Connected>"); + } + } +} + +std::string Convertint(int data) +{ + std::stringstream c; + c << data; + + return c.str(); +} + +std::string Convert(double data) +{ + std::stringstream c; + c << data; + + return c.str(); +} + +bool PostData(MDMSerial &mdm, const int &identifier_, const std::vector<int> &input, bool mode) +{ + char buf[512] = ""; + int ret; + bool res = false; + int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); + + std::string body; + body += "{\"val\":\""; + body += identifier_; + body += std::string(input.begin(), input.end()); + body += "\"}"; + + std::string http; + if(mode == SKADA) + { + http += "PUT /todo/api/v1.0/scadas/1 HTTP/1.1\r\n"; + } + else if(mode == TASK) + { + http += "POST /todo/api/v1.0/tasks HTTP/1.1\r\n"; + } + http += "Host: 182.23.67.168:5000\r\n"; + http += "Authorization: Basic am9objpoZWxsbw==\r\n"; + http += "Connection: close\r\n"; + http += "Content-Type: application/json\r\n"; + http += "Content-Length: "; + http += Convertint((int)body.size()); + http += "\r\n\r\n"; + http += body; + http += "\r\n\r\n"; + + dbg.printf("Request:\r\n%s",http.c_str()); + + dbg.printf("Ready to post\r\n"); + mdm.socketSetBlocking(socket, 10000); + if (mdm.socketConnect(socket, "182.23.67.168", 5000)) + { + dbg.printf("Sending request\r\n"); + mdm.socketSend(socket, http.c_str(), http.size()); + + ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); + if (ret > 0) + { + dbg.printf("Socket Recv \"%*s\"\r\n", ret, buf); + if(strstr(buf, "HTTP/1.0 200") || strstr(buf, "HTTP/1.0 201")) + { + res = true; + } + else + { + res = false; + } + } + else + { + dbg.printf("return 0\r\n"); + res = false; + } + + mdm.socketClose(socket); + mdm.socketFree(socket); + } + else + { + mdm.socketSetBlocking(socket, 10000); + mdm.socketConnect(socket, "182.23.67.168", 5000); + res = false; + } + return res; +} + +bool chekConect(MDMSerial &mdm) +{ + bool stat = mdm.CheckCon(); + if(!stat) + { + MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); + if (ip != NOIP) + { + mdm.dumpIp(ip); + return true; + } + else + { + mdm.join(APN,USERNAME,PASSWORD); + return false; + } + } + else + return true; +}
diff -r 000000000000 -r 64967b7043c2 Modem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modem.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,19 @@ +#ifndef MODEMINIT_H +#define MODEMINIT_H + +#include "MDM.h" +#include <vector> + +#define TASK false +#define SKADA true + +#define SIMPIN NULL +#define APN NULL +#define USERNAME NULL +#define PASSWORD NULL + +void SetModem(MDMSerial &mdm); +bool PostData(MDMSerial &mdm, const int &identifier_, const std::vector<int> &input, bool mode=SKADA); +bool chekConect(MDMSerial &mdm); + +#endif \ No newline at end of file
diff -r 000000000000 -r 64967b7043c2 NetServices.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetServices.lib Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/irsanjul/code/NetServices/#361d9e984660
diff -r 000000000000 -r 64967b7043c2 RtuRpc.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RtuRpc.cpp Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,57 @@ +#include "RtuRpc.h" +#include "mbed.h" +#include <sstream> + +extern Serial dbg; + +RtuRpc::RtuRpc(std::string host) +{ + hostAddr = host; +} + +void RtuRpc::commitReading(const std::time_t &dt, int readingId, double value) +{ + HTTPText dat; + + std::tm * ptm = std::localtime(&dt); + char buffer[48]; + std::strftime(buffer, 48, "%Y-%m-%d-%H:%M:%S", ptm); + + char sent[50]; + sprintf(sent,"%s/commitReading.php?w=%s&r=%d&v=%.2f", hostAddr.c_str(), buffer, readingId, value); + + HTTPResult res = cl.get(sent, &dat); + + if(res == HTTP_OK) + { + dbg.printf("HTTP_OK\r\n"); + } + else + { + dbg.printf("Error %d\r\n", res); + } +} + +void RtuRpc::commitEvent(const std::time_t &dt, int eventId, double value) +{ + HTTPText dat; + + std::tm * ptm = std::localtime(&dt); + char buffer[48]; + + std::strftime(buffer, 48, "%Y-%m-%d-%H:%M:%S", ptm); + + char sent[50]; + sprintf(sent,"%s/commitReading.php?w=%s&r=%d&v=%.2f", hostAddr.c_str(), buffer, eventId, value); + + HTTPResult res = cl.get(sent, &dat); + + if(res == HTTP_OK) + { + dbg.printf("HTTP_OK\r\n"); + } + else + { + dbg.printf("Error %d\r\n", res); + } +}
diff -r 000000000000 -r 64967b7043c2 RtuRpc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RtuRpc.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,26 @@ +#ifndef RTURPC_H +#define RTURPC_H + +#include "HTTPClient.h" +#include <string> +#include <ctime> + +class RtuRpc +{ +public: + + RtuRpc(std::string host); + + void commitReading(const std::time_t &dt, int readingId, double value); + + void commitEvent(const std::time_t &dt, int eventId, double value); + +private: + + std::string hostAddr; + + HTTPClient cl; + +}; + +#endif
diff -r 000000000000 -r 64967b7043c2 debug.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debug.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,16 @@ +#ifndef __DEBUG_H__ +#define __DEBUG_H__ + + +#ifdef DEBUG +#define INFO(x, ...) std::printf("[INFO: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WARN: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#endif + + +#endif \ No newline at end of file
diff -r 000000000000 -r 64967b7043c2 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,354 @@ +/******************************************************************************* + Project : Aru Master + Version : 1.2.0 + *******************************************************************************/ +#include "mbed.h" +#include "C027_api.h" +#include "AruFraming.h" +#include "AruChannel.h" +#include "DataProcess.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" +//#include "RtuRpc.h" +#include "Modem.h" +#include "MDM.h" +#include "modeldata.h" +#include <sstream> + +//------------------------------------------------------------------------------ +// Pin Init +//------------------------------------------------------------------------------ +DigitalOut led(LED); // Status Led +Serial dbg(USBTX, USBRX); // Serial to PC +Serial sock(D1, D0); // Aru Master Line +DigitalOut pin(MDMPWRON, 0); +DigitalOut led1(D9, 0), led2(D8, 0), led3(D7, 0); + +//------------------------------------------------------------------------------ +// Variable Init +//------------------------------------------------------------------------------ +std::vector<int> RCV_BUFF; // temporary buffer receiving progress +int timerRxTOutCnt; +bool AruReqTick; +bool tmr10msTick; +bool PriSnd; // true = primary, false = secondary +bool SendNow; +int steptelegram; +int itung; +bool SkadaTask; + +//------------------------------------------------------------------------------ +// Function Init +//------------------------------------------------------------------------------ +//RtuRpc cmd("180.235.151.2"); + +//------------------------------------------------------------------------------ +// Serial Interrupt +void rxInterupt() +{ + RCV_BUFF.push_back(sock.getc()); + timerRxTOutCnt = 0; +} + +//------------------------------------------------------------------------------ +// FTL protocol handling +// +AruChannel ARU; + +//------------------------------------------------------------------------------ +// Aru pooling mechanism +// +Ticker timerAru; +void timerAruTick(); + +//------------------------------------------------------------------------------ +// Mechanism timer 10ms for all +// +Ticker timer10ms; +void timer10msTick() +{ + tmr10msTick = true; +} + +//------------------------------------------------------------------------------ +// Blinking +// +Ticker timer1s; +void timer1sTick() +{ + led = !led; + led1 = !led1; +} + +//------------------------------------------------------------------------------ +// Data Processing +// +DataProcess Data; + +//------------------------------------------------------------------------------ +// Main Program +//------------------------------------------------------------------------------ +int main() +{ +// Clear MODBUS Timers + tmr10msTick = false; + AruReqTick = false; + PriSnd = true; + SendNow = false; + SkadaTask = SKADA; + + dbg.baud(9600); + dbg.format(8,SerialBase::None,1); + + sock.baud(9600); + sock.format(8,SerialBase::None,1); + + printf("\r\n<Setup is OK>\r\n"); + Net::poll(); + + dbg.printf("<main>\r\n"); + + sock.attach(rxInterupt); + timerRxTOutCnt = 0; + + dbg.printf("<Modem Created>\r\n"); + MDMSerial mdm; + SetModem(mdm); + + timerAru.attach(&timerAruTick, 8); + timer10ms.attach_us(&timer10msTick, 10000); + + timer1s.attach(&timer1sTick,1); + + steptelegram = 0; + itung = 0; + + while(1) + { + Net::poll(); + + if(tmr10msTick) + { + tmr10msTick = false; + ARU.Tick10ms(); + } + + if(AruReqTick) + { + AruReqTick = false; + + if(PriSnd) + { + Eot r('E', TelegramState[steptelegram]); + ARU.Transmit(r); + } + else + { + Eot r('I', TelegramState[steptelegram]); + ARU.Transmit(r); + } + PriSnd = !PriSnd; + dbg.printf("\r\n<Start Send Querry with step %d>\n\r", steptelegram); + } + + ARU.Process(); + + if(ARU.ReceiveCount() > 0) + { + led2 = 1; + dbg.printf("\r\n<Receive Data>\n\r"); + + AruFrm mf = ARU.Receive(); + + dbg.printf("Identifier : %c\r\n", mf.Identifier); + dbg.printf("Data input :\r\n"); + unsigned int sz = mf.Data.size(); + dbg.printf("%c", mf.Identifier); + for(unsigned int i=0; i<sz; i++) + { + dbg.printf("%c", mf.Data[i]); + } + dbg.printf("\r\n"); + Data.CheckFtlMassage('E', mf); + + if(Data.getFtlMessage() == FtlMessage::OK) + { + Data.CheckDtMassage(mf); + if(Data.getDtMessage() == DataMessage::OK) + { + switch(steptelegram) + { + case 0 : /*Order_state*/ + { + DataProcess::STATE st = Data.checkOrderState(); + + if(st == Data.NOORDER) + { + dbg.printf("No Order Present\r\n"); + } + else if(st == Data.ORDERRCV) + { + dbg.printf("Order Receive\r\n"); + } + else if(st == Data.PLANORDERPROC) + { + dbg.printf("Order Process\r\n"); + steptelegram = 1; + } + else if(st == Data.UNPLANORDERPROC) + { + dbg.printf("Order Process\r\n"); + steptelegram = 1; + } + else if(st == Data.ORDERFINERROR) + { + dbg.printf("Order Finish with error\r\n"); + } + else if(st == Data.ORDERFINNOERROR) + { + dbg.printf("Order Finish without error\r\n"); + } + SendNow = true; + SkadaTask = SKADA; + break; + } + case 1 : /* Interlock */ + { + DataProcess::STATE st = Data.chackInterlock(); + if(st == Data.ACCCLOSED) + { + dbg.printf("Access is closed, please wait\r\n"); + } + else if(st == Data.ACCOPEN) + { + dbg.printf("Access is opened, goto meter_state\r\n"); + steptelegram = 2; + SendNow = true; + SkadaTask = SKADA; + } + break; + } + case 2 : /*Meter_state*/ + { + DataProcess::STATE st = Data.checkState(); + + if(st == Data.STEPDOWN) + { + dbg.printf("STEPDOWN\r\n"); + } + else if(st == Data.STEPUP) + { + dbg.printf("STEPUP\r\n"); + } + else if(st == Data.STEPJUMP) + { + dbg.printf("STEP LONCAT\r\n"); + } + else if(st == Data.STEPEND) + { + dbg.printf("JUMP TO END\r\n"); + steptelegram = 3; + } + else if(st == Data.STAY) + { + dbg.printf("STEP TETAP\r\n"); + } + else if(st == Data.FINISH) + { + dbg.printf("FINISH\r\n"); + steptelegram = 3; + } + else if(st == Data.STAYNOL) + { + dbg.printf("STEP TETAP DI NOL\r\n"); + itung++; + if(itung > 10) + { + itung = 0; + steptelegram = 0; + } + } + else if(st == Data.ERROR) + { + dbg.printf("STEP ERROR COY\r\n"); + steptelegram = 6; // error querry + } + else + { + dbg.printf("STEP GA NORMAL\r\n"); + } + SendNow = true; + SkadaTask = SKADA; + break; + } + case 3 : + { + steptelegram = 4; + SendNow = true; + SkadaTask = TASK; + break; + } + case 4 : + { + steptelegram = 0; + SendNow = true; + SkadaTask = TASK; + break; + } + case 5 : + { + SendNow = true; + SkadaTask = TASK; + break; + } + case 6 : + { + steptelegram = 0; + SendNow = true; + SkadaTask = TASK; + break; + } + default: break; + } + } + else + { + dbg.printf("Data Ditolak\r\n"); + steptelegram = 0; + } + + if(chekConect(mdm) && SendNow) + { + SendNow = false; + + if(PostData(mdm, mf.Identifier, mf.Data, SkadaTask)) + { + dbg.printf("Data Berhasil dikirim\r\n"); + } + else + { + dbg.printf("Gagal Kirim Data\r\n"); + } + } + } + else if(Data.getFtlMessage() == FtlMessage::PROERROR) + { + dbg.printf("Protokol Error GAN!!!\r\n"); + steptelegram = 0; + } + else + { + dbg.printf("Balasan Tidak sesuai coy!!!\r\n"); + steptelegram = 0; + } + led2 = 0; + } + } // akhir while(1) +}// akhir main + +//------------------------------------------------------------------- +void timerAruTick() +{ + AruReqTick = true; +} +//-------------------------------------------------------------------
diff -r 000000000000 -r 64967b7043c2 mbed-rpc.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rpc.lib Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/irsanjul/code/mbed-rpcx/#e7fffe9a856a
diff -r 000000000000 -r 64967b7043c2 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8ab26030e058 \ No newline at end of file
diff -r 000000000000 -r 64967b7043c2 modeldata.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modeldata.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,36 @@ +#ifndef MODELDATA_H +#define MODELDATA_H + +//------------------------------------------------------------------------------ +// Model Data +//------------------------------------------------------------------------------ +// Alur Step +// order state => interlock => meterstate => meter_info +// **************************************************** +// Order State +// - 0 saat "No Order Present" +// - 4 saat Order di proses +// **************************************************** +// interlock +// - dapet langsung kirim +// **************************************************** +// meter state +// - ambil state untuk nentuin step +// - pas sampe 4 baru lanjut +// **************************************************** +// meter info +// - langsung kirim +//------------------------------------------------------------------------------ +string MeterStateTelegram = "ENQ,FTL,OPERATION,Meter_State"; +string OrderStateTelegram = "ENQ,FTL,ORDER,State"; +string InterlockTelegram = "ENQ,FTL,OPERATION,Interlock"; +string MeterInfoTelegram = "ENQ,FTL,OPERATION,Meter_Info"; +string LFileTelegram = "ENQ,FTL,LOG,L_File"; +string AckTelegram = "A"; +string ErrorTelegram = "ENQ,HAAR,Warnings"; + +string TelegramState[7] = { OrderStateTelegram, InterlockTelegram, + MeterStateTelegram, MeterInfoTelegram, + LFileTelegram, AckTelegram, ErrorTelegram}; + +#endif
diff -r 000000000000 -r 64967b7043c2 myRpc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/myRpc.h Tue Aug 06 12:59:05 2019 +0000 @@ -0,0 +1,15 @@ +#ifndef MYRPC_H +#define MYRPC_H + +#include "ip_addr.h" + +namespace mbed{ +void rpc(const char* func, char* param){ +} +}; + +void dns_gethostbyname(const char* nama, ip_addr * ip, void(*cbFunc)(const char*, ip_addr*, void*), void* param){ + +}; + +#endif \ No newline at end of file