CentOS5.4 Linux下Modbus TCP IP協議C++的一個小例子

來源:互聯網
上載者:User

///////////////////////////////////////////////////////////////////////////<br />/*<br />ReadCoils函數:<br />讀繼電器的狀態 , 成功返回ture。<br />send 資料長度=12<br />recv 資料長度=10<br />unsigned short int addr 讀繼電器的起始地址(繼電器編號-1) 。<br />注意:1~8個繼電器,起始地址從0開始<br />unsigned short int len 讀取繼電器的個數,最大8.<br />debug_state 如果為1 列印出調式資訊<br />ubool *paraDO 讀出的繼電器狀態<br />SOCKET s_bnc,fd_set fds,timeval tv 是輔助的網路介面參數<br />*/<br />///////////////////////////////////////////////////////////////////////////<br />inline bool ReadCoils(unsigned short int addr, byte & coilstate, SOCKET_INT s_bnc)<br />{<br />cmd_len = 12;<br />byte cmd_code = 1; //命令代碼:01,讀繼電器<br />byte *cmd_string ;<br />byte *echo_string ;<br />cmd_string = new byte[cmd_len] ;<br />echo_string = new byte[cmd_len] ;<br />cmd_string[ 0] = 0 ; // modbus 協議規定標示<br />cmd_string[ 1] = 0 ;<br />cmd_string[ 2] = 0 ;<br />cmd_string[ 3] = 0 ;<br />cmd_string[ 4] = 0 ; // length 高位部分 命令的後續位元組數量<br />cmd_string[ 5] = 6 ; // length 低位部分 命令的後續位元組數量<br />cmd_string[ 6] = 0 ; // Unit Identifier :identification of a remote slave connected on a serial line or on other buses.<br />cmd_string[ 7] = cmd_code ;<br />cmd_string[ 8] = (unsigned char) (addr >> 8);<br />cmd_string[ 9] = (unsigned char) addr;<br />cmd_string[10] = 0; //(unsigned char) (len >> 8);<br />cmd_string[11] = 8; //(unsigned char) len ;<br />int iret;<br />fd_set fds;<br />struct timeval tv;<br />tv.tv_sec = SLEEP ; //逾時等待時<br />tv.tv_usec = 0;<br />FD_ZERO(&fds);<br />FD_SET(s_bnc, &fds);<br />/* wait for permission to send(等待是否可寫write)*/<br />iret = select(s_bnc +1, NULL, &fds, NULL, &tv); /*write*/<br />if (iret=0)<br />{<br /> if(debug_state) printf("Timer over!n");<br /> close(s_bnc);<br /> return false ;<br />}<br />else if (iret<0)<br />{<br />if(debug_state) printf("select is err! n");<br />close(s_bnc);<br />return false ;<br />}<br />/*send 寫入資料 */<br />iret = send(s_bnc, (char *)cmd_string, cmd_len , 0);<br />if (iret < cmd_len)<br />{<br />if(debug_state) printf("failed to send chars:%dn",iret);<br />close(s_bnc);<br />return false;<br />}<br />/*wait for response(等待是否可讀read)*/<br />FD_SET(s_bnc, &fds);<br />iret = select(s_bnc +1 , &fds, NULL, NULL, &tv);<br />if (iret=0)<br />{<br />if(debug_state) printf("Timer over!n");<br />close(s_bnc);<br />return false ;<br />}<br />else if (iret<0)<br />{<br />if(debug_state) printf("select is err! n");<br />close(s_bnc);<br />return false ;<br />}<br />/*讀出資料 */<br />iret = recv(s_bnc, (char *)echo_string, 12 , 0);<br />if (iret<=0)<br />{<br />if(debug_state) printf("接受資料recv語句出錯n");<br />}<br />if ( echo_string[7] == cmd_code) /*7位置是命令*/<br />{<br />/*if(debug_state) printf("n讀資料成功了n");*/<br />}<br />else if ( echo_string[7] == (0x80 | cmd_code))<br />{<br />if(debug_state) printf("裝置主動報告:操作出錯n");<br />return false;<br />}<br />else<br />{<br />if(debug_state) printf("出錯n");<br />return false;<br />}<br />//int nDOFlag = echo_string[9] |echo_string[10] << 8); //9位置10位置是繼電器的狀態<br />//unsigned char nDOFlag = echo_string[9]; //因為E3018隻有8個繼電器,故沒有10位置<br />coilstate = echo_string[9];<br />delete []cmd_string;<br />delete []echo_string;<br />return true;<br />}<br />

全部程式正在完善中。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.