Mobile GPS Reader.docx

上传人:b****6 文档编号:6913648 上传时间:2023-01-12 格式:DOCX 页数:45 大小:33.17KB
下载 相关 举报
Mobile GPS Reader.docx_第1页
第1页 / 共45页
Mobile GPS Reader.docx_第2页
第2页 / 共45页
Mobile GPS Reader.docx_第3页
第3页 / 共45页
Mobile GPS Reader.docx_第4页
第4页 / 共45页
Mobile GPS Reader.docx_第5页
第5页 / 共45页
点击查看更多>>
下载资源
资源描述

Mobile GPS Reader.docx

《Mobile GPS Reader.docx》由会员分享,可在线阅读,更多相关《Mobile GPS Reader.docx(45页珍藏版)》请在冰豆网上搜索。

Mobile GPS Reader.docx

MobileGPSReader

核心类GPSReader

 

//GPSReader.cs

//

//Copyright(C)2003JWHedgehog,Inc.Allrightsreserved

//

//JWHedgehog,Inc

//

//

//Directquestionstomailto:

jimw@

//

//Thiscode,commentsandinformationareprovided"ASIS"with

//nowarrentyofanykind,eitherexpressedorimplied,including

//butnotlimitedtotheimpliedwarrantiesofmerchentabilityand/or

//fitnessforaparticularpurpose

//---------------------------------------------------------------------

usingSystem;

usingSystem.Data;

usingSystem.Windows.Forms;

usingSystem.Runtime.InteropServices;

usingSystem.Text;

usingSystem.Collections;

usingSystem.Threading;

usingSystem.IO;

namespaceGPSExample.Util

{

///

///ClassthatmanagestheGPSreadingprocess.

///

///Togetstartedusingtheclassdothefollowing

///1)ConstructGPSReaderpassingtheportnameandbaudrateoftheGPSdevice

///C#:

GPSReadergps=newGPSReader("COM4:

",4800);

///VB:

DimWithEventsgpsAsNewGPSReader("COM4:

",4800)

///2)HandletheOnGPSMessageevent

///ThiseventwillfireeachtimetheGPSsendsanupdate

///3)Callgps.StartRead()

///LaunchestheGPSreadingprocessonabackgroundthread

///

///UsetheStartReadandStopReadmethodstocontroltheGPSreadingprocess.Beforecalling

///StartReadyoumustprovideatleasttheportnameintheform"COMx"(xistheportnumber)and

///thebaudrate.YoucandothisusingeitheraconstructororthePortNameandBaudRateproperties.

///EachtimeaGPSmessageisreceived,theOnGPSMessageeventwillfirepassinganinstanceofthe

///GPSEventArgsclasscontainingtherawGPSsentencealongwithsomeofthevaluesalreadyparsedinto

///read-onlyfields.

///

///ThisclassdoestheactualGPSreadingworkonabackgroundthread.TheindividualOnGPSMessageevents

///areraisedinaUIthreadsafemannersonospecialhandlingisrequired.Becauseitisconsideredunsafe

///tointeractwithUIelements(TextBox,ListBox,etc.)fromathreadotherthenthethreadonwhichtheywere

///created,theGPSreaderthreadraisestheOnGPSMessageeventontheUIthread.Theseisacheivedbyderiving

///theGPSReaderclassfromControlandthenusingtheinheritedInvokemethod.Callingthis.Invokecausesthe

///eventtoberaisedonthesamethreadonwhichtheGPSReaderwascreated.SincetheGPSReaderisusually

///createdasamemberofeitheraFormoramethodonaForm,itissafetoassumethattheGPSReaderwas

///createdonthesamethreadastheFormandtheForm'sassociatedUIelements.

///

///BecausetherecansometimesbeashortdelaybetweenwhenStartRead/StopReadarecalledandwhenthe

///actionactuallyoccursonthebackgroundthread,OnGPSStartReadandOnGPSStopReadeventsareprovided.

///Eachfireswhenthebackgroundthreadactuallyperformstheaction.LiketheOnGPSMessageevent,

///theseareraisedinaUIthreadsafemanner.

///

///TosupportthebroadestnumberofGPSdevices,theclassactuallysupportstwodifferentreadmodes.

///Thepreferredreadmodeis"MesssageMode".InMessageMode,welettheCOMMportdrivermonitorthe

///GPSstreamwatchingforthearrivalofthecarriage-return(\n).OurcodeblocksuntiltheCOMMport

///drivernotifiesusofthecarriage-return,atwhichtimewethengoreadtheentireGPSsentencefrom

///theCOMMportdriver.

///Thealternativereadmodeis"CharacterMode".InCharacterModewereadthedatacharacter-by-character

///fromtheCOMMportdrivermanuallybuildingtheGPSsentenceandwatchingforthecarriage-return.This

///modewasaddedbecauseexperimentationshowedthatsomeGPSdevicesthatsimulateCOMMports(i.e.theGPS

///mightbeanexpansionpackofcompactflashcardbutappearsasaCOMMporttothedevice)donot

///supportlettingtheCOMMportdrivermonitorforthecarriage-return.

///UsingthePreferredReadModeproperty,youcansetwhichmodetheGPSReaderusesIfyouchooseMessageMode

///orAuto(thedefault)TheGPSReaderclassteststoseeifthedriversupportsMessageModeandifsouseit.

///OtherwiseitwilldowngradetoCharacterMode.TheActiveReadModepropertyindicateswhichreadmodeis

///actuallybeingused.

///ThecodethattestsforMessageModesupportisinthe"DriverSupportsMessageModemethod.Becauseitsnot

///possibletotesteveryGPSinexistencethereisnowaytobe100%surethatthistestwillalwaysworkbut

///onthedevicestestedithasbeenreliable.

///***************************************************************************************************************

///Note***********************************************************

///IfyoutryreadingfromadeviceandtheGPSReaderneverreturnsanydata,thecausemaybethatMessageMode

///supporthasbeenfalslyindicatedassupported.SettingtheGPSReaderPreferredReadModetoReadMode.Character

///shouldovercometheproblem.Theneedtodothishasneverbeenobserveredbutsinceitsnotpossibletotest

///everyGPSdevicethepossibilityalwaysexists.

///***************************************************************************************************************

///

publicclassGPSReader:

Control

{

//*************************************************************

//Constructors

//*************************************************************

///

///Defaultconstructor

///Ataminimum,willneedtosetthePortNameandBaudRatepropertiesbeforecallingStartRead

///

publicGPSReader()

{

}

///

///Constructor-AcceptsCOMMportname(COMx:

///WillneedtosettheBaudRatepropertiesbeforecallingStartRead

///

///

publicGPSReader(stringportName)

:

this()

{

_portName=portName;

}

///

///Constructor-AcceptsCOMMportname(COMx:

)andBaudRate

///IfdefaultCOMMportsettings(NoParity,8bits/byteandOneStopBit)areacceptable,

///cancallStartReadwithoutsettinganyoftheconfigurationproperties

///

///

///

publicGPSReader(stringportName,intbaudRate)

:

this(portName)

{

_baudRate=baudRate;

}

///

///Constructor-verbose

///ProvidesfullcontroloverallCOMMportsettings

///

///

///

///

///

///

publicGPSReader(stringportName,intbaudRate,ParitySettingparity,bytebyteSize,StopBitsSettingstopBits)

:

this(portName,baudRate)

{

_parity=parity;

_byteSize=byteSize;

_stopBits=stopBits;

}

//*************************************************************

//Events

//*************************************************************

///

///FireseachtimeaGPSmessageisreceived

///

publiceventGPSEventHandlerOnGPSMessage;

///

///Fireswhenthebackgroundthreadbeginsthereadprocess

///

publiceventEventHandlerOnGPSReadStart;

///

///Fireswhenthebackgroundthreadexitsthereadprocess

///

publiceventEventHandlerOnGPSReadStop;

//*************************************************************

//Start/StopReading

//*************************************************************

///

///InitiateGPSReading

///Actualreadingdoneonabackgroundthread-thismethodreturnsimmediatly

///

///ThrowsanerrorifeitherPortNameorBaudRatenotset

///

publicvoidStartRead()

{

//Verifythatweknowtheportnameandbaudrate

if(_baudRate==baudRateNotSet||_portName==portNameNotSet)

thrownewApplicationException("MustsetBaudRate&PortNamebeforeopeningtheport");

Cursor.Current=Cursors.WaitCursor;

_readData=true;

_gpsReadThread=newThread(newThreadStart(this.GPSReadLoop));

_gpsReadThread.Start();

Cursor.Current=Cursors.Default;

}

///

///TerminateGPSReading

///Sets_readDatatofalsewhichexitstheunderlyingreadloop

///AlsoclosestheCOMMportwhichabortsanypendingCOMMportoperations

///

publicvoidStopRead()

{

Cursor.Current=Cursors.WaitCursor;

_readData=false;

Thread.Sleep(500);//Givethreadtimetofinishanypendingwork

ClosePort();

Cursor.Current=Cursors.Default;

}

//*************************************************************

//PortSetupandconfiguration

//*************************************************************

///

///SetPortName(COMx:

///

publicstringPortName

{

get{return_portName;}

set{_portName=value;}

}

///

///SetBaudRate-NoDefault

///

publicintBaudRate

{

get{return_baudRate;}

set{_baudRate=value;}

}

///

///SetPortParity-defaultstoNoParity

///

publicParitySettingParity

{

get{return_parity;}

set{_parity=value;}

}

///

///SetPortStopBits-defaultstoOneStopBit

///

publicStopBitsSettingStopBits

{

get{return_stopBits;}

set{_stopBits=value;}

}

///

///SetPortByteSize(inbits)-defaultsto8

///

publicbyteByteSize

{

get{return_byteSize;}

set{_byt

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1