首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 呼叫 Prodave DLL 實戰(四):完整測試程式碼與常用函數驗證

     編輯:關於C語言

(5)測試代碼

// 測試 LoadConnection_ex6
short ConNr = 63; // First connection (0...63, max 64)
string AccessPoint = "S7ONLINE";
Prodave6_CS.Prodave6.CON_TABLE_TYPE ConTable;
int ConTableLen = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Prodave6_CS.Prodave6.CON_TABLE_TYPE));
int RetValue;
ConTable.Adr = new byte[] { 192, 168, 1, 200, 0, 0 };
ConTable.AdrType = 2; // IP
ConTable.SlotNr = 2; ConTable.RackNr = 0;
RetValue = Prodave6.LoadConnection_ex6(ConNr, AccessPoint, ConTableLen, ref ConTable);

// 測試 SetActiveConnection_ex6
UInt16 UConNr = (UInt16)ConNr;
RetValue = Prodave6.SetActiveConnection_ex6(UConNr);

// 測試 db_write_ex6
UInt16 BlkNr = 4; Prodave6.DatType DType = Prodave6.DatType.BYTE;
UInt16 StartNr = 0; UInt32 pAmount = 20; UInt32 BufLen = 20;
byte[] pWriteBuffer = new byte[20];
for (int i = 0; i < pWriteBuffer.Length; i++) pWriteBuffer[i] = (byte)(i + 1);
RetValue = Prodave6.db_write_ex6(BlkNr, DType, StartNr, ref pAmount, BufLen, pWriteBuffer);

// 測試 db_read_ex6
byte[] pReadBuffer = new byte[20]; UInt32 pDatLen = 0;
RetValue = Prodave6.db_read_ex6(BlkNr, DType, StartNr, ref pAmount, BufLen, pReadBuffer, ref pDatLen);

// 測試 field_write_ex6 (DB區)
Prodave6.FieldType FType = Prodave6.FieldType.D;
for (int i = 0; i < pWriteBuffer.Length; i++) pWriteBuffer[i] = (byte)i;
RetValue = Prodave6.field_write_ex6(FType, BlkNr, StartNr, pAmount, BufLen, pWriteBuffer);

// 測試 field_read_ex6 (DB區)
byte[] pReadBuffer2 = new byte[20];
RetValue = Prodave6.field_read_ex6(FType, BlkNr, StartNr, pAmount, BufLen, pReadBuffer2, ref pDatLen);

// 測試 field_write_ex6 (M區)
Prodave6.FieldType FTypeM = Prodave6.FieldType.M;
byte[] pWriteBufferM = { 2 };
RetValue = Prodave6.field_write_ex6(FTypeM, 0, 100, 1, 1, pWriteBufferM);

// 測試 field_read_ex6 (M區)
byte[] pReadBufferM2 = new byte[1];
RetValue = Prodave6.field_read_ex6(FTypeM, 0, 100, 1, 1, pReadBufferM2, ref pDatLen);

// 測試 mb_setbit_ex6
UInt16 MbNr = 100; UInt16 BitNr = 0; byte Value = 1;
RetValue = Prodave6.mb_setbit_ex6(MbNr, BitNr, Value);

// 測試 mb_bittest_ex6
int pValue = 0;
RetValue = Prodave6.mb_bittest_ex6(MbNr, BitNr, ref pValue);

// 測試 GetLoadedConnections_ex6
BufLen = 64; int[] pBufferI = new int[64];
Prodave6.GetLoadedConnections_ex6(BufLen, pBufferI);

// 測試 UnloadConnection_ex6
RetValue = Prodave6.UnloadConnection_ex6(UConNr);

// 測試 GetErrorMessage_ex6
int ErrorNr = 0x7040; StringBuilder Buffer = new StringBuilder(300);
BufLen = (UInt32)Buffer.Capacity;
RetValue = Prodave6.GetErrorMessage_ex6(ErrorNr, BufLen, Buffer);

// 測試浮點數轉換
float IEee = 1.2F; UInt32 gp = 0; float pIEee = 0;
RetValue = Prodave6.float_2_gp_ex6(IEee, ref gp);
RetValue = Prodave6.gp_2_float_ex6(gp, ref pIEee);

// 測試 testbit_ex6
for (int b = 0; b < 8; b++) RetValue = Prodave6.testbit_ex6(7, b);

// 測試 byte_2_bool_ex6
int[] boolValue = new int[8];
Prodave6.byte_2_bool_ex6(255, boolValue);

// 測試 bool_2_byte_ex6
byte byteValue = Prodave6.bool_2_byte_ex6(boolValue);

// 測試 kf_2_integer_ex6 / kf_2_long_ex6
UInt16 u16 = 25600, u16_; u16_ = Prodave6.kf_2_integer_ex6(u16);
UInt32 u32 = 1677721600, u32_; u32_ = Prodave6.kf_2_long_ex6(u32);

// 測試 swab_buffer_ex6
byte[] pBuffer = new byte[11]; UInt32 Amount = (UInt32)pBuffer.Length;
for (int i = 0; i < Amount; i++) pBuffer[i] = (byte)(i + 1);
Prodave6.swab_buffer_ex6(pBuffer, 6);

// 測試 copy_buffer_ex6
byte[] pSourceBuffer = { 1,2,3,4,5,6,7,8,9,10 };
byte[] pTargetBuffer = new byte[10];
Prodave6.copy_buffer_ex6(pTargetBuffer, pSourceBuffer, 8);

// 測試 BCD 轉換
UInt16[] pwValues = { 10,20,30,40,50,60,70,80,90,100 };
Prodave6.ushort_2_bcd_ex6(pwValues, 10, 0, 0);
Prodave6.bcd_2_ushort_ex6(pwValues, 10, 0, 0);
UInt32[] pdwValues = { 10,20,30,40,50,60,70,80,90,100 };
Prodave6.ulong_2_bcd_ex6(pdwValues, 8, 0, 0);
Prodave6.bcd_2_ulong_ex6(pdwValues, 8, 0, 0);

(6)說明

未轉換所有函數,僅挑選常用或可能用到的進行轉換。完整程式包可至 CSDN 下載,附英文版 Prodave PDF 說明書。僅需電子書請看 這裡。開發環境 VS2008 SP1,控制台程式無輸出,請自行斷點追蹤。程式經測試無誤,除非您改動代碼。請在 PLC 位址開放必要區域供測試。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved