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