Mobile GPS Reader.docx
《Mobile GPS Reader.docx》由会员分享,可在线阅读,更多相关《Mobile GPS Reader.docx(45页珍藏版)》请在冰豆网上搜索。
![Mobile GPS Reader.docx](https://file1.bdocx.com/fileroot1/2023-1/10/9851e437-24cb-4f3c-bbbf-c423b1ce4556/9851e437-24cb-4f3c-bbbf-c423b1ce45561.gif)
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