41 #define _CRT_SECURE_NO_DEPRECATE //do not use windows secure crt 49 #define __FILE_ID__ "serialport" 67 if (!strcmp(j.
token,
"m")) {
72 tmp = (string)j.
token;
74 tmp +=(string)j.
token;
76 _dir = (tmp[0] ==
'<' ? 1 : 0);
78 }
else if(!strcmp(j.
token,
"t")) {
112 ,_rxCount(RXCOUNT_INVALID)
113 ,_txCount(TXCOUNT_INVALID)
114 ,_errCount(ERRCOUNT_INVALID)
115 ,_rxMsgCount(RXMSGCOUNT_INVALID)
116 ,_txMsgCount(TXMSGCOUNT_INVALID)
117 ,_lastMsg(LASTMSG_INVALID)
118 ,_currentJob(CURRENTJOB_INVALID)
119 ,_startupJob(STARTUPJOB_INVALID)
120 ,_command(COMMAND_INVALID)
121 ,_voltageLevel(VOLTAGELEVEL_INVALID)
122 ,_protocol(PROTOCOL_INVALID)
123 ,_serialMode(SERIALMODE_INVALID)
124 ,_valueCallbackSerialPort(NULL)
148 if(json_val->
has(
"rxCount")) {
151 if(json_val->
has(
"txCount")) {
154 if(json_val->
has(
"errCount")) {
157 if(json_val->
has(
"rxMsgCount")) {
160 if(json_val->
has(
"txMsgCount")) {
163 if(json_val->
has(
"lastMsg")) {
166 if(json_val->
has(
"currentJob")) {
169 if(json_val->
has(
"startupJob")) {
172 if(json_val->
has(
"command")) {
175 if(json_val->
has(
"voltageLevel")) {
178 if(json_val->
has(
"protocol")) {
181 if(json_val->
has(
"serialMode")) {
209 }
catch (std::exception) {
238 }
catch (std::exception) {
267 }
catch (std::exception) {
296 }
catch (std::exception) {
325 }
catch (std::exception) {
354 }
catch (std::exception) {
383 }
catch (std::exception) {
409 res =
_setAttr(
"currentJob", rest_val);
410 }
catch (std::exception) {
439 }
catch (std::exception) {
465 res =
_setAttr(
"startupJob", rest_val);
466 }
catch (std::exception) {
488 }
catch (std::exception) {
503 res =
_setAttr(
"command", rest_val);
504 }
catch (std::exception) {
535 }
catch (std::exception) {
564 char buf[32]; sprintf(buf,
"%d", newval); rest_val = string(buf);
565 res =
_setAttr(
"voltageLevel", rest_val);
566 }
catch (std::exception) {
603 }
catch (std::exception) {
637 res =
_setAttr(
"protocol", rest_val);
638 }
catch (std::exception) {
673 }
catch (std::exception) {
703 res =
_setAttr(
"serialMode", rest_val);
704 }
catch (std::exception) {
752 }
catch (std::exception) {
774 if (callback != NULL) {
781 if (callback != NULL && this->
isOnline()) {
851 bufflen = (int)(buff).size();
856 while ((idx < bufflen) && (ch != 0)) {
857 ch = ((u8)buff[idx]);
858 if ((ch >= 0x20) && (ch < 0x7f)) {
864 if (idx >= bufflen) {
869 return this->
_upload(
"txdata", buff);
883 return this->
_upload(
"txdata", buff);
902 bufflen = (int)byteList.size();
903 buff = string(bufflen, (
char)0);
905 while (idx < bufflen) {
906 hexb = byteList[idx];
907 buff[idx] = (char)(hexb);
911 res = this->
_upload(
"txdata", buff);
931 bufflen = (int)(hexString).length();
935 bufflen = ((bufflen) >> (1));
936 buff = string(bufflen, (
char)0);
938 while (idx < bufflen) {
939 hexb = (int)strtoul((hexString).substr( 2 * idx, 2).c_str(), NULL, 16);
940 buff[idx] = (char)(hexb);
944 res = this->
_upload(
"txdata", buff);
964 bufflen = (int)(buff).size()-2;
969 while ((idx < bufflen) && (ch != 0)) {
970 ch = ((u8)buff[idx]);
971 if ((ch >= 0x20) && (ch < 0x7f)) {
977 if (idx >= bufflen) {
982 return this->
_upload(
"txdata", buff);
1004 bufflen = (int)(
_rxbuff).size();
1014 bufflen = (int)(buff).size();
1015 if (
_rxptr == currpos+bufflen) {
1016 res = ((u8)buff[0]);
1026 bufflen = (int)(buff).size();
1027 if (
_rxptr == currpos+bufflen) {
1028 res = ((u8)buff[0]);
1038 bufflen = (int)(buff).size() - 1;
1041 while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1042 endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1044 bufflen = bufflen - 1;
1050 res = ((u8)buff[0]);
1072 if (nChars > 65535) {
1077 bufflen = (int)(buff).size() - 1;
1080 while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1081 endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1083 bufflen = bufflen - 1;
1086 res = (buff).substr( 0, bufflen);
1109 if (nChars > 65535) {
1114 bufflen = (int)(buff).size() - 1;
1117 while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1118 endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1120 bufflen = bufflen - 1;
1123 res = string(bufflen, (
char)0);
1125 while (idx < bufflen) {
1126 res[idx] = (char)(((u8)buff[idx]));
1152 if (nChars > 65535) {
1157 bufflen = (int)(buff).size() - 1;
1160 while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1161 endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1163 bufflen = bufflen - 1;
1168 while (idx < bufflen) {
1169 b = ((u8)buff[idx]);
1195 if (nBytes > 65535) {
1200 bufflen = (int)(buff).size() - 1;
1203 while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1204 endpos = endpos + mult * (((u8)buff[bufflen]) - 48);
1206 bufflen = bufflen - 1;
1211 while (ofs + 3 < bufflen) {
1212 res =
YapiWrapper::ysprintf(
"%s%02x%02x%02x%02x", res.c_str(), ((u8)buff[ofs]), ((u8)buff[ofs + 1]), ((u8)buff[ofs + 2]),((u8)buff[ofs + 3]));
1215 while (ofs < bufflen) {
1239 vector<string> msgarr;
1246 msglen = (int)msgarr.size();
1251 msglen = msglen - 1;
1252 _rxptr = atoi((msgarr[msglen]).c_str());
1285 vector<string> msgarr;
1293 msglen = (int)msgarr.size();
1298 msglen = msglen - 1;
1299 _rxptr = atoi((msgarr[msglen]).c_str());
1301 while (idx < msglen) {
1346 bufflen = (int)(buff).size() - 1;
1347 while ((bufflen > 0) && (((u8)buff[bufflen]) != 64)) {
1348 bufflen = bufflen - 1;
1350 res = atoi(((buff).substr( 0, bufflen)).c_str());
1370 vector<string> msgarr;
1377 msglen = (int)msgarr.size();
1382 msglen = msglen - 1;
1383 _rxptr = atoi((msgarr[msglen]).c_str());
1404 this->
_upload(jobfile, jsonDef);
1453 if (!((
int)(buff).size() == 1)) {
1457 res = ((u8)buff[0]) - 48;
1481 vector<string> msgarr;
1483 vector<YSnoopingRecord> res;
1489 msglen = (int)msgarr.size();
1494 msglen = msglen - 1;
1495 _rxptr = atoi((msgarr[msglen]).c_str());
1497 while (idx < msglen) {
1541 vector<string> reps;
1546 funCode = pduBytes[0];
1547 nib = ((funCode) >> (4));
1551 while (i < (
int)pduBytes.size()) {
1559 if (!((
int)reps.size() > 1)) {
1563 if ((
int)reps.size() > 1) {
1565 replen = (((int)(rep).length() - 3) >> (1));
1567 while (i < replen) {
1568 hexb = (int)strtoul((rep).substr(2 * i + 3, 2).c_str(), NULL, 16);
1569 res.push_back(hexb);
1572 if (res[0] != funCode) {
1616 pdu.push_back(0x01);
1617 pdu.push_back(((pduAddr) >> (8)));
1618 pdu.push_back(((pduAddr) & (0xff)));
1619 pdu.push_back(((nBits) >> (8)));
1620 pdu.push_back(((nBits) & (0xff)));
1623 if ((
int)reply.size() == 0) {
1626 if (reply[0] != pdu[0]) {
1633 while (bitpos < nBits) {
1634 if (((val) & (mask)) == 0) {
1639 bitpos = bitpos + 1;
1645 mask = ((mask) << (1));
1672 pdu.push_back(0x02);
1673 pdu.push_back(((pduAddr) >> (8)));
1674 pdu.push_back(((pduAddr) & (0xff)));
1675 pdu.push_back(((nBits) >> (8)));
1676 pdu.push_back(((nBits) & (0xff)));
1679 if ((
int)reply.size() == 0) {
1682 if (reply[0] != pdu[0]) {
1689 while (bitpos < nBits) {
1690 if (((val) & (mask)) == 0) {
1695 bitpos = bitpos + 1;
1701 mask = ((mask) << (1));
1727 pdu.push_back(0x03);
1728 pdu.push_back(((pduAddr) >> (8)));
1729 pdu.push_back(((pduAddr) & (0xff)));
1730 pdu.push_back(((nWords) >> (8)));
1731 pdu.push_back(((nWords) & (0xff)));
1734 if ((
int)reply.size() == 0) {
1737 if (reply[0] != pdu[0]) {
1742 while (regpos < nWords) {
1743 val = ((reply[idx]) << (8));
1745 val = val + reply[idx];
1748 regpos = regpos + 1;
1773 pdu.push_back(0x04);
1774 pdu.push_back(((pduAddr) >> (8)));
1775 pdu.push_back(((pduAddr) & (0xff)));
1776 pdu.push_back(((nWords) >> (8)));
1777 pdu.push_back(((nWords) & (0xff)));
1780 if ((
int)reply.size() == 0) {
1783 if (reply[0] != pdu[0]) {
1788 while (regpos < nWords) {
1789 val = ((reply[idx]) << (8));
1791 val = val + reply[idx];
1794 regpos = regpos + 1;
1820 pdu.push_back(0x05);
1821 pdu.push_back(((pduAddr) >> (8)));
1822 pdu.push_back(((pduAddr) & (0xff)));
1823 pdu.push_back(value);
1824 pdu.push_back(0x00);
1827 if ((
int)reply.size() == 0) {
1830 if (reply[0] != pdu[0]) {
1860 nBits = (int)bits.size();
1861 nBytes = (((nBits + 7)) >> (3));
1862 pdu.push_back(0x0f);
1863 pdu.push_back(((pduAddr) >> (8)));
1864 pdu.push_back(((pduAddr) & (0xff)));
1865 pdu.push_back(((nBits) >> (8)));
1866 pdu.push_back(((nBits) & (0xff)));
1867 pdu.push_back(nBytes);
1871 while (bitpos < nBits) {
1872 if (bits[bitpos] != 0) {
1873 val = ((val) | (mask));
1875 bitpos = bitpos + 1;
1881 mask = ((mask) << (1));
1889 if ((
int)reply.size() == 0) {
1892 if (reply[0] != pdu[0]) {
1895 res = ((reply[3]) << (8));
1896 res = res + reply[4];
1918 pdu.push_back(0x06);
1919 pdu.push_back(((pduAddr) >> (8)));
1920 pdu.push_back(((pduAddr) & (0xff)));
1921 pdu.push_back(((value) >> (8)));
1922 pdu.push_back(((value) & (0xff)));
1925 if ((
int)reply.size() == 0) {
1928 if (reply[0] != pdu[0]) {
1957 nWords = (int)values.size();
1958 nBytes = 2 * nWords;
1959 pdu.push_back(0x10);
1960 pdu.push_back(((pduAddr) >> (8)));
1961 pdu.push_back(((pduAddr) & (0xff)));
1962 pdu.push_back(((nWords) >> (8)));
1963 pdu.push_back(((nWords) & (0xff)));
1964 pdu.push_back(nBytes);
1966 while (regpos < nWords) {
1967 val = values[regpos];
1968 pdu.push_back(((val) >> (8)));
1969 pdu.push_back(((val) & (0xff)));
1970 regpos = regpos + 1;
1974 if ((
int)reply.size() == 0) {
1977 if (reply[0] != pdu[0]) {
1980 res = ((reply[3]) << (8));
1981 res = res + reply[4];
2002 int nWriteWords = 0;
2010 nWriteWords = (int)values.size();
2011 nBytes = 2 * nWriteWords;
2012 pdu.push_back(0x17);
2013 pdu.push_back(((pduReadAddr) >> (8)));
2014 pdu.push_back(((pduReadAddr) & (0xff)));
2015 pdu.push_back(((nReadWords) >> (8)));
2016 pdu.push_back(((nReadWords) & (0xff)));
2017 pdu.push_back(((pduWriteAddr) >> (8)));
2018 pdu.push_back(((pduWriteAddr) & (0xff)));
2019 pdu.push_back(((nWriteWords) >> (8)));
2020 pdu.push_back(((nWriteWords) & (0xff)));
2021 pdu.push_back(nBytes);
2023 while (regpos < nWriteWords) {
2024 val = values[regpos];
2025 pdu.push_back(((val) >> (8)));
2026 pdu.push_back(((val) & (0xff)));
2027 regpos = regpos + 1;
2031 if ((
int)reply.size() == 0) {
2034 if (reply[0] != pdu[0]) {
2039 while (regpos < nReadWords) {
2040 val = ((reply[idx]) << (8));
2042 val = val + reply[idx];
2045 regpos = regpos + 1;
2062 vector<YFUN_DESCR> v_fundescr;
2064 string serial, funcId, funcName, funcVal, errmsg;
2067 v_fundescr.size() == 0 ||
static YSerialPort * FirstSerialPort(void)
virtual string readHex(int nBytes)
virtual int read_tell(void)
yCRITICAL_SECTION _this_cs
void(* YSerialPortValueCallback)(YSerialPort *func, const string &functionValue)
static const string CURRENTJOB_INVALID
int set_protocol(const string &newval)
virtual vector< string > readMessages(string pattern, int maxWait)
string _json_get_string(const string &json)
YRETCODE _load_unsafe(int msValidity)
virtual int registerValueCallback(YSerialPortValueCallback callback)
static const string PROTOCOL_INVALID
static const int TXMSGCOUNT_INVALID
virtual int get_direction(void)
virtual vector< int > modbusReadBits(int slaveNo, int pduAddr, int nBits)
YSnoopingRecord(const string &json)
virtual int sendCommand(string text)
virtual vector< int > modbusReadInputBits(int slaveNo, int pduAddr, int nBits)
YRETCODE _upload(const string &path, const string &content)
virtual int writeBin(string buff)
static int DefaultCacheValidity
static const string LASTMSG_INVALID
virtual vector< int > modbusWriteAndReadRegisters(int slaveNo, int pduWriteAddr, vector< int > values, int pduReadAddr, int nReadWords)
string getString(const string &key)
int set_serialMode(const string &newval)
virtual vector< int > modbusReadRegisters(int slaveNo, int pduAddr, int nWords)
static const int RXCOUNT_INVALID
virtual int set_RTS(int val)
void _throw(YRETCODE errType, string errMsg)
yJsonRetCode yJsonParse(yJsonStateMachine *j)
virtual int read_avail(void)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
virtual vector< int > queryMODBUS(int slaveNo, vector< int > pduBytes)
string _download(const string &url)
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
virtual string readStr(int nChars)
void yJsonSkip(yJsonStateMachine *j, int nitems)
#define YAPI_INVALID_STRING
string get_protocol(void)
virtual int _parseAttr(YJSONObject *json_val)
virtual int modbusWriteBits(int slaveNo, int pduAddr, vector< int > bits)
virtual int read_seek(int absPos)
virtual int get_CTS(void)
virtual int modbusWriteBit(int slaveNo, int pduAddr, int value)
static const string STARTUPJOB_INVALID
int getInt(const string &key)
static u64 GetTickCount(void)
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
int set_voltageLevel(Y_VOLTAGELEVEL_enum newval)
static const string COMMAND_INVALID
virtual int _invokeValueCallback(string value)
YSerialPortValueCallback _valueCallbackSerialPort
static YFunction * _FindFromCache(const string &classname, const string &func)
vector< string > _json_get_array(const string &json)
virtual int readByte(void)
virtual int writeHex(string hexString)
static string ysprintf(const char *fmt,...)
virtual int modbusWriteRegister(int slaveNo, int pduAddr, int value)
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
static yCRITICAL_SECTION _global_cs
static const int TXCOUNT_INVALID
virtual int writeLine(string text)
virtual int uploadJob(string jobfile, string jsonDef)
virtual int modbusWriteRegisters(int slaveNo, int pduAddr, vector< int > values)
int set_currentJob(const string &newval)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
int set_command(const string &newval)
static const int RXMSGCOUNT_INVALID
virtual string readBin(int nChars)
string get_startupJob(void)
virtual string get_message(void)
virtual string queryLine(string query, int maxWait)
YSerialPort(const string &func)
Y_VOLTAGELEVEL_enum get_voltageLevel(void)
virtual vector< int > readArray(int nChars)
virtual int writeByte(int code)
virtual vector< YSnoopingRecord > snoopMessages(int maxWait)
static const Y_VOLTAGELEVEL_enum VOLTAGELEVEL_INVALID
virtual int selectJob(string jobfile)
static const string SERIALMODE_INVALID
static bool _apiInitialized
YRETCODE _nextFunction(string &hwId)
string get_currentJob(void)
virtual int writeMODBUS(string hexString)
int set_startupJob(const string &newval)
virtual int get_time(void)
virtual string readLine(void)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Y_VOLTAGELEVEL_enum _voltageLevel
static const int ERRCOUNT_INVALID
YSerialPort * nextSerialPort(void)
string get_serialMode(void)
static YSerialPort * FindSerialPort(string func)
virtual int writeStr(string text)
bool has(const string &key)
YRETCODE _setAttr(string attrname, string newvalue)
virtual int _parseAttr(YJSONObject *json_val)
virtual vector< int > modbusReadInputRegisters(int slaveNo, int pduAddr, int nWords)
virtual int writeArray(vector< int > byteList)
virtual int _invokeValueCallback(string value)