生活随笔
收集整理的這篇文章主要介紹了
C#开发纽曼来电小秘书总结(指南)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近幫別人開發(fā)紐曼來電小秘書,小踩了幾個(gè)坑,在此與大家分享,給大家排排雷
坑1. 文檔不夠詳細(xì),函數(shù)不全,更沒有介入流程介紹,總之就是一個(gè)大坑。
EnableCard()CheckLine(Line:Word)...
坑2. 沒有注明dll使用方式,是直接引用還是DllImport?
1.直接引用的話,vs會(huì)直接報(bào)錯(cuò),說明此方法錯(cuò)誤
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#程序,強(qiáng)調(diào)各位一定要把Delphi的demo邏輯理清楚,也可以按照完全模仿。
附我整理的Delphi流程(c#基本實(shí)現(xiàn)此流程基本的功能均能ok)
1.Init
//record對(duì)應(yīng)c#中的class
type TLines = record
Status:integer;
Timer:integer;
OldRingCounter:integer;
IsLine:Boolean;
IsKeyUpOld:Boolean;
end;
//對(duì)象數(shù)組
Lines:array[0..2] of Tlines;
//對(duì)應(yīng)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驅(qū)動(dòng)設(shè)備成功!';if EnableCard =1 thenbeginstr:='啟動(dòng)USB設(shè)備成功!';ReadSerialNo(0,Serial);str := '序列號(hào)=';for i:=0 to 4 do beginstr:= str+','+inttostr(ord(Serial[i]));end;//注意定時(shí)器啟動(dòng)了Timer1.Enabled := true;DeviceState := false;end;SetPCMode();
end else
beginstr:='加載USB驅(qū)動(dòng)設(shè)備失敗!';
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;//對(duì)應(yīng)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('外線摘機(jī)');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('外線掛機(jī)');Lines[line].Status := STATUS_FREE ;end;end;STATUS_RECORD:beginif CheckRecordEnd(line) then beginMemo1.Lines.Add(datetimetostr(now)+'錄音結(jié)束');Lines[line].Status := STATUS_FREE ;end;end;STATUS_PLAY:beginif CheckPlayEnd(line) then beginMemo1.Lines.Add(datetimetostr(now)+'放音結(jié)束'+inttostr(line));Lines[line].Status := STATUS_FREE ;end;end;STATUS_SEND:beginif CheckSendEnd(line) then beginMemo1.Lines.Add(datetimetostr(now)+'撥號(hào)結(jié)束'+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)+':設(shè)備移除'elsestr := datetimetostr(now)+':設(shè)備接入';DeviceState := ReadUsbState(line);Memo1.Lines.Add(str);end;
實(shí)現(xiàn)效果圖
附:Delphi與c#類型轉(zhuǎn)換對(duì)照表
附:使用C#開發(fā)紐曼USB來電小秘書客戶端小結(jié)
至此小秘書的c#版本demo開發(fā)基本完成,其功能自行添加修改。如需完整代碼,請(qǐng)?jiān)L問我的博客
聲明:原創(chuàng)聲明,版權(quán)聲明。
轉(zhuǎn)載于:https://www.cnblogs.com/wlei24/p/5736404.html
總結(jié)
以上是生活随笔為你收集整理的C#开发纽曼来电小秘书总结(指南)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。