2012-03-14 77 views
6

我嘗試通過從PC(C#.NET)到PLC以太網模塊(Omron)的UDP發送FINS命令,但沒有收到來自PLC的任何響應,並且沒有任何線索可以開始排查。發送FINS命令從C#到PLC

PLC具有非常簡單的梯形邏輯如下: 如果DM100具有#0001值,然後在輸出101.00觸發。 (在這裏,「觸發」只是用於存儲器區域D100一個符號名,「輸出」是用於輸出101.00一個符號) enter image description here

然後寫一塊C#執行的「存儲區域寫入」 FINS命令其中命令碼爲01 02,後面跟着起始地址,要寫入的項目數量和數據。 C#代碼應將#0001的值寫入PLC的D100區域以在101.00上觸發ON。

[刪除代碼不起作用] ...

輸出101.00沒有被觸發,我也收到任何異常。 我已經確定了以下內容:

  1. 通過「工作網」,在CX-Programmer中所確認的正確配置端口,節點和地址。我也ping每個IP,以確保節點連接。
  2. UdpClient代碼是有效的,因爲我寫了一個非常簡單的成功發送和接收數據包的服務器/客戶端代碼。
  3. 梯形圖邏輯沒有問題。我將梯形圖傳送到PLC並在監視模式下通過工作在線進行測試,並手動設置D100的數值。

我懷疑在fins_cmnd數組中有錯誤,但在我的代碼中看到,我已經在每個值上註釋了儘可能詳細的內容;我不可能發現自己錯過了任何東西。我懷疑我可能不會正確解析十六進制數,但是我再也沒有例外可以指導我。

我不知道在哪裏以及如何排除故障。希望有人在這裏用FINS編程或PLC經驗可以給我一些幫助。

【答案】
感謝Porge的鏈接 - 這讓我發現了這個問題。經過一段時間後,終於可以開始工作了。請參閱下面的工作代碼。

string SERV_IP_ADDR = "192.168.250.1"; 
const int FINS_UDP_PORT = 9600; 

byte[] sendPacket = new byte[] 
{ 
    // Full UDP packet: 80 00 02 00 00 00 00 05 00 19 01 02 82 00 64 00 00 01 00 01 

    // Header 
    0x80, //0.(ICF) Display frame information: 1000 0001 
    0x00, //1.(RSV) Reserved by system: (hex)00 
    0x02, //2.(GCT) Permissible number of gateways: (hex)02 
    0x00, //3.(DNA) Destination network address: (hex)00, local network 
    0x00, //4.(DA1) Destination node address: (hex)00, local PLC unit 
    0x00, //5.(DA2) Destination unit address: (hex)00, PLC 
    0x00, //6.(SNA) Source network address: (hex)00, local network 
    0x05, //7.(SA1) Source node address: (hex)05, PC's IP is 192.168.250.5 
    0x00, //8.(SA2) Source unit address: (hex)00, PC only has one ethernet 
    0x19, //9.(SID) Service ID: just give a random number 19 

    // Command 
    0x01, //10.(MRC) Main request code: 01, memory area write 
    0x02, //11.(SRC) Sub-request code: 02, memory area write 

    // PLC Memory Area 
    0x82, //12.Memory area code (1 byte): 82(DM) 

    // Address information 
    0x00, //13.Write start address (2 bytes): D100 
    0x64, 
    0x00, //15.Bit address (1 byte): Default 0 
    0x00, //16.No. of items (2 bytes): only one address which is D100 
    0x01, 

    // Write Data 
    0x00, //18.Data to write (2 bytes): value is 1 
    0x01, 
}; 

UdpClient client = new UdpClient(); //create a UdpClient instance 

try 
{ 
    client.Send(sendPacket, sendPacket.Length, SERV_IP_ADDR, FINS_UDP_PORT); 
} 
catch (SocketException se) 
{ 
    Console.WriteLine(se.ErrorCode + ": " + se.Message); 
} 

client.Close(); 

回答

6

這些字符串都不會被解析爲十六進制。 NumberStyles.AllowHexSpecifier允許十六進制前綴「0x」,但不會將該數字解析爲十六進制,除非它存在。所以你想要(char)Int16.Parse("0x64", NumberStyles.AllowHexSpecifier)

但是,C#中的數字文字可以是十六進制的,因此您可以代之以寫出0x64而不是所有的文字。

我剛剛查看了協議的this page as a reference,並且最好直接創建消息作爲字節,而不是指定Unicode代碼點並將它們解碼爲ASCII字節。您也可以使用數組規範語法刪除了很多混亂:

var message = new byte[] 
{ 
    // header 
    0x80, //(ICF) Display frame information: 1000 0001 
    0x00, //(RSV) Reserved by system: (hex)00 
    0x02, //(GCT) Permissible number of gateways: (hex)02 
    0x00, //(DNA) Destination network address: (hex)00, local network 
    0x00, //(DA1) Destination node address: (hex)00, local PLC unit 
    0x00, //(DA2) Destination unit address: (hex)00, PLC 
    0x00, //(SNA) Source network address: (hex)00, local network 
    0x05, //(SA1) Source node address: (hex)05, PC's IP is 192.168.250.5 
    0x00, //(SA2) Source unit address: (hex)00, PC only has one ethernet 
    0x19, //(SID) Service ID: just give a random number 19 

    // command 
    0x01, //(MRC) Main request code: 01, memory area write 
    0x02, //(SRC) Sub-request code: 02, memory area write 

    // data 
    0x82, //Memory area code, 2 bytes: 82(DM) 
    0x00, //Write start address DM100 
    0x64, 
    0x00, 
    0x00, //Word write: only one address 
    0x01, 
    0x00, //Write value of 1 to address DM100 (0000 0000 0000 0001) 
    0x01, // - this value is 0xaabbccdd -> cc dd aa bb 
    0x00, 
    0x00, 
}; 

它也像有一些問題,你的數據段 - 根據鏈接的文檔的內存地址應該是4個字節,寫長度爲2個字節,每個值爲4個字節。這些與你所擁有的不匹配,所以我擴展了這一部分。

+0

內存是,是,4個字節,因此我有兩個數組項目一個0x00和0x64一起4個字節。因爲每個「部分」只需要16位。 – KMC 2012-03-14 04:41:42

+0

@KMC:我已經擴展了「數據」部分,這是基於我認爲它是從給定頁面看起來的樣子。它有幫助嗎? – porges 2012-03-14 04:50:47