台灣最大程式設計社群網站
∣
免費加入會員
∣登入
∣回首頁∣
您好
線上人數
1085
會員總數:
246691
討論主題:
190104
討論區
程式下載/上傳
科技新聞
專欄文章
會員中心
加值服務
外包接案
求職求才
登入
登出
歡迎您
免費
加入會員
討論區選單
新手必讀
我要提問!!
討論區
個人討論區
我的關注主題
我的黑名單
討論區EP英雄榜
專家等級說明
討論區常見問題
兌換發問點數QP
扣點申請加值服務
申請版主
開發工具
ASP
ASP.NET
C#
VB.NET
VB6
C/C++
PHP
Java
Java Script/ Node.js
AJAX / JSON / jQuery
其他語言
行動裝置開發
Android APP 開發
iOS APP/ swift 開發
Windows Phone APP
資料庫
ACCESS
MS SQL
MySQL
Oracle
其他DB
多媒體 / 網管
CSS/HTML5/Bootstarp
影像處理
office VBA / WinOS
Windows 伺服器
Linux / Unix
網管 / 資安 / VM
硬體 / 週邊 / 其他
綜合
求職求才
外包接案
心情甘苦談
網站經營 / 合夥 / 證照
建言 / 公告
文章區
專欄文章
科技新聞
Blog精華文章
討論區列表
>>
C#
>> FX5U SLMP通訊
[]
[
我要回覆
]
FX5U SLMP通訊
價值 : 100 QP
點閱數:1784 回應數:0
樓主
Switchport
0
1
18
0
發送站內信
各位好
我目前在測試C#對FX5U SLMP通訊
但是我目前遇到我對FX5U下命令時需要下五次以上才會有反應
不過這個問題軟體開啟的第一次是不會的
想請問各為我的程式部分是否有哪裡不妥,在麻煩各位了!!
我主要為兩個Function一個為建立連接另一個為對PLC下命令
public int ConnToQcpu(string ip) { try { IPAddress ipaddr = IPAddress.Parse(ip); tcpClient.Connect(ip, port); tcpClient.NoDelay = true; tcpClient.SendBufferSize = 8192; netStream = tcpClient.GetStream(); br = new BinaryReader(netStream); bw = new BinaryWriter(netStream); return 0; } catch (Exception ex) { return -1; } } public int QWord(string method, string name, uint address, int value, ref int feedback) { retrytimes = 0; int[] bytetoword = { 0, 0 }; byte[] commands = { }; byte[] rlength = { 0x0C, 0x00, 0x00, 0x00 }; byte[] wlength = { 0x0E, 0x00, 0x00, 0x00 }; byte[] read = { 0x01, 0x04, 0x00, 0x00 }; byte[] write = { 0x01, 0x14, 0x00, 0x00 }; byte[] Address = convertAddress(address); byte[] numof = { 0x01, 0x00 }; byte[] wvalue = convertWvalue(value); byte[] wname = { 0x00 }; switch (name) { case "D": wname[0] = 0xA8; break; case "W": wname[0] = 0xB4; break; } if (method == "W") { label: if (retrytimes == 8) { return -9999; } commands = head.Concat(wlength).ToArray(); commands = commands.Concat(write).ToArray(); commands = commands.Concat(Address).ToArray(); commands = commands.Concat(wname).ToArray(); commands = commands.Concat(numof).ToArray(); commands = commands.Concat(wvalue).ToArray(); Thread.Sleep(20); if (netStream.CanWrite) { try { bw.Write(commands); bw.Flush(); } catch (Exception) { return -100; } } Thread.Sleep(1000); if (netStream.CanRead) { if (netStream.DataAvailable) { byte[] bytes = new byte[tcpClient.ReceiveBufferSize]; if (bytes.Length == 0) { retrytimes++; goto label; } int numBytesRead = br.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize); byte[] bytesRead = new byte[numBytesRead]; Array.Copy(bytes, bytesRead, numBytesRead); if (BitConverter.ToString(bytesRead) != BitConverter.ToString(writeresponse)) { return -1; } feedback = 0; //正常寫入回饋0 return 0; } else if (!netStream.DataAvailable) { Thread.Sleep(100); retrytimes++; goto label; } } } }
搜尋相關Tags的文章:
[ networkstream ] ,
[ plc ] ,
本篇文章發表於2018-03-27 22:19
目前尚無任何回覆
回覆
如要回應,請先
登入
.
|
網站導覽
|
網站介紹
|
4P點數說明
|
電子報
|
小舖活動
|
大事紀
|
廣告刊登
|
常見問題
|
聯絡我們
|
版權所有 ©copyright 2000 All Rights Reserved