Professional Documents
Culture Documents
Lập trình giao tiếp máy tính
Lập trình giao tiếp máy tính
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
1/78
Tutorialno01.02
Gin:
Nidung:
onHip,DonMinhng,HunhChuThun
picvietnam@googlegroups.com
LptrnhgiaotipmytnhquacngRS232..
MICROSOFTWORD
Tmtt:
VngiaotipgiaPCvviiukhinrtquantrongtrongccngdngiukhin,olng,..
trongthct.
DotixingiithiuchoccbnvitchngtrnhtrnPCdngMSComm.Tixinaramt
svnnhsau:
GiithiuchungiaotipRS232viukhinActiveXMicrosoftCommunication6.0(
MSComm):cchtnhchtvcchthitlpthamstiuchoiukhin.
LptrnhngdnggiaotiptrncssdngphnmmVisualC++trongbcngcVisual
Studio6.0caMicrosft:
+Thitkgiaodinchngtrnh.
+Vitmchochngtrnh.
1. iukhinMSComm
1.1.
ChungiaotipRS232
RS232lmtchungiaotipnitipdngnhdngkhngngb,ktninhiu
nhtlvi2thitb,chiudiktnilnnhtchophpmbodliul50100feet
(12.7n25.4m),tc20kBt/sikhiltc115kBt/svimtsthitbcbit.
bitcccthamscachungiaotipRS232tronghiuhnhcabnnhth
nothbnkchphichutvobiutngMyComputer,chnProperties,chnTab
HardWarechnDeviceManagerscmttreehinra.BnchnPort(Com&LPT),kch
chutphivoCommunicationPort(COM1).ChuynsangtabPortSettingsthyc
ccthamsmchngtacnthitlpbaogmtnsbus,DataBits,ParityBits,StopBits,
Handshaking,..
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
2/78
Hnh1.1:ChnMyComputer>Properties
Hnh1.2:ChnDeviceManager
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
3/78
Hnh1.3:ChnPropertiescaCommunicationPort(COM1)
Hnh1.4:ChnthPortSettingbitthams
chnhlldotisaotilichnccthamsavotrongccComboBoxnh
dichngtrnhny.
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
4/78
1.2.
CcthuctnhcaMSComm
MSCommlmtiukhinActiveXdngtrongtruynthngnitip.Cctnhcht
caiukhinnycdngthitlpgiaotipviccthitbngoiviquacng
RS232.DotixingiithiuviccbniukhinnytrongVisualBasicvcchgi
thuctnhngincaVB.TslylmcslptrnhtrnVisualC++.
1.2.1. iukhinMSCommtrongVisualBasic
TtccctnhchtnybncthtmtithvinMSDNJuly2001theongdn
nhsautabcontents:
MSDNLibraryJuly2001/VisualToolsandLanguages/VisualStudio6.0
Documentation/VisualBasicDocumentation/Reference/ControlReference/ActiveX
Control/MSCommControl
CctnhchtcaMSCommcspxptheochcnng:
Thitlpthamschocng:
+CommID:trlihandlesngnhttithitbtruynthng,ckiuLong.Tnhcht
nykhngclcthitkmchckhithihnh,thuctnhnylReadOnly.
+CommPort:dngobject.CommPort=value.ValuelchscacngComcgitrt
1>16vmcnhcgitr=1.Ccbncnphithitlpthngsnytrckhimcng.
Sclierror68(Deviceunavailable)nunhkhngmccngny.
+InBuferSize:thitlphoctrlikchthccabmnhn,tnh=byte.Mcnh
l1024byte.CcbnkhngcnhmlnvictnhInBufferCountlsbyteang
chtrongbmnhn.
+InputLen:object.InputLen[=value]thitlphoctrlisbytemilnthuctnh
Inputctrongbmnhn.McnhgitrValue=0tclthuctnhInputscht
nidungcabmnhnkhithuctnhnycgi.Nuskttrongbmnhn
khng=InputLenththuctnhInputstrliktrng.Vthbncnphichn
cchkimtraInBufferCountchcchnsktyucuctrckhidnglnh
.Input.Tnhchtnyrtlcchkhicdliumtmymdliuracnhdng
bngcckhickchthccnh.
+InputMode:object.InputMode[=value].
Value=0hay=comInputModeTextdliunhncdngvnbnkiukttheo
chunANSI.Dliunhncslmtsu.
Value=1hay=comInputModeBinarydngnhnmikiudliunhktiukhin
nhng,ktNULL,..GitrnhnctInputslmtmngkiuByte.
+NullDiscard:object.NullDiscard[=value]tnhchtnyquytnhkttrngc
ctruyntcngnbmnhnhaykhng.Nuvalue=Truektnykhngc
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
5/78
truyn.value=falsekttrngsctruyn.Kttrngcnhnghatheochun
ASCIIlkt0chr$(0).
+OutBuferSize:gingnhInBuferSize,mcnhl512.
+ParityReplace:thitlpvtrliktthaythktkhngngtrongliging
nhau.
+PortOpen:thitlpvtrlitnhtrngcacng(nghocm).
object.PortOpen[=value].value=truecngm.value=falsecngngvxaton
bdliutrongbmnhnvtruyn.CnphithitlpthuctnhCommPortngvi
tncacngtrckhimcnggiaotip.Thmvo,cnggiaotipcathitbcabn
phihtrgitrtrongthuctnhSettingththitbcabnmihotngng,cn
khngthnshotngrtdhinukhngnilnchykhngtt.ngDTRv
RTSlungilitrngthicacng.
+RthresHold:object.Rthreshold[=value]valuekiusnguyn.Thitlpskt
nhnctrckhigylnskincomEvReceive.Mcnh=0tclkhngcskin
OnCommkhinhncdliu.Thitlp=1tclskinOnCommxyrakhibtkk
tnocchuynnbmnhn.
+Settings:object.Settings[=value]thitlphoctrliccthngstnsbaud,bt
dliu,btchnl,btstop.NuValuekhngcgitrkhimsgyrali380(Invalid
propertyvalue).
+SThreshold:thitlpvvtrlisktnhnhtcchophptrongbmgi
xyraskinOnComm=comEvSend.Theomcnhgitrny=0tclkhitruyn
skhnggyraskinOnComm.Nuthitlpthngsny=1thskinOnCommxy
rakhibmtruynrng.SkinOnComm=comEvSendchxyrakhimskt
trongbmtruynnhhnhoc=Sthreshold.Nuskttrongbmnylunln
hnSthresholdthskinnykhngthxyra.
Truynnhndliu:
+CommEvent:trliphnlnskingiaotiphoccli.CommEventxyrakhic
lihockhixyraskinno.Sauylmtshngsli:
Skin
Gitr
Miutskin
comEventBreak
1001
XyrakhinhncmttnhiuBreak.
comEventFrame
1004
Lihthng.Phncngphthinramtlihthng
comEventOverrun
1006
Xyrakhicngttrn(Overrun).Mtktkhngc
ctphncngtrckhikttiptheotivdokt
nybmt.
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
6/78
comEventRxOver
1008
Xyrakhibmnhnbtrn.Khngcchchod
liutrongbmnhn.
comEventRxParity
1009
LiParity.PhncngphthinramtliParity.
comEventTxFull
1010
xyrakhibmtruynby.Bmtruynby
trongkhighidliulnvobm
comEventDCB
1011
Mtlikhngmongmunkhiangkhiphclikhi
iukhinthitb(DCBDeviceControlBlock)chocng
Mtsskin:
Skin
Gitr
Miutskin
comEvSend
Xyrakhiskttrongbmtruynnhhngitr
SthresHold.
comEvReceive
Xyrakhibmnhncsktbnggitr
RthresHold.Skinnyctoralintcchotikhibn
dngthuctnhInputlyhtdliuttrongbm
nhn.
RcomEvCTS
XyrakhicthayitrongngCTS(ClearToSend)
comEvDSR
XyrakhithayitrongngDSR(DataSetReady).S
kinnychxyrakhingDSRthayit1>0.
comEvCD
XyrakhicthayitrongngCD(CarrierDetect)
comEvRing
Phthinchung(Ring).MtsUARTkhnghtrs
kinny.
comEvEOF
Xyrakhinhncktktthcfile(kt26trong
bngmASCII)
+EOFEnable:object.EOFEnable[=value]quytnhcchnhngnuMSComm
tmthyktktthcfile.Nuvalue=truekhitmthyktktthcfilethsgylns
kincomEvEOFtrongOnCommEvent.Nuvalue=falsethskhnggylnskinny.
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
7/78
+InBufferCout:trlisktangctrongbmnhnBncthxobmnhn
bngcchtthuctnhny=0.KhngnhmvithuctnhInBufferSizeltngkch
thccabmnhn.
+Input:nhnvxodliutrongbmnhn.
NuInputModelcomInputModeTextthgitrtrvslmtxutcckiu
String,dliudngtexttrongmtbinkiuVariant.NuInputMode=
comInputModeBinaryththuctnhnystrlidliudngnhphndidngmt
mngkiubytetrongmtbinVariant.
+OutBufferCount:trliskttrongbmtruyn.
+Output:ghidliuvobmtruyn.cthtruynkiutexthockiunhphn.
NutruynbngkiutextthchomtbinVariant=kiuString,nutruynkiunhphn
thchochoOutput=variant=mtmngkiuByte.
Bttay(handshaking):
+Break:thitlphocxotnhiu.object.Break[=value]value=truehocfalse.Khi
setvalue=trueththngsBreaknysgimttnhiubreak.Tnhiubreaktrhon
victruyndliuvangtruynvotrngthibreaktikhimvalue=false.
+CDHolding:qutnhxemstruynnynubngcchtruyvnngCD(
CarrierDetect).CarrierDetectltnhiugitmodemtimytnhktnivinthng
borngnangonline.Nugitr=truethnngCDangmccao,nu=false
thngdynyangmcthp.Tnhchtnykhngctronglcthitkchctrong
khichychngtrnh.CarrierDetectcbitnhlReceiveLineSignalDetect(RLSD).
+CTSHolding:qutnhkhinobngidliubngcchtruyvntrngthing
ClearToSend(CTS).ThngthngtnhiuCTScgitmodemtimytnhktni
vinborngangqutrnhtruyndliu.ThuctnhReadonlychxuthinkhi
chychngtrnh.ngClearToSenddngtrongRTS/CTS(RequestToSend/ClearTo
Send)bttayphncng.CTSHoldingchobnmtcchttaydngClearToSend
nubncnbittrngthican.
+DSRHolding:bittrngthicangDataSetReady(DSR).TnhiuDataSet
Readytruyntmodemtimytnhnivinthngborngmodemsnsng
hotng.TnhchtnydngkhivitDataSetReady/DataTerminalReadyhandshaking
routinechomyDataTerminalEquipment(DTE)mytrangbucuidliu.
+DTREnable:tnhchtnyquytnhkhinochophpngDataTerminalReady
(DTR)trongtruynthng.TnhiuDTRgitmytnhtimodemborngmytnh
snsnglninhndliu.KhiDTREnable=truethngDataTerminalReadysetln
caokhicngm,vthpkhicngng.NuDTREnable=falsethnglunmc
thp.TrongphnlntrnghpsetngDataTerminalReadythnhthphangup
telephone.
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
8/78
+Handshaking:thitlpvtrligiaothcbttayphncng.object.Handshaking[=
value].
Ccgitrcavalue:
comNone
(Mcnh)Khngbttay
comXOnXOff
BttayXON/XOFF
comRTS
BttayRTS/CTS(RequestToSend/ClearToSend)
DngcbttayRequestToSendandXON/XOFF
comRTSXOnXOff
Handshakingchlgiaothctruynthngnitiquytnhbidliunoc
truyntcngphncngtibmnhn.Khiktcadliuticngnitip,thitb
truynthngschuynnvotrongbmnhnvchngtrnhcabncthc
chng.Nukhngcbmdliuhocchngtrnhcabncnckttrctipt
phncng,bncthmtdliubivkttphncngnrtnhanh.Giaothc
Handshakingmbodliukhngbmt,khidliuncngqunhanhththitb
truynthngschuyndliuvotrongbmnhn.
+RTSEnable:qutnhkhinochophpngRequestToSend(RTS),TnhiuRTS
tmytnhtimodemyucuctryndliu.KhiRTSEnable=truethng
RTSmccaokhicngm,tchmcthpkhicngng.VhinnhinkhiRTSEnableth
ngRTSlunmcthp.RTSdngtrongRTS/CTShardwarehandshaking.RTSEnable
chophpbndngRTSkhicnbittnhtrngcangny.
Cctnhchttrnkhngclcthitkgiaodinmchclcchychngtrnh(
dngtrongvitcode).
1.1.2. iukhinMSCommtrongVisualC++
TrnylccthamscaiukhinMSCommtrongVB.
TrongVisualC++,mimtiukhincnhnghatrongmtlpring.V
MSCommcngkhngphilngoil.
Viccthamstrnccbnhontoncthpdngrtlttcholptrnhvi
VC++bilccthamsnybncthlygitrhocthitlpthamschochngtng
ngbngcchmthnhvincalpnhGet_thuctnhhocSet_thuctnh.
Vd,bnmunthitlpdngcngCOM1chnghnthdngthuctnhCommPort
trnvchthmtintSet_numunthitlpvGet_numunlygitrny.
SauyllpMSCommlmtlpkthatlpcsCWnd,ccbncththam
kho.Ccbnchvcckiudliucaccthamsvgitrtrvcacchm.Vic
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
9/78
chuynigiacckiudliunyccbncththamkhotithvinMSDN
MicrosoftJuly2001bngcchSearch
KeyWord:chnhlcctkhochnhlcckiudliu.(nhCString,)
classCMSComm:publicCWnd
{
protected:
DECLARE_DYNCREATE(CMSComm)
public:
CLSIDconst&GetClsid()
staticCLSIDconstclsid
={0x648a5600,0x2c6e,0x101b,{0x82,0xb6,0x0,0x0,0x0,0x0,0x0,0x14}};
returnclsid;
virtualBOOLCreate(LPCTSTRlpszClassName,
LPCTSTRlpszWindowName,DWORDdwStyle,
constRECT&rect,
CWnd*pParentWnd,UINTnID,
CCreateContext*pContext=NULL)
{returnCreateControl(GetClsid(),lpszWindowName,dwStyle,rect,pParentWnd,nID);
}
BOOLCreate(LPCTSTRlpszWindowName,DWORDdwStyle,
constRECT&rect,CWnd*pParentWnd,UINTnID,
CFile*pPersist=NULL,BOOLbStorage=FALSE,
BSTRbstrLicKey=NULL)
{returnCreateControl(GetClsid(),lpszWindowName,dwStyle,rect,pParentWnd,nID,
pPersist,bStorage,bstrLicKey);}
//Attributes
public:
//Ccphpton
public:
voidSetCDHolding(BOOLbNewValue);
BOOLGetCDHolding();
voidSetCommID(longnNewValue);
longGetCommID();
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
10/78
voidSetCommPort(shortnNewValue);
shortGetCommPort();
voidSetCTSHolding(BOOLbNewValue);
BOOLGetCTSHolding();
voidSetDSRHolding(BOOLbNewValue);
BOOLGetDSRHolding();
voidSetDTREnable(BOOLbNewValue);
BOOLGetDTREnable();
voidSetHandshaking(longnNewValue);
longGetHandshaking();
voidSetInBufferSize(shortnNewValue);
shortGetInBufferSize();
voidSetInBufferCount(shortnNewValue);
shortGetInBufferCount();
voidSetBreak(BOOLbNewValue);
BOOLGetBreak();
voidSetInputLen(shortnNewValue);
shortGetInputLen();
voidSetNullDiscard(BOOLbNewValue);
BOOLGetNullDiscard();
voidSetOutBufferSize(shortnNewValue);
shortGetOutBufferSize();
voidSetOutBufferCount(shortnNewValue);
shortGetOutBufferCount();
voidSetParityReplace(LPCTSTRlpszNewValue);
CStringGetParityReplace();
voidSetPortOpen(BOOLbNewValue);
BOOLGetPortOpen();
voidSetRThreshold(shortnNewValue);
shortGetRThreshold();
voidSetRTSEnable(BOOLbNewValue);
BOOLGetRTSEnable();
voidSetSettings(LPCTSTRlpszNewValue);
CStringGetSettings();
voidSetSThreshold(shortnNewValue);
shortGetSThreshold();
voidSetOutput(constVARIANT&newValue);
VARIANTGetOutput();
voidSetInput(constVARIANT&newValue);
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
11/78
};
VARIANTGetInput();
voidSetCommEvent(shortnNewValue);
shortGetCommEvent();
voidSetEOFEnable(BOOLbNewValue);
BOOLGetEOFEnable();
voidSetInputMode(longnNewValue);
longGetInputMode();
Ccbnxemkcchm,ccphptontrncththylkiudliudnglmtham
schohmhaygitrtrvcacchmhuhtlcckiudliucbnnhBOOL,
short,..ChcbitlckiudliuVARIANT.
VARIANTlmtcutrcmdliunchalmtkiuunioncnhnghanh
sau:
bVal;
iVal;
lVal;
fltVal;
dblVal;
boolVal;
scode;
cyVal;
date;
bstrVal;
FAR* pdecVal
FAR* punkVal;
FAR* pdispVal;
FAR* parray;
FAR* pbVal;
FAR* piVal;
FAR* plVal;
FAR* pfltVal;
FAR* pdblVal;
FAR* pboolVal;
FAR* pscode;
FAR* pcyVal;
FAR* pdate;
FAR* pbstrVal;
FAR* FAR* ppunkVal;
FAR* FAR* ppdispVal;
FAR* FAR* pparray;
FAR* pvarVal;
FAR* byref;
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
12/78
char
unsigned short
unsigned long
int
unsigned int
char FAR *
unsigned short FAR *
unsigned long FAR *
int FAR *
unsigned int FAR *
cVal;
uiVal;
ulVal;
intVal;
uintVal;
pcVal;
puiVal;
pulVal;
pintVal;
puintVal;
// VT_BYREF|VT_UI4.
};
};
CcbnthyrngkiuVARIANTdnglmthamschohmSetInputvlkiu
trvcahmGetOutput.MvikiutruynkiudngTextchngtatruyndliura
cngldngxuktthchngtachuynigikiuVARIANTsangkiuktCString
nhthno?
ChuyntkiuCString>VARIANT:tidnglpColeVariant(ccbncthtra
trongMSDNdngtabIndex)ldngnggicakiucutrcVARIANT,lpnyc
hmkhitoCOleVariant(CString&strSrc);vdoncthlmthamschohm
SetInputcaMSComm.VvychngtachcnkhaibomtbinColeVariantlxong.
CStringdata_tosend=Example;
CodeVarianttemp(data_tosend);
m_mscomm1.SetInput(temp);
ChuyntkiuVARIANTsangkiuCString.Ccbnxemlinhnghacutrc
VARIANTtrnxemcbinnockiutrvkiutngthchvikiuCString(tcc
thpkiutrthnhkiuCString).
Tithycthnhphn
BSTRbstrVal;
Dotachvicpkiulxong.
VARIANTdata;
CStringm_strData=(CString)data.bstrVal;
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
13/78
1.3.
Cchthitlptiuchongdng
chongdngcthcngaydliukhibtuctrongbmnhnthccbn
nntthuctnhRthresHold=1.
Ngoiraccbncnquantmnccthams:CommPort,Settings,Rthreshold,
SthresHold,PortOpen,InputLen,InputBuffer,OutputBuffer,InBufferSize,InputMode,
OutBufferSize.
2. Lptrnh
2.1.
Mcchyucu
Chngtrnhnyrtlngin.Chngtastoramtchngtrnhcgiaodinnh
sau:
Hnh2.1:Giaodinchngtrnh
Chngtrnhcchcnngsau:
NhpkthocxuktvoEditBoxTransfer,iuchnhthamsgiaotiptrn
ccComboBox.NhnntSendgidliuracngCOM.
ngthivinnucdliutruynvcngComthdliuschinth
lnEditBoxReceive.KhibnnhnvoClearthsxodliuhinthtrnEditBoxny.
Ch:
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
14/78
cthtestlunchngtrnhccbnnittchn2vchn3caRS232livinhau
chnhlnichnRxDvTxDchngtatruyndliuraRS232saunhndliu
lun.ylvdtestnginkhngcbttayphncng.
Hnh2.2:SuchncaRS232
2.2.
Thitkgiaodinchochngtrnh
2.2.1. TodnRS232TUT
KhingVisualC++,chnNewtmenuFile
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
15/78
Hnh2.3:ChnNewtmenufile
Hnh2.4:ChnMFCApplication(exe),tndn,nichadn
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
16/78
Hnh2.5:ChnDialogbased
Hnh2.6:NhnNexttiptc
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
17/78
Hnh2.7:chnnhmcnh,nhnNextlun
Hnh2.8:Kchfinishktthcvictodn
Giaodincangdngbanunhsau:
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
18/78
Hnh2.9:Giaodinbanu
2.2.2. ThmiukhinActiveXMicrosoftCommunication6.0
utinbndngntDeletexo2ntOK,CancelvdngvnbntnhTODO
TmenuProjectchnAddtoProject.MenuconhinrabnchnComponents
andControls:
Hnh2.10:ChnComponentsControls
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
19/78
HpthoiComponentsandControlGalleryxuthinbnchnRegestered
ActiveXControls(ylthmcchacciukhinActiveX):
Hnh2.11:Chnthmcutin
TrongthmcRegisteredActiveXControlsbnchniukhinmmnhcn
MicrosoftCommunicationsControl,version6.0.KchOK
Hnh2.12:ChnMicrosoftCommunicationControl,version6.0
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
20/78
Hpthoiyucubnxcnhncthmiukhinnyvokhng,chnOK
Hnh2.13:ChnOKthmiukhin
KhichngtasccmtlpmiCMSCommllpdnxutcalpc
sCWnd.LpnyckhaibotrongfileMSComm.hvnhnghatrongfile
MSComm.cpp
Hnh2.14:Chntnlp,tnheaderfile,filethcthinhmcnh
NhnClosenghpthoi
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
21/78
Hnh2.15:ngcasComponentsandControlsGallery
BnthmiukhinMSCommvohpgiaodinbngcchgpthnhvicc
iukhinthngthngkhc
Hnh2.16:ThmiukhinMSComm
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
22/78
ThlccbnchocngdngMSCommvotrongDialog
2.2.3. Thmcciukhinkhc
2.2.3.1.
ThmccGroupBox
Hnh2.17:ThmGroupBox1
Hnh2.18:ThmGroupBox2
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
23/78
Hnh2.19:ThmGroupBox3
Hnh2.20:ThmGroupBox4
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
24/78
Hnh2.21:ThmGroupBox5
Hnh2.22:ThmGroupBox6
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
25/78
Hnh2.23:ThmGroupBox7
Hnh2.24:ThmGroupBox8
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
26/78
Hnh2.25:ThmGroupBox9
ThayithuctnhchoGroupBox1:
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
27/78
Hnh2.26:KchchutvoGroupBox1chnProperties
Hnh2.27:ThitlpthuctnhCaption:Settings
CcbnlmtngtviccGroupBoxkhc,theohnhvdiy:
Hnh2.28:ThitlpthuctnhchoGroupBox2
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
28/78
Hnh2.30:ThitlpthuctnhchoGroupBox3
Hnh2.31:ThitlpthuctnhchoGroupBox4
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
29/78
Hnh2.32:ThitlpthuctnhchoGroupBox5
Hnh2.33:ThitlpthuctnhchoGroupBox6
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
30/78
Hnh2.34:ThitlpthuctnhchoGroupBox7
Hnh2.34:ThitlpthuctnhchoGroupBox8
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
31/78
Hnh2.35:ThitlpthuctnhchoGroupBox9
TiptheoccbnlmchoccGroupBox4>9ckchthcbngnhauvcnlphn
Hnh2.36:LmchoccGroupBoxccngkchthc
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
32/78
Hnh2.37LmchoccGroupBox7>9ckhongcchbngnhau
Hnh2.38:LmchoccGroupBoxnythnghngvinhau
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
33/78
Hnh2.39:Ktqusaukhilmchocngkchthc
2.2.3.2.
ThmccEditBox
Hnh2.40:ThmEditBox1
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
34/78
Hnh2.41:kchchutphi,chnProperties
Hnh2.42:ThitlpIDchoEditBox1
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
35/78
Hnh2.43:ThitlpthuctnhReadOnlyvMultiLine
Hnh2.44:ThmEditBox2
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
36/78
Hnh2.45:ChnProperties
Hnh2.46:ThuctnhcaEditBox
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
37/78
2.2.3.3.
ThmccButton
Hnh2.47:ThmButton1
Hnh2.48:ThmButton2
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
38/78
Hnh2.49:ThmButton3
Hnh2.50:To3Buttonccngkchthc
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
39/78
Hnh2.51:ChnProperties
Hnh2.52:ThuctnhcaButton1
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
40/78
Hnh2.53:ThuctnhcaButton2
Hnh2.54:ThuctnhcaButton3
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
41/78
2.2.3.4.
ThmccComboBox
Hnh2.55:ThmComboBox1
Hnh2.56:ThmComboBox2
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
42/78
Hnh2.57:ThmComboBox3
Hnh2.58:ThmComboBox4
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
43/78
Hnh2.59:ThmComboBox5
Hnh2.60:ThmComboBox6
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
44/78
Hnh2.61:TochocccomboBoxccngchiurng
Ch:RingviComboBoxthccbnphikosaochochiucaocaiu
khinphilnchaccdliunmtrongnsaunynukhngthbns
chngthynhinthghoclsthycthanhcun.Ttnhtlkodithoimi
i.
iuchnhchiucaocaccComboBoxnhsau:
DichutnComboBoxcontrchutnmtrnntxxungcaiukhin
saukchchuttri1ln.KhibnscthcodnchiucaocaComboBox
thoimi
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
45/78
Hnh2.62:CodnchiudicaComboBox
Tiptheo,thitlpccthamschoccComboBox
Hnh2.63:ChnthuctnhPropertiescaComboBox1
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
46/78
Hnh2.64:ThuctnhIDcaCombo1
Hnh2.65:ThuctnhIDcaCombo3
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
47/78
Hnh2.66:ThuctnhIDcaCombo5
Hnh2.67:ThuctnhIDcaCombo2
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
48/78
Hnh2.68:ThuctnhIDcaComboBox4
Hnh2.69:ThuctnhIDcaComboBox6
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
49/78
Hnh2.70:ThuctnhType:DropListcaccComboBoxchung
Ccbnsaukimtraccthuctnhcacciukhinbngbngsau:
STT
1
itng
MSComm
Thuctnh
ID
ComboBox
ID
Style
Button
ID
Thitlp
IDC_MSCOMM1
IDC_COMBO_COMPORT
Droplist
IDC_COMBO_BITRATE
Droplist
IDC_COMBO_DATABIT
Droplist
IDC_COMBO_PARITYBIT
Droplist
IDC_COMBO_STOPBIT
Droplist
IDC_COMBO_HANDSHAKING
Droplist
IDC_BUTTON_CLEAR
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
50/78
Caption
10
11
EditBox
12
13
GroupBox
ID
Style
ID
14
15
2.3.
&Clear
IDC_BUTTON_SEND
&Send
IDC_BUTTON_EXIT
&Exit
IDC_EDIT_RECEIVE
ReadOnly(check)
IDC_EDIT_TRANSFER
IDC_STATIC
Settings
IDC_STATIC
Receive
IDC_STATIC
Trasfer
Vitmchochngtrnh
2.3.1.Thmccbin,cchmthnhvin
vitmchochngtrnhthccbncnphithmccbinchocciu
khin.TmenuViewchnClassWizard.ChntabMemberVariables.Chnlp
CRS232TUTDlg
Hnh2.71:ChnMFCClassWizardtabMemberVariables,chnAddVariable..
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
51/78
Xinlyvdthmbinm_cboBitRatechoiukhincIDl
IDC_COMBO_BITRATE
Hnh2.72:ThmbiniukhinchoComboBoxBitRate
Ccbnthmccbinchocciukhintngtnhtrn.Sauylhnhminhho
victhmccbinny:
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
52/78
Hnh2.73:ThmbiniukhinchoComboBoxComPort
Hnh2.74:ThmbiniukhinchoComboBoxDataBit
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
53/78
Hnh2.75:ThmbiniukhinchoComboHandshaking
Hnh2.76:ThmbiniukhinchoComboParityBit
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
54/78
Hnh2.77:ThmbiniukhinchoComboStopBit
Hnh2.78:ThmbingitrchoEditBoxReceive
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
55/78
Hnh2.79:ThmbingitrchoEditBoxTransfer
Hnh2.80:ThmbiniukhinchoMSComm
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
56/78
Hnh2.81:KtqucuicngtrongcasMFC..
Thmcchmthnhvin:
xongphnthmccbin.Bygiccbnsthmmtshmnhsau:
voidInitComboBox(void)
dngkhitochoccComboBox
voidSetting(void)
thitlpthamschoMSComm
CStringgetCurStrInCombobox(constCComboBox&a)
:hmnydnglyxuangclachntrongccComboBox
lmciuthccbnlmnhsau:ChntabClass..Kchchutphi
volpCRS232TUTDlgchnAddMemberFunction:
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
57/78
Hnh2.82:ChntabClasstrongcasWorkspace
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
58/78
Hnh2.83:KchchutphichnAddMemberFunction
Hnh2.84:ThmhmgetCurStrInCombobox,chnOK
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
59/78
Hnh2.85:Lmtngt,thmhmInitComboBox
Hnh2.86:ThmhmSettings()
2.3.2.Vitmchotnghm
Chngtasvitmlnhchomtshm
2.3.2.1.
HmgetCurStrInCombobox:hmctcdnglygitrcchn
caccComboBox
CcbnkchvocasbntrichntabClass,kchpchutvotnhm,bn
phishinraninhnghahm,ccbnthmmvo.
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
60/78
Hnh2.87:KchpvotnhmgetCurStrInComboboxbntrinhnghahm
Thmmchngtrnh:
CStringCRS232TUTDlg::getCurStrInCombobox(constCComboBox&a)
{
CStringstr;
a.GetLBText(a.GetCurSel(),str);
returnstr;
}
2.3.2.2.
HmInitComboBox:dngkhitoccgitrchoComboBox
KchpchutvohmInitComboBox(hmnydngkhitoccgitrcho
ComboBox)trongcasbntritngtnhvihmtrn.
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
61/78
Hnh2.88:KchpvotnhmInitComboBox
Ccbnthmonmnyvohm:
voidCRS232TUTDlg::InitComboBox()
{
//ComboBoxComPort
m_cboComPort.ResetContent();
m_cboComPort.AddString(COM1);
m_cboComPort.AddString(COM2);
m_cboComPort.SetCurSel(0);
//ComboBoxBitRate
m_cboBitRate.ResetContent();
m_cboBitRate.InsertString(0,600);
m_cboBitRate.InsertString(1,1200);
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
62/78
m_cboBitRate.InsertString(2,2400);
m_cboBitRate.InsertString(3,4800);
m_cboBitRate.InsertString(4,9600);
m_cboBitRate.InsertString(5,14400);
m_cboBitRate.InsertString(6,19200);
m_cboBitRate.InsertString(7,28800);
m_cboBitRate.InsertString(8,38400);
m_cboBitRate.InsertString(9,56000);
m_cboBitRate.InsertString(10,57600);
m_cboBitRate.InsertString(11,115200);
m_cboBitRate.InsertString(12,128000);
m_cboBitRate.InsertString(13,256000);
m_cboBitRate.SetCurSel(4);
//ComboBoxDataBit
m_cboDataBit.ResetContent();
m_cboDataBit.AddString(5);
m_cboDataBit.AddString(6);
m_cboDataBit.AddString(7);
m_cboDataBit.AddString(8);
m_cboDataBit.SetCurSel(3);
//ComboBoxStopBit
m_cboStopBit.ResetContent();
m_cboStopBit.AddString(1);
m_cboStopBit.AddString(1.5);
m_cboStopBit.AddString(2);
m_cboStopBit.SetCurSel(0);
//ComboBoxParityBit
m_cboParityBit.ResetContent();
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
63/78
m_cboParityBit.InsertString(0,None);
m_cboParityBit.InsertString(1,Old);
m_cboParityBit.InsertString(2,Even);
m_cboParityBit.InsertString(3,Mark);
m_cboParityBit.InsertString(4,Space);
m_cboParityBit.SetCurSel(0);//mcnhchnNone
//ComboBoxHandshaking
m_cboHandshaking.ResetContent();
m_cboHandshaking.InsertString(0,None);
m_cboHandshaking.InsertString(1,XON/XOFF);
m_cboHandshaking.InsertString(2,RTS);
m_cboHandshaking.InsertString(3,RTS/CTS+XON/XOFF);
m_cboHandshaking.SetCurSel(0);
}
Ccbnhontoncththmdliubanubngcchkhckchchutphi
lnComboBoxchnProperties>chntabData,sauthnhpgitrvo,sau
midngnthpphmCtr+Enterxungdngnhptiptheo.
Hnh2.89:Thmdliubngtay
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
64/78
2.3.2.3.
HmSettings:dngthitlpccthamschoMSComm
BnkchpvohmSettings(hmnydngthitlpccthamscho
MSComm)
Hnh2.90:KchpchutvotnhmSettingsbntri
Thmonmvohm
voidCRS232TUTDlg::Settings()
{
//nucngmthngcngthitlpthams
if(m_mscomm.GetPortOpen())
//Thitlptncng
m_mscomm.SetCommPort(m_cboComPort.GetCurSel()+1);
//Thitlpbttay
m_mscomm.SetHandshaking(m_cboHandshaking.GetCurSel());
m_mscomm.SetPortOpen(false);
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
65/78
//Thitlptns,bitdliu,btstop
CStringstrBitRate=getCurStrInCombobox(m_cboBitRate);
CStringstrParity=getCurStrInCombobox(m_cboParityBit);
CStringstrDataBit=getCurStrInCombobox(m_cboDataBit);
CStringstrStopBit=getCurStrInCombobox(m_cboStopBit);
CStringstrSetting;
strSetting.Format(%s,%c,%s,%s,strBitRate,strParity[1],strDataBit,strStopBit);
m_mscomm.SetSettings(strSetting);
//Thitlpkhc
m_mscomm.SetRThreshold(1);
m_mscomm.SetInputLen(1);//milncmtkt
m_mscomm.SetInBufferSize(1024);
m_mscomm.SetInputMode(0);//0TextMode,1BinaryMode
m_mscomm.SetOutBufferSize(1024);
m_mscomm.SetPortOpen(true);//mcng
}
2.3.2.4.
Hmkhitochohpthoi
HmOnInitDialog()hmnynmtronglpCRS232TUTDialog,ngdngs
loadccthitlpcabntronghmnyngaykhichy.
utinbnphithmhmnyvobngcchmMFCClassWizard(nthp
phmCtr+W),chntabMessageMaps:
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
66/78
Hnh2.91:ThmhmInitDialog
Tiptheo,bnkchvoEditCode,khungbnphicaVC++shinrahm
OnInitDialog()bnthmcodevo:
Hnh2.92:ChnEditCodevitthmvohm
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
67/78
Thmonmnyvo:
BOOLCRS232TUTDlg::OnInitDialog(){
.
//TODO:Addextrainitializationhere
InitComboBox();
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
2.3.2.5.
CchmchoccntButtonpngskinClick:
Ccbclmcnghontontngtnhtrn:thmhm,vitmlnhcho
hm
HmOnButtonExit()
Hnh2.93:ThmhmchontExit
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
68/78
Hnh2.94:ngvitnhmlOnButtonExit
Hnh2.95:NhnEditCodevitmchohm
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
69/78
Hnh2.96:Cascodehinrathmcode
voidCRS232TUTDlg::OnButtonExit()
{
//TODO:Addyourcontrolnotificationhandlercodehere
//Nucngcnmthngli
if
(m_mscomm.GetPortOpen())
OnOK();
m_mscomm.SetPortOpen(false);
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
70/78
HmOnButtonSend:
Hnh2.97:ThmhmchontSend
Hnh2.98:ngvitnhmlOnButtonSend
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
71/78
Hnh2.99:NhnEditCodevithm
Hnh2.100:Casthmcode
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
72/78
voidCRS232TUTDlg::OnButtonSend()
{
//TODO:Addyourcontrolnotificationhandlercodehere
UpdateData();
Settings();
//senddata
m_mscomm.SetOutput((COleVariant)m_strTransfer);
UpdateData(false);
}
HmOnButtonClear:
Hnh2.101:ThmhmchontClear
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
73/78
Hnh2.102:ngvitnhmlOnButtonClear
Hnh2.103:Casthmcode
voidCRS232TUTDlg::OnButtonClear()
{
//TODO:Addyourcontrolnotificationhandlercodehere
UpdateData();
m_strReceive=;
UpdateData(false);
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
74/78
HmOnOnCommMscomm1:ytichdngmtngtlngtnhn,ccbn
cthdngccngtkhctucamnh.
DotithitlpInputLen=1,nnmilnchnhnmtkt,ccbncthdng
iunykimtrathngtinnhntviiukhin,vdmtktbohiuchng
hn
Hnh2.104:ThmhmchoMSComm1
Hnh2.105:ngvitnhmlOnOnCommMscomm1
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
75/78
Hnh2.106:Casthmm
voidCRS232TUTDlg::OnOnCommMscomm1()
{
//TODO:Addyourcontrolnotificationhandlercodehere
UpdateData();
switch(m_mscomm.GetCommEvent()){
case1://comEvSend
case2://comEvReceive
VARIANTdata=m_mscomm.GetInput();
m_strReceive+=(CString)data.bstrVal;
break;
case3://comEvCTS
break;
break;
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
76/78
case4://comEvDSR
case5://comEvCD
case6://comEvRing
case7://comEvEOF
};
UpdateData(false);
break;
break;
break;
break;
}
Thlxong.Bygiccbndch(nuclithsalichong)vchy
chngtrnhxemktqumnhthno.
Hnh2.107:Dchchngtrnh
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
77/78
Hnh2.108:Kimtrasalichngtrnhnkhikhngcli
Hnh2.109:Chychngtrnh
Ngiboco:
NgHiBc
Tiliu:
TUT01.03
Ngy:
10/01/06
Trang:
78/78
Hnh2.110:ngdngkhichy
BnnhmtviktvotrongTransferthitlpthamsvtruynth
kimtrachngtrnh.
3. Tiliuthamkho
PhmCngNgPhmNgcNamPhmTunLngThclptrnh
VisualC++6.0tcbnnnngcao
MicrosoftCorporationMSDNJuly2001
JanAxelsonSerialPorCompleteProgrammingandCircuitsforRS232andRS
485LinksandNetworks
http://codeproject.com
http://microsoft.com/MSDN