Mobile GPS ReaderWord格式.docx
- 文档编号:19935152
- 上传时间:2023-01-12
- 格式:DOCX
- 页数:45
- 大小:33.17KB
Mobile GPS ReaderWord格式.docx
《Mobile GPS ReaderWord格式.docx》由会员分享,可在线阅读,更多相关《Mobile GPS ReaderWord格式.docx(45页珍藏版)》请在冰豆网上搜索。
usingSystem.Windows.Forms;
usingSystem.Runtime.InteropServices;
usingSystem.Text;
usingSystem.Collections;
usingSystem.Threading;
usingSystem.IO;
namespaceGPSExample.Util
{
///<
summary>
///ClassthatmanagestheGPSreadingprocess.
///
///Togetstartedusingtheclassdothefollowing
///1)ConstructGPSReaderpassingtheportnameandbaudrateoftheGPSdevice
///C#:
GPSReadergps=newGPSReader("
COM4:
"
4800);
///VB:
DimWithEventsgpsAsNewGPSReader("
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.
/summary>
publicclassGPSReader:
Control
{
//*************************************************************
//Constructors
///<
///Defaultconstructor
///Ataminimum,willneedtosetthePortNameandBaudRatepropertiesbeforecallingStartRead
publicGPSReader()
{
}
///Constructor-AcceptsCOMMportname(COMx:
)
///WillneedtosettheBaudRatepropertiesbeforecallingStartRead
paramname="
portName"
>
<
/param>
publicGPSReader(stringportName)
:
this()
_portName=portName;
)andBaudRate
///IfdefaultCOMMportsettings(NoParity,8bits/byteandOneStopBit)areacceptable,
///cancallStartReadwithoutsettinganyoftheconfigurationproperties
baudRate"
publicGPSReader(stringportName,intbaudRate)
this(portName)
_baudRate=baudRate;
///Constructor-verbose
///ProvidesfullcontroloverallCOMMportsettings
parity"
byteSize"
stopBits"
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("
GPSReader>
MustsetBaudRate&
PortNamebeforeopeningtheport"
);
Cursor.Current=Cursors.WaitCursor;
_readData=true;
_gpsReadThread=newThread(newThreadStart(this.GPSReadLoop));
_gpsReadThread.Start();
Cursor.Current=Cursors.Default;
///TerminateGPSReading
///Sets_readDatatofalsewhichexitstheunderlyingreadloop
///AlsoclosestheCOMMportwhichabortsanypendingCOMMportoperations
publicvoidStopRead()
_readData=false;
Thread.Sleep(500);
//Givethreadtimetofinishanypendingwork
ClosePort();
//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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Mobile GPS Reader