生活随笔
收集整理的這篇文章主要介紹了
C#开发纽曼来电小秘书总结(指南)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近幫別人開發紐曼來電小秘書,小踩了幾個坑,在此與大家分享,給大家排排雷
坑1. 文檔不夠詳細,函數不全,更沒有介入流程介紹,總之就是一個大坑。
EnableCard()CheckLine(Line:Word)...
坑2. 沒有注明dll使用方式,是直接引用還是DllImport?
1.直接引用的話,vs會直接報錯,說明此方法錯誤
2.DllImport申明方法,使用正常
附P/Invoke代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;namespace Secretary
{class Secretary{[DllImport("usbms.dll", EntryPoint = "LoadDRV")]public static extern int LoadDRV(); [DllImport("usbms.dll", EntryPoint = "IsRing")]public static extern bool IsRing(int Line);[DllImport("usbms.dll", EntryPoint = "ReadUsbState")]public static extern bool ReadUsbState(int line); [DllImport("usbms.dll", EntryPoint = "IsOffHook")]public static extern bool IsOffHook(UInt16 Line);[DllImport("usbms.dll", EntryPoint = "GetDtmfCode")]public static extern int GetDtmfCode(UInt16 Line);[DllImport("usbms.dll", EntryPoint = "GetCallerIDStr")]public static extern UInt16 GetCallerIDStr(UInt16 Line, StringBuilder IDStr);[DllImport("usbms.dll", EntryPoint = "ReadSerialNo")]public static extern int ReadSerialNo(UInt16 line, StringBuilder serialNo);[DllImport("usbms.dll", EntryPoint = "GetRingNum")]public static extern int GetRingNum(int line);[DllImport("usbms.dll", EntryPoint = "SetPCMode")]public static extern bool SetPCMode();[DllImport("usbms.dll", EntryPoint = "CheckLine")]public static extern bool CheckLine(UInt16 line);[DllImport("usbms.dll", EntryPoint = "InitRingNum")]public static extern void InitRingNum(int line);[DllImport("usbms.dll", EntryPoint = "EnableCard")]public static extern int EnableCard();}
}
坑三. 沒有C#版本DEMO,只有Delphi版本DEMO
于是苦逼的我,一遍顯示器開著Delphi版本的DEMO,一遍手寫C#程序,強調各位一定要把Delphi的demo邏輯理清楚,也可以按照完全模仿。
附我整理的Delphi流程(c#基本實現此流程基本的功能均能ok)
1.Init
//record對應c#中的class
type TLines = record
Status:integer;
Timer:integer;
OldRingCounter:integer;
IsLine:Boolean;
IsKeyUpOld:Boolean;
end;
//對象數組
Lines:array[0..2] of Tlines;
//對應c#中stringbuilder
Serial:array[0..10] of char;
const STATUS_FREE = 0 ;
const STATUS_RING = 1 ;
const STATUS_WAIT_FSK = 2 ;
const STATUS_WAIT_RING_END = 3 ;const STATUS_HANGUP = 4 ;
const STATUS_WAIT_HANGUP_END = 5 ;
const STATUS_RECORD = 6 ;
const STATUS_PLAY = 7 ;
const STATUS_SEND = 8 ;Lines[0].Status := STATUS_FREE ;
Lines[1].Status := STATUS_FREE ;
Lines[2].Status := STATUS_FREE ;usbcount := LoadDRV;
if usbcount > 0 then
beginstr:='加載USB驅動設備成功!';if EnableCard =1 thenbeginstr:='啟動USB設備成功!';ReadSerialNo(0,Serial);str := '序列號=';for i:=0 to 4 do beginstr:= str+','+inttostr(ord(Serial[i]));end;//注意定時器啟動了Timer1.Enabled := true;DeviceState := false;end;SetPCMode();
end else
beginstr:='加載USB驅動設備失敗!';
end;
2.Timer
2.1 GetTrunkState(0)
//line = 0
//<> -> != =->== =:->=
if Lines[line].IsLine <> CheckLine(line) thenbeginif Lines[line].IsLine thenbeginstr := '外線斷開'end elsebeginstr := '外線接通'end;Memo1.Lines.Add(str);Lines[line].IsLine := CheckLine(line);end;//對應c# switch .. case
case Lines[line].Status ofSTATUS_FREE:beginif IsRing(line) thenbeginLines[line].OldRingCounter := GetRingNum(line);Memo1.Lines.Add('外線振鈴'+inttostr(Lines[line].OldRingCounter));Lines[line].Status := STATUS_RING ;end;if IsOffHook(line) thenbeginMemo1.Lines.Add('外線摘機');Lines[line].Status := STATUS_HANGUP ;end;end;STATUS_RING:beginif IsRing(line) thenbeginend elsebeginLines[line].Status := STATUS_WAIT_FSK;Lines[line].Timer := 0;end;end;STATUS_WAIT_FSK:beginsetlength(str,200);if (GetCallerIDStr(line,pchar(str))=3) thenbeginstr :='外線來電='+ string((str));Memo1.Lines.Add(str);end;inc(Lines[line].Timer);if ( GetRingNum(line) > 1) or ( Lines[line].Timer > 50 ) thenbeginLines[line].Status := STATUS_WAIT_RING_END ;Lines[line].Timer := 0;end;if Lines[line].OldRingCounter <> GetRingNum(line) thenbeginLines[line].OldRingCounter := GetRingNum(line);Memo1.Lines.Add('外線振鈴'+inttostr(Lines[line].OldRingCounter));end;end;STATUS_WAIT_RING_END:beginif IsRing(line) thenbeginif Lines[line].OldRingCounter <> GetRingNum(line) thenbeginLines[line].OldRingCounter := GetRingNum(line);Memo1.Lines.Add('外線振鈴'+inttostr(Lines[line].OldRingCounter));end;Lines[line].Timer := 0;end else if Lines[line].Timer > 50 thenbeginMemo1.Lines.Add('外線空閑');Lines[line].Status := STATUS_FREE;Lines[line].Timer := 0;InitRingNum(line);end;inc(Lines[line].Timer);end;STATUS_HANGUP:beginif not IsOffHook(line) thenbeginMemo1.Lines.Add('外線掛機');Lines[line].Status := STATUS_FREE ;end;end;STATUS_RECORD:beginif CheckRecordEnd(line) then beginMemo1.Lines.Add(datetimetostr(now)+'錄音結束');Lines[line].Status := STATUS_FREE ;end;end;STATUS_PLAY:beginif CheckPlayEnd(line) then beginMemo1.Lines.Add(datetimetostr(now)+'放音結束'+inttostr(line));Lines[line].Status := STATUS_FREE ;end;end;STATUS_SEND:beginif CheckSendEnd(line) then beginMemo1.Lines.Add(datetimetostr(now)+'撥號結束'+inttostr(line));Lines[line].Status := STATUS_FREE ; end;end;end;
2.2 GetUserState(1)
//line = 1
if DeviceState <> ReadUsbState(line) thenbeginif DeviceState thenstr := datetimetostr(now)+':設備移除'elsestr := datetimetostr(now)+':設備接入';DeviceState := ReadUsbState(line);Memo1.Lines.Add(str);end;
實現效果圖
附:Delphi與c#類型轉換對照表
附:使用C#開發紐曼USB來電小秘書客戶端小結
至此小秘書的c#版本demo開發基本完成,其功能自行添加修改。如需完整代碼,請訪問我的博客
聲明:原創聲明,版權聲明。
轉載于:https://www.cnblogs.com/wlei24/p/5736404.html
總結
以上是生活随笔為你收集整理的C#开发纽曼来电小秘书总结(指南)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。