|
|
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment">/*</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span> <span class="comment"> ModbusSerial.h - Header for ModbusSerial Library</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="comment"> Copyright (C) 2014 André Sarmento Barbosa</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="comment">*/</span></div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="preprocessor">#include <Arduino.h></span></div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="preprocessor">#include <Modbus.h></span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span> </div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="preprocessor">#ifndef MODBUSSERIAL_H</span></div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span> <span class="preprocessor">#define MODBUSSERIAL_H</span></div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span> </div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="comment">//#define USE_SOFTWARE_SERIAL</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span> </div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span> <span class="preprocessor">#ifdef USE_SOFTWARE_SERIAL</span></div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span> <span class="preprocessor">#include <SoftwareSerial.h></span></div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span> <span class="preprocessor">#endif</span></div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span> </div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span> <span class="keyword">enum</span> MB_PARITY {</div><div class="line"><a name="l00045"></a><span class="lineno"> 45</span>  MB_PARITY_NONE = SERIAL_8N2, </div><div class="line"><a name="l00046"></a><span class="lineno"> 46</span>  MB_PARITY_EVEN = SERIAL_8E1, </div><div class="line"><a name="l00047"></a><span class="lineno"> 47</span>  MB_PARITY_ODD = SERIAL_8O1 </div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span> };</div><div class="line"><a name="l00049"></a><span class="lineno"> 49</span> </div><div class="line"><a name="l00054"></a><span class="lineno"><a class="line" href="classModbusSerial.html"> 54</a></span> <span class="keyword">class </span><a class="code" href="classModbusSerial.html">ModbusSerial</a> : <span class="keyword">public</span> <a class="code" href="classModbus.html">Modbus</a> {</div><div class="line"><a name="l00055"></a><span class="lineno"> 55</span> <span class="preprocessor">#ifndef __DOXYGEN__</span></div><div class="line"><a name="l00056"></a><span class="lineno"> 56</span>  <span class="keyword">private</span>:</div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  Stream* _port;</div><div class="line"><a name="l00058"></a><span class="lineno"> 58</span>  <span class="keywordtype">long</span> _baud;</div><div class="line"><a name="l00059"></a><span class="lineno"> 59</span>  byte _format;</div><div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  <span class="keywordtype">int</span> _txPin;</div><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> _t15; <span class="comment">// inter character time out</span></div><div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> _t35; <span class="comment">// frame delay</span></div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span>  byte _slaveId;</div><div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  word calcCrc(byte address, byte* pduframe, byte pdulen);</div><div class="line"><a name="l00065"></a><span class="lineno"> 65</span> <span class="preprocessor">#endif</span></div><div class="line"><a name="l00066"></a><span class="lineno"> 66</span>  <span class="keyword">protected</span>:</div><div class="line"><a name="l00067"></a><span class="lineno"> 67</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#a3fb991a7b8d0d9766889e2cad7fca115">receive</a>(byte* frame);</div><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#a69850777f84662664dd17e137250e3b0">sendPDU</a>(byte* pduframe);</div><div class="line"><a name="l00069"></a><span class="lineno"> 69</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#ae7b2f3574d102b26a1de084cb345b6cc">send</a>(byte* frame);</div><div class="line"><a name="l00070"></a><span class="lineno"> 70</span> </div><div class="line"><a name="l00071"></a><span class="lineno"> 71</span>  <span class="keyword">public</span>:</div><div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  <a class="code" href="classModbusSerial.html#add48c6a2b436e600865e0cf63ed0aafc">ModbusSerial</a>();</div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#aefa9a7f9a910a311bfcd5110a6a8ab71">config</a>(HardwareSerial * port, <span class="keywordtype">long</span> baud, byte parity=MB_PARITY_EVEN, <span class="keywordtype">int</span> txenPin=-1);</div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span> </div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span> <span class="preprocessor">#ifdef __DOXYGEN__</span></div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span> </div><div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#aefa9a7f9a910a311bfcd5110a6a8ab71">config</a>(SoftwareSerial* port, <span class="keywordtype">long</span> baud, <span class="keywordtype">int</span> txenPin=-1);</div><div class="line"><a name="l00112"></a><span class="lineno"> 112</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#aefa9a7f9a910a311bfcd5110a6a8ab71">config</a>(Serial_ * port, <span class="keywordtype">long</span> baud, byte parity=MB_PARITY_EVEN, <span class="keywordtype">int</span> txenPin=-1);</div><div class="line"><a name="l00113"></a><span class="lineno"> 113</span> <span class="preprocessor">#else </span></div><div class="line"><a name="l00114"></a><span class="lineno"> 114</span> <span class="preprocessor"> #ifdef USE_SOFTWARE_SERIAL</span></div><div class="line"><a name="l00115"></a><span class="lineno"> 115</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#aefa9a7f9a910a311bfcd5110a6a8ab71">config</a>(SoftwareSerial* port, <span class="keywordtype">long</span> baud, <span class="keywordtype">int</span> txenPin=-1);</div><div class="line"><a name="l00116"></a><span class="lineno"> 116</span> <span class="preprocessor"> #endif</span></div><div class="line"><a name="l00117"></a><span class="lineno"> 117</span> <span class="preprocessor"> #ifdef __AVR_ATmega32U4__</span></div><div class="line"><a name="l00118"></a><span class="lineno"> 118</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#aefa9a7f9a910a311bfcd5110a6a8ab71">config</a>(Serial_ * port, <span class="keywordtype">long</span> baud, byte parity=MB_PARITY_EVEN, <span class="keywordtype">int</span> txenPin=-1);</div><div class="line"><a name="l00119"></a><span class="lineno"> 119</span> <span class="preprocessor"> #endif</span></div><div class="line"><a name="l00120"></a><span class="lineno"> 120</span> <span class="preprocessor">#endif</span></div><div class="line"><a name="l00121"></a><span class="lineno"> 121</span> </div><div class="line"><a name="l00126"></a><span class="lineno"> 126</span>  <span class="keywordtype">void</span> <a class="code" href="classModbusSerial.html#ad47e2b6a70d42a106a1a55a515e2f33a">task</a>();</div><div class="line"><a name="l00132"></a><span class="lineno"> 132</span>  <span class="keywordtype">bool</span> <a class="code" href="classModbusSerial.html#a2b727d4d516177e698db6e49729624d8">setSlaveId</a>(byte slaveId);</div><div class="line"><a name="l00136"></a><span class="lineno"> 136</span>  byte <a class="code" href="classModbusSerial.html#adefaff8d3e8f4cfc1d90c9b52d880262">getSlaveId</a>();</div><div class="line"><a name="l00137"></a><span class="lineno"> 137</span> };</div><div class="line"><a name="l00138"></a><span class="lineno"> 138</span> </div><div class="line"><a name="l00139"></a><span class="lineno"> 139</span> <span class="preprocessor">#ifndef __DOXYGEN__</span></div><div class="line"><a name="l00140"></a><span class="lineno"> 140</span> <span class="comment">/* Table of CRC values for highorder byte */</span></div><div class="line"><a name="l00141"></a><span class="lineno"> 141</span> <span class="keyword">const</span> byte _auchCRCHi[] = {</div><div class="line"><a name="l00142"></a><span class="lineno"> 142</span>  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,</div><div class="line"><a name="l00143"></a><span class="lineno"> 143</span>  0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,</div><div class="line"><a name="l00144"></a><span class="lineno"> 144</span>  0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,</div><div class="line"><a name="l00145"></a><span class="lineno"> 145</span>  0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,</div><div class="line"><a name="l00146"></a><span class="lineno"> 146</span>  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,</div><div class="line"><a name="l00147"></a><span class="lineno"> 147</span>  0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,</div><div class="line"><a name="l00148"></a><span class="lineno"> 148</span>  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,</div><div class="line"><a name="l00149"></a><span class="lineno"> 149</span>  0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,</div><div class="line"><a name="l00150"></a><span class="lineno"> 150</span>  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,</div><div class="line"><a name="l00151"></a><span class="lineno"> 151</span>  0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,</div><div class="line"><a name="l00152"></a><span class="lineno"> 152</span>  0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,</div><div class="line"><a name="l00153"></a><span class="lineno"> 153</span>  0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,</div><div class="line"><a name="l00154"></a><span class="lineno"> 154</span>  0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,</div><div class="line"><a name="l00155"></a><span class="lineno"> 155</span>  0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,</div><div class="line"><a name="l00156"></a><span class="lineno"> 156</span>  0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,</div><div class="line"><a name="l00157"></a><span class="lineno"> 157</span>  0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,</div><div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,</div><div class="line"><a name="l00159"></a><span class="lineno"> 159</span>  0x40};</div><div class="line"><a name="l00160"></a><span class="lineno"> 160</span> </div><div class="line"><a name="l00161"></a><span class="lineno"> 161</span> <span class="comment">/* Table of CRC values for loworder byte */</span></div><div class="line"><a name="l00162"></a><span class="lineno"> 162</span> <span class="keyword">const</span> byte _auchCRCLo[] = {</div><div class="line"><a name="l00163"></a><span class="lineno"> 163</span>  0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,</div><div class="line"><a name="l00164"></a><span class="lineno"> 164</span>  0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,</div><div class="line"><a name="l00165"></a><span class="lineno"> 165</span>  0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,</div><div class="line"><a name="l00166"></a><span class="lineno"> 166</span>  0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,</div><div class="line"><a name="l00167"></a><span class="lineno"> 167</span>  0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,</div><div class="line"><a name="l00168"></a><span class="lineno"> 168</span>  0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,</div><div class="line"><a name="l00169"></a><span class="lineno"> 169</span>  0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,</div><div class="line"><a name="l00170"></a><span class="lineno"> 170</span>  0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,</div><div class="line"><a name="l00171"></a><span class="lineno"> 171</span>  0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,</div><div class="line"><a name="l00172"></a><span class="lineno"> 172</span>  0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,</div><div class="line"><a name="l00173"></a><span class="lineno"> 173</span>  0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,</div><div class="line"><a name="l00174"></a><span class="lineno"> 174</span>  0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,</div><div class="line"><a name="l00175"></a><span class="lineno"> 175</span>  0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,</div><div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,</div><div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,</div><div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,</div><div class="line"><a name="l00179"></a><span class="lineno"> 179</span>  0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,</div><div class="line"><a name="l00180"></a><span class="lineno"> 180</span>  0x40};</div><div class="line"><a name="l00181"></a><span class="lineno"> 181</span> <span class="preprocessor">#endif</span></div><div class="line"><a name="l00182"></a><span class="lineno"> 182</span> </div><div class="line"><a name="l00183"></a><span class="lineno"> 183</span> <span class="preprocessor">#endif //MODBUSSERIAL_H</span></div><div class="ttc" id="classModbusSerial_html_aefa9a7f9a910a311bfcd5110a6a8ab71"><div class="ttname"><a href="classModbusSerial.html#aefa9a7f9a910a311bfcd5110a6a8ab71">ModbusSerial::config</a></div><div class="ttdeci">bool config(HardwareSerial *port, long baud, byte parity=MB_PARITY_EVEN, int txenPin=-1)</div><div class="ttdoc">Connect an ModbusSerial object to a hardware serial port. </div></div>
|