2016年12月4日 星期日

Choosing Between Class and Struct


One of the basic design decisions every framework designer faces is whether to design a type as a class (a reference type) or as a struct (a value type). Good understanding of the differences in the behavior of reference types and value types is crucial in making this choice.

The first difference between reference types and value types we will consider is that reference types are allocated on the heap and garbage-collected, whereas value types are allocated either on the stack or inline in containing types and deallocated when the stack unwinds or when their containing type gets deallocated. Therefore, allocations and deallocations of value types are in general cheaper than allocations and deallocations of reference types.
簡單來說struct的allocate跟deallocate比Class來的便宜Cheaper. 指記憶體的消耗

Next, arrays of reference types are allocated out-of-line, meaning the array elements are just references to instances of the reference type residing on the heap. Value type arrays are allocated inline, meaning that the array elements are the actual instances of the value type. Therefore, allocations and deallocations of value type arrays are much cheaper than allocations and deallocations of reference type arrays. In addition, in a majority of cases value type arrays exhibit much better locality of reference.
The next difference is related to memory usage. Value types get boxed when cast to a reference type or one of the interfaces they implement. They get unboxed when cast back to the value type. Because boxes are objects that are allocated on the heap and are garbage-collected, too much boxing and unboxing can have a negative impact on the heap, the garbage collector, and ultimately the performance of the application. In contrast, no such boxing occurs as reference types are cast.
Next, reference type assignments copy the reference, whereas value type assignments copy the entire value. Therefore, assignments of large reference types are cheaper than assignments of large value types.
因為Struct有box unbox,所以如果是很龐大的Class會比較便宜.

Finally, reference types are passed by reference, whereas value types are passed by value. Changes to an instance of a reference type affect all references pointing to the instance. Value type instances are copied when they are passed by value. When an instance of a value type is changed, it of course does not affect any of its copies. Because the copies are not created explicitly by the user but are implicitly created when arguments are passed or return values are returned, value types that can be changed can be confusing to many users. Therefore, value types should be immutable.
最後, refernce type與value type的不同就是 一個pass by ref ,另一個pass by value.
ref type的實際值改變時所有的ref都跟著變.  value type都是獨立的instances,改變值時其他並不會變.


As a rule of thumb, the majority of types in a framework should be classes. There are, however, some situations in which the characteristics of a value type make it more appropriate to use structs.
所以選擇class or struct要視情況而定

✓ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.
如果你要用的type很小而且短命,或是崁入別的物件內 就用struct.

X AVOID defining a struct unless the type has all of the following characteristics:
避免使用struct ,除非以下條件都達到
It logically represents a single value, similar to primitive types (int, double, etc.).
用來表示single value,就像是int double之類的.
It has an instance size under 16 bytes.
實作的size小於16 bytes
It is immutable.
固定的值,不變動的
It will not have to be boxed frequently.
不需要常常封裝
In all other cases, you should define your types as classes.
如在其他狀況下,你就應該用class而非struct

Portions © 2005, 2009 Microsoft Corporation. All rights reserved.
Reprinted by permission of Pearson Education, Inc. from Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition by Krzysztof Cwalina and Brad Abrams, published Oct 22, 2008 by Addison-Wesley Professional as part of the Microsoft Windows Development Series.

2016年12月2日 星期五

SLMP通訊一:PLC的設定

首先你先要有顆PLC能支援Ethernet(廢話), 本系列都是以FX5U為範例

因為使用FX5U所以還需要安裝GX Works 3

在你的PLC專案內,開啟左邊的"參數" -> FX5UCPU -> 模組參數 -> 乙太網路端口  視窗
在"對象設備連接配置設置" 點選 <詳細設置> ,會開啟下圖的視窗
並新增SLMP連接設備加入列表中 ,記得選取"TCP"協議 (這是我們要用的通訊方式) ,然後設定Port 9000, (這是我的設定) ,如圖內No.2 項目.
其他的項目可以不用管,這樣SLMP的設定就完成了! 輕鬆簡單

存檔後,記得要將剛剛改好了參數寫入PLC,  寫完之後PLC還要作 Reset(切記切記!!)
這樣你的PLC算大功告成!

另外一提的是GX Works 3有乙太網路診斷的功能,可以監控PLC的連線狀況,非常方便

以上就是PLC的SLMP設定啦!!收工!

PLC Ethernet SLMP通訊格式 C#

前言.
    開始講解前先說明這系列(SLMP)的說明,主要是紀錄自己在寫protocol時碰到的問題或想法.
人老了很容易忘記事情,只好記錄下來了.
如果你對c#是完全陌生的話,此系列並不會講解太詳細(抱歉,人很懶).
當然如果您發現有錯誤的地方,也非常歡迎糾正喔!
以上是廢話.

這系列大致會分成幾篇
1.PLC的設定方法
2.TCP的編寫
3.SLMP的編寫
4.測試

使用的PLC是:Mitsubishi FX5U
通訊格式是:SLMP (廢話)

因為手邊目前只有這顆CPU有支援Ethernet ,所以只能測試一種CPU,有點可惜
以後有機會再測試Q系列的


2016年11月30日 星期三

PLC SLMP Protocol的用法 (一)



在Receive loop的動作於一般TCP server的動作不同
一般server都會有ack的回應封包,所以可以判定資料封包是否有收完全

SLMP沒有這動作
PLC單純只會回應client端send的封包,資料封包丟完後無ACK的
簡單講也就是non-handshaking 的server啦

以下是code

private void OnReceive(System.IAsyncResult ar)
        {
            try
            {
                // Read data from the remote device.
                int bytesRead = tcpAsyn.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    asb.Append(Encoding.ASCII.GetString(tcpAsyClBuffer, 0, bytesRead));

                    // Get the rest of the data.
                    tcpAsyn.BeginReceive(tcpAsyClBuffer, 0,tcpAsyClBuffer.Length , 0,
                        new AsyncCallback(OnReceive), tcpAsyn);
                }
                else
                {
                    // All the data has arrived; put it in response.
                    if (asb.Length > 1)
                    {
                        response = asb.ToString();
                    }
                    //add do_packet
                    do_packet();
                    // Signal that all bytes have been received.
                    receiveDone.Set();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                //Console.ReadKey(true);
            }
        }

上面的一般tcp server的用法
這樣是無法正常運作的喔

2016年4月15日 星期五

Remote Debug

首先要安裝軟體在想要debug的電腦上. 

軟體會放在在debug端,建一個使用者MyName,當然權限越高越好。

 執行Remote Debugging Monitor,打開Toos\Option,可以發現Server Name,For example MyName@OtherPC。 

會到visual studio,開你的C++ Solution,打開Project\Property\Linker,Output File設為"\\OtherPC\c$\Test.exe" 

5:在C++ Solution,打開Project\Property\Debugging 你應該可以看到Debugger to launch的預設值應該是 "Local Windows Debugger",把它改為"Remote Window Debugger"。

 5.1:在Remote Command設定為"\\OtherPC\C$\Test\Test.exe" 

5.2:在Remote Server Name設定為"MyName@OtherPC" 如果一切順利,你應該可以開始Remote Debugging。

 P.S. 如果發生以下訊息"Unable to start debugging",那是因為Debugging Server無回應,請確定OtherPC的Remote Debugging Server是否已經打開. 

有時開始Remote Debugging時,Visual Studio會發出一個Dialog Box告訴你無法Remote Debugging,這時請檢查"系統管理工具\本機安全性原則工具\安全性設定\本機原則\安全性選項\網路存取:共用和安全性模式用於本機帳戶"的設定. 如果是"僅適用於來賓-本機使用者以Guest驗證"請改為"傳統-本機使用者以自身身份驗證"。

引用:http://blog.xuite.net/xujy280/twblog/127304235-Remote+Debug(For+Visual+Studio).