00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <e32base.h>
00036 #include <e32test.h>
00037 #include <e32svr.h>
00038 #include <c32comm.h>
00039 #include <f32file.h>
00040
00041 #include "CommonFiles.h"
00042
00043
00044
00045 _LIT(LDD_NAME,"ECOMM");
00046
00047 #if defined (__WINS__)
00048 _LIT(PDD_NAME,"ECDRV");
00049 #else
00050 _LIT(PDD_NAME,"EUART");
00051 #endif
00052
00053
00054
00055 const TInt KBufSize (512);
00056 const TInt KOneHundredSecond (100000000);
00057
00058
00059 _LIT(KMessage2,"%c\n");
00060 _LIT(KMessage14,"^%c");
00061 _LIT(KMessage15,"%c");
00062 _LIT(KColons,"::");
00063
00064
00065 void printReceivedText(TDes8& localInputBuffer,TInt numberRead);
00066
00067 LOCAL_C void doExampleL ()
00068 {
00069 _LIT(KMessage0,"Select S for RS232 Serial or R for InfraRed port : ");
00070 _LIT(KMessage1,"Select 0 for no handshaking, 1 for CTS/RTS and 2 for XON/XOFF :");
00071 _LIT(KMessage4,"Loading device drivers\n");
00072 _LIT(KMessage5,"Starting comms server\n");
00073 _LIT(KMessage6,"Connecting to comms server\n");
00074 _LIT(KMessage7,"Loading %S.CSY module\n");
00075 _LIT(KMessage8,"%S has %S available as %S::%u to %S::%u\n");
00076 _LIT(KMessage9,"Opened %S\n");
00077 _LIT(KMessage10,"Configuring Serial port for 115200 bps 8 bits no parity 1 stop\n");
00078 _LIT(KMessage11,"Powering up port\n");
00079 _LIT(KMessage12,"\nDisconnecting\n");
00080 _LIT(KMessage13,"\nWrite Failed %d\n");
00081 _LIT(KMessage16,"\nRead failed %d\n");
00082 _LIT(KMessage17,"Closed %S\n");
00083 _LIT(KMessage18,"Closing server connection\n");
00084 _LIT(KMessage19,"Comms server reports we have %u comms modules loaded\n");
00085 _LIT(KMessage20,"Using the lowest %S out of %S::%u to %S::%u\n");
00086
00087 _LIT(KPanic,"StraySignal");
00088 _LIT(RS232,"ECUART");
00089 _LIT(IRCOMM,"IRCOMM");
00090
00091 TBuf16 < 6 > csyName;
00092
00093 TUint8 csyMode;
00094 const TUint8 mask=0xdf;
00095
00096 console->Printf (KMessage0);
00097 do
00098 csyMode = STATIC_CAST(TUint8,console->Getch () & mask);
00099 while ((csyMode < 'R') || (csyMode > 'S'));
00100 console->Printf (KMessage2, csyMode);
00101
00102 if (csyMode=='S')
00103 csyName.Copy(RS232);
00104 else
00105 csyName.Copy(IRCOMM);
00106
00107
00108
00109 TKeyCode handshakingMode;
00110 console->Printf (KMessage1);
00111 do
00112 handshakingMode = console->Getch ();
00113 while ((handshakingMode < '0') || (handshakingMode > '2'));
00114 console->Printf (KMessage2, handshakingMode);
00115
00116
00117
00118
00119
00120
00121
00122
00123 #if defined (__WINS__)
00124 _LIT(KMessage3,"Connect to file server\n");
00125 console->Printf (KMessage3);
00126 RFs fileServer;
00127 User::LeaveIfError (fileServer.Connect ());
00128 fileServer.Close ();
00129 #endif
00130
00131
00132
00133
00134
00135
00136
00137
00138 console->Printf (KMessage4);
00139 TInt r = User::LoadPhysicalDevice (PDD_NAME);
00140 if (r != KErrNone && r != KErrAlreadyExists)
00141 User::Leave (r);
00142 r = User::LoadLogicalDevice (LDD_NAME);
00143 if (r != KErrNone && r != KErrAlreadyExists)
00144 User::Leave (r);
00145
00146
00147
00148
00149 #if !defined (__WINS__)
00150 console->Printf (KMessage5);
00151 r = StartC32 ();
00152 if (r != KErrNone && r != KErrAlreadyExists)
00153 User::Leave (r);
00154 #endif
00155
00156
00157
00158 console->Printf (KMessage6);
00159 RCommServ server;
00160 User::LeaveIfError (server.Connect ());
00161
00162
00163
00164
00165
00166 console->Printf (KMessage7,&csyName);
00167 r = server.LoadCommModule (csyName);
00168 User::LeaveIfError (r);
00169
00170
00171
00172
00173
00174
00175
00176 TInt numPorts;
00177 r = server.NumPorts (numPorts);
00178 User::LeaveIfError (r);
00179 console->Printf (KMessage19,numPorts);
00180
00181
00182
00183
00184
00185
00186
00187 TSerialInfo portInfo;
00188 TBuf16 < 12 > moduleName;
00189
00190 for (TInt index=0 ; index < numPorts ; index++)
00191 {
00192 r = server.GetPortInfo (index, moduleName, portInfo);
00193 User::LeaveIfError (r);
00194 console->Printf (KMessage8,
00195 &moduleName,
00196 &portInfo.iDescription,
00197 &portInfo.iName,
00198 portInfo.iLowUnit,
00199 &portInfo.iName,
00200 portInfo.iHighUnit);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 r = server.GetPortInfo (csyName, portInfo);
00213 console->Printf (KMessage20,
00214 &portInfo.iDescription,
00215 &portInfo.iName,
00216 portInfo.iLowUnit,
00217 &portInfo.iName,
00218 portInfo.iHighUnit);
00219
00220
00221
00222
00223
00224
00225 TBuf16 < KMaxPortName + 4 > portName;
00226 portName.Num (portInfo.iLowUnit);
00227 portName.Insert (0, KColons);
00228 portName.Insert (0, portInfo.iName);
00229
00230
00231
00232 RComm commPort;
00233 console->Printf (KMessage9, &portName);
00234 r = commPort.Open (server, portName, ECommExclusive);
00235 User::LeaveIfError (r);
00236
00237
00238
00239
00240
00241
00242 TCommCaps ourCapabilities;
00243 commPort.Caps (ourCapabilities);
00244
00245 if (((ourCapabilities ().iRate & KCapsBps115200) == 0) ||
00246 ((ourCapabilities ().iDataBits & KCapsData8) == 0) ||
00247 ((ourCapabilities ().iStopBits & KCapsStop1) == 0) ||
00248 ((ourCapabilities ().iParity & KCapsParityNone) == 0))
00249 User::Leave (KErrNotSupported);
00250
00251 console->Printf (KMessage10);
00252
00253 TCommConfig portSettings;
00254 commPort.Config (portSettings);
00255 portSettings ().iRate = EBps115200;
00256 portSettings ().iParity = EParityNone;
00257 portSettings ().iDataBits = EData8;
00258 portSettings ().iStopBits = EStop1;
00259
00260
00261
00262
00263 portSettings ().iFifo = EFifoEnable;
00264 if (handshakingMode == '2')
00265 portSettings ().iHandshake = (KConfigObeyXoff | KConfigSendXoff);
00266 else if (handshakingMode == '1')
00267 portSettings ().iHandshake = (KConfigObeyCTS | KConfigFreeRTS);
00268 else
00269 portSettings ().iHandshake = KConfigFailDSR;
00270
00271 portSettings ().iTerminator[0] = 10;
00272 portSettings ().iTerminatorCount = 1;
00273
00274 r = commPort.SetConfig (portSettings);
00275 User::LeaveIfError (r);
00276
00277
00278
00279 commPort.SetSignals (KSignalDTR, 0);
00280 commPort.SetSignals (KSignalRTS, 0);
00281 TInt curlenth = commPort.ReceiveBufferLength ();
00282 commPort.SetReceiveBufferLength (4096);
00283 curlenth = commPort.ReceiveBufferLength ();
00284
00285
00286
00287 TKeyCode key;
00288 TPtrC8 outputByte ((TUint8 *) & key, 1);
00289 TBuf8 < KBufSize > localInputBuffer;
00290 TRequestStatus readStat, keyStat;
00291
00292
00293
00294 console->Printf (KMessage11);
00295 commPort.Read (readStat, localInputBuffer, 0);
00296 User::WaitForRequest(readStat);
00297 r = readStat.Int ();
00298 User::LeaveIfError (r);
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 console->Read (keyStat);
00314 commPort.Read (readStat, KOneHundredSecond, localInputBuffer);
00315 for (;;)
00316 {
00317 User::WaitForRequest (readStat, keyStat);
00318
00319
00320
00321 if (keyStat != KRequestPending)
00322 {
00323 key = console->KeyCode ();
00324
00325 if (key == 0x1b)
00326 {
00327 console->Printf (KMessage12);
00328 commPort.ReadCancel ();
00329 User::WaitForRequest (readStat);
00330 break;
00331 }
00332
00333 if (key < 256)
00334 {
00335 TRequestStatus stat;
00336 commPort.Write (stat, outputByte);
00337 User::WaitForRequest (stat);
00338 r = stat.Int ();
00339 if (r != KErrNone)
00340 console->Printf (KMessage13, r);
00341 }
00342
00343 console->Read (keyStat);
00344 }
00345
00346
00347
00348
00349
00350
00351
00352 else if (readStat != KRequestPending)
00353 {
00354 if (readStat == KErrNone || readStat == KErrTimedOut)
00355 {
00356
00357 TInt numberRead = localInputBuffer.Length ();
00358 if (numberRead != 0)
00359 printReceivedText(localInputBuffer,numberRead);
00360 else
00361
00362 {
00363 numberRead = commPort.QueryReceiveBuffer();
00364 if (numberRead != 0)
00365 {
00366 commPort.ReadOneOrMore(readStat, localInputBuffer);
00367 User::WaitForRequest (readStat);
00368 if (readStat == KErrNone) printReceivedText(localInputBuffer,numberRead);
00369 }
00370 }
00371 }
00372 else
00373 console->Printf (KMessage16, readStat.Int ());
00374 commPort.Read (readStat, KOneHundredSecond, localInputBuffer);
00375 }
00376
00377
00378
00379 else
00380 {
00381 User::Panic (KPanic, 0);
00382 }
00383
00384 }
00385
00386
00387
00388 commPort.Close ();
00389 console->Printf (KMessage17, &portName);
00390 console->Printf (KMessage18);
00391 server.Close ();
00392 }
00393
00394 void printReceivedText(TDes8& localInputBuffer,TInt numberRead)
00395 {
00396 TUint8 *nextByte = &localInputBuffer[0];
00397 for (int i = 0; i < numberRead; i++, nextByte++)
00398 {
00399 if ((*nextByte < 32) && (*nextByte != 10) && (*nextByte != 13))
00400 console->Printf (KMessage14, (*nextByte) + 64);
00401 else
00402 console->Printf (KMessage15, *nextByte);
00403 }
00404 }