' ' ' #################### ' ##### PROLOG ##### ' #################### ' PROGRAM "SerRex" ' 1-8 char program/file name without .x or any .extent VERSION "0.0.1" ' version number - increment before saving altered program ' ' You can stop the PDE from inserting the following PROLOG comment lines ' by removing them from the prolog.xxx file in your \xb\xxx directory. ' ' Programs contain: 1: PROLOG - no executable code - see below ' 2: Entry function - start execution at 1st declared func ' * = optional 3: Other functions - everything else - all other functions ' ' The PROLOG contains (in this order): ' * 1. Program name statement PROGRAM "progname" ' * 2. Version number statement VERSION "0.0000" ' * 3. Import library statements IMPORT "libName" ' * 4. Composite type definitions TYPE ... END TYPE ' 5. Internal function declarations DECLARE/INTERNAL FUNCTION Func (args) ' * 6. External function declarations EXTERNAL FUNCTION FuncName (args) ' * 7. Shared constant definitions $$ConstantName = literal or constant ' * 8. Shared variable declarations SHARED variable ' ' ****** Comment libraries in/out as needed ***** ' IMPORT "xma" ' Math library : SIN/ASIN/SINH/ASINH/LOG/EXP/SQRT... IMPORT "xcm" ' Complex library : complex number library (trig, etc) IMPORT "xst" ' Standard library : required by most programs IMPORT "xgr" ' GraphicsDesigner : required by GuiDesigner programs IMPORT "xui" ' GuiDesigner : required by GuiDesigner programs ' IMPORT "kernel32" IMPORT "user32" TYPE CONVERT UNION UBYTE .ubyte STRING*1 .string END UNION END TYPE DECLARE FUNCTION Entry () DECLARE FUNCTION SystemRexOpen ( CPort$, ctoParam ) DECLARE FUNCTION SystemRexClose () DECLARE FUNCTION SystemRexComm$ ( Command$, SendFF ) DECLARE FUNCTION WrapRexComm$ ( WhichCommand ) DECLARE FUNCTION FindPort$ () DECLARE FUNCTION ConvTime (outbuffer$) DECLARE FUNCTION CalcRexChecksum$ ( Command$ ) ' ' ###################### ' ##### Entry () ##### ' ###################### ' ' Programs contain: ' 1. A PROLOG with type/function/constant declarations. ' 2. This Entry() function where execution begins. ' 3. Zero or more additional functions. ' FUNCTION Entry () SHARED Debug SHARED hCom 'declarations DCB dcb COMMTIMEOUTS cto ' Debug level ' 0 : nothing ' 1 : only commands and answers ' 2 : system calls and descriptions Debug = 2 'open port PcCommPort$ = FindPort$() ctoParam = 1000 ' dont know what this one does SystemRexOpen( PcCommPort$, ctoParam ) WhichCommand = 1 outbuffer$ = WrapRexComm$( WhichCommand ) WhichCommand = 2 outbuffer$ = WrapRexComm$( WhichCommand ) WhichCommand = 3 outbuffer$ = WrapRexComm$( WhichCommand ) 'write the actual command to the Rex 'close the port SystemRexClose () END FUNCTION ' ' ' ############################## ' ##### SystemRexOpen () ##### ' ############################## ' FUNCTION SystemRexOpen ( CPort$, ctoParam ) SHARED Debug SHARED hCom DCB dcb COMMTIMEOUTS cto cto.ReadIntervalTimeout = ctoParam cto.ReadTotalTimeoutMultiplier = 0 cto.ReadTotalTimeoutConstant = ctoParam cto.WriteTotalTimeoutMultiplier = 0 cto.WriteTotalTimeoutConstant = ctoParam hCom = CreateFileA(&CPort$,ULONG($$GENERIC_READ)| ULONG($$GENERIC_WRITE), 0, 0, ULONG($$OPEN_EXISTING), 0, 0) IF (Debug>1) THEN IF (hCom>0) THEN PRINT "SystemRexOpen : CreateFile Succeded" ELSE PRINT "SystemRexOpen : CreateFile Failed" RETURN 0 ENDIF ELSE IF (hCom=0) THEN PRINT "SystemRexOpen : CreateFile Failed" RETURN 0 ENDIF ENDIF dummy = SetCommMask( hCom, 1 ) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexOpen : SetCommMask Succeded" ELSE PRINT "SystemRexOpen : SetCommMask Failed" RETURN 0 ENDIF ELSE IF (dummy=0) THEN PRINT "SystemRexOpen : SetCommMask Failed" RETURN 0 ENDIF ENDIF dummy = SetupComm(hCom,8192,8192) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexOpen : SetupComm Succeded" ELSE PRINT "SystemRexOpen : SetupComm Failed" RETURN 0 ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexOpen : SetupComm Failed" RETURN 0 ENDIF ENDIF dummy = SetCommTimeouts(hCom,&cto) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexOpen : SetCommTimeouts Succeded" ELSE PRINT "SystemRexOpen : SetCommTimeouts Failed" RETURN 0 ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexOpen : SetCommTimeouts Failed" RETURN 0 ENDIF ENDIF dummy = GetCommState(hCom,&dcb) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexOpen : GetCommState Succeded" ELSE PRINT "SystemRexOpen : GetCommState Failed" RETURN 0 ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexOpen : GetCommState Failed" RETURN 0 ENDIF ENDIF dcb.BaudRate = 57600 dcb.Parity = 0 dcb.StopBits = 0 dummy = SetCommState(hCom,&dcb) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexOpen : SetCommState Succeded" ELSE PRINT "SystemRexOpen : SetCommState Failed" RETURN 0 ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexOpen : SetCommState Failed" RETURN 0 ENDIF ENDIF RETURN 1 END FUNCTION ' ' ' ############################### ' ##### SystemRexClose () ##### ' ############################### ' FUNCTION SystemRexClose () SHARED Debug SHARED hCom dummy = CloseHandle( hCom ) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexClose : Succeded" ELSE PRINT "SystemRexClose : Failed" ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexClose : Failed" ENDIF ENDIF END FUNCTION ' ' ############################## ' ##### SystemRexComm$ () #### ' ############################## ' FUNCTION SystemRexComm$ ( Command$, SendFF ) SHARED Debug SHARED hCom tx1 = 0xFF tx2$ = "DSBS0000EC"+CHR$(10) tx3$ = "DSVR3F"+CHR$(10) ' Command$ = tx2$ numwritten = 0 numread = 0 outbuffer$ = CHR$(0,8192) IF (SendFF=1) THEN ' write first character to port dummy = WriteFile(hCom,&tx1,ULONG(1),&numwritten,0) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexComm$ : 1st FF Succeded" ELSE PRINT "SystemRexComm$ : 1st FF Failed" ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexComm$ : 1st FF Failed" ENDIF ENDIF 'write second character to port dummy = WriteFile(hCom,&tx1,ULONG(1),&numwritten,0) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexComm$ : 2nd FF Succeded" ELSE PRINT "SystemRexComm$ : 2nd FF Failed" ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexComm$ : 2nd FF Failed" ENDIF ENDIF ' hopefully we won"t need this, because I don"t know the right params 'lpError = $$CE_BREAK 'lpStatus = ULONG(0) 'dummy = ClearCommError(hCom,&lpError,&lpStatus) ENDIF 'write the actual command to the Rex dummy = WriteFile(hCom,&Command$,ULONG(LEN(Command$)),&numwritten,0) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexComm$ : Sending command succeded" ELSE PRINT "SystemRexComm$ : Sending command failed" ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexComm$ : Sending command failed" ENDIF ENDIF dummy = FlushFileBuffers(hCom) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexComm$ : Flushing buffer succeded" ELSE PRINT "SystemRexComm$ : Flushing buffer failed" ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexComm$ : Flushing buffer failed" ENDIF ENDIF 'read what we got dummy = ReadFile(hCom,&outbuffer$,8192,&numread,0) outbuffer$ = CSIZE$(outbuffer$) IF (Debug>1) THEN IF (dummy=1) THEN PRINT "SystemRexComm$ : Reading succeded" ELSE PRINT "SystemRexComm$ : Reading failed" ENDIF ELSE IF (dummy<>1) THEN PRINT "SystemRexComm$ : Reading failed" ENDIF ENDIF RETURN outbuffer$ END FUNCTION ' ' ' ############################ ' ##### WrapRexComm$ () #### ' ############################ ' FUNCTION WrapRexComm$ ( WhichCommand ) SHARED Debug SHARED hCom C_Firmware$ = "VR" C_EraseFile$ = "FE" C_ReadFile$ = "FR" C_WriteFile$ = "SF" C_WhatEver$ = "SF" C_Defrag$ = "SG" C_DeleteRecord$ = "SD" C_ReadRecord$ = "SR" C_WriteRecord$ = "SW" C_ReadDateTime$ = "TR" C_WriteDateTime$ = "TW" C_StartSession$ = "BS" C_EndSession$ = "BE" C_RexInfo$ = "PR" C_UpdateFirmware$ = "UF" SELECT CASE WhichCommand CASE 1: ' Command$ = "DSBS0000EC" + CHR$(10) Command$ = "DSBS0000" SendFF = 1 CASE 2: ' Command$ = "DSVR3F" + CHR$(10) Command$ = "DSVR" SendFF = 0 CASE 3: ' Command$ = "DSTR3D" + CHR$(10) Command$ = "DSTR" SendFF = 0 END SELECT IF (Debug>0) PRINT "WrapRexComm : Sending ",Command$ ENDIF PRINT Command$ Command$ = CalcRexChecksum$( Command$ ) outbuffer$ = SystemRexComm$( Command$, SendFF ) IF (Debug>0) PRINT "WrapRexComm : Received ",outbuffer$ ENDIF SELECT CASE WhichCommand CASE 1: PRINT "1" CASE 2: PRINT "2" CASE 3: ConvTime(outbuffer$) END SELECT RETURN outbuffer$ END FUNCTION ' ' ' ########################## ' ##### FindPort$ () ##### ' ########################## ' FUNCTION FindPort$ () SHARED Debug ctoParam = 1000 IF (dummy=0) THEN port$ = "COM1:" dummy = SystemRexOpen( port$, ctoParam ) ENDIF IF (dummy=0) THEN port$ = "COM2:" dummy = SystemRexOpen( port$, ctoParam ) ENDIF IF (dummy=0) THEN port$ = "COM3:" dummy = SystemRexOpen( port$, ctoParam ) ENDIF IF (dummy=0) THEN port$ = "COM4:" dummy = SystemRexOpen( port$, ctoParam ) ENDIF IF (Debug>1) THEN IF (dummy=1) THEN PRINT "FindPort$ : Opened", port$, "for communication" ELSE PRINT "FindPort$ : Failed to open COM port" ENDIF ELSE IF (dummy<>1) THEN PRINT "FindPort$ : Failed to open COM port" ENDIF ENDIF IF (dummy=1) THEN SystemRexClose() RETURN port$ ELSE RETURN "0" ENDIF END FUNCTION ' ' ' ######################### ' ##### ConvTime () ##### ' ######################### ' FUNCTION ConvTime (rextimestring$) rextimestring$ = "1" IF (LEN(rextimestring$)>20) THEN yyyy$ = MID$(rextimestring$,3,4) mmmm$ = MID$(rextimestring$,7,4) dddd$ = MID$(rextimestring$,11,4) mmmi$ = MID$(rextimestring$,15,4) hhhh$ = MID$(rextimestring$,19,4) ssss$ = MID$(rextimestring$,23,4) year = ULONG("0x"+yyyy$) month = ULONG("0x"+mmmm$) day = ULONG("0x"+dddd$) hour = ULONG("0x"+hhhh$) minute = ULONG("0x"+mmmi$) second = ULONG("0x"+ssss$) PRINT "RexTime is",year,"-",month,"-",day," ",hour,":",minute,":",second ELSE year = 0 month = 0 day = 0 hour = 0 minute = 0 second = 0 weekday = 0 millisecond = 0 XstGetDateAndTime(@year,@month,@day,@weekday,@hour,@minute,@second,@millisecond) yyyy$ = HEX$(year,4) mmmm$ = HEX$(month,4) dddd$ = HEX$(day,4) hhhh$ = HEX$(hour,4) mmmi$ = HEX$(minute,4) ssss$ = HEX$(second,4) PRINT year,month,day,hour,minute,second rextimestring$ = yyyy$ + mmmm$ + dddd$ + hhhh$ + mmmi$ + ssss$ PRINT rextimestring$ ENDIF END FUNCTION ' ' ' ################################ ' ##### CalcRexChecksum () ##### ' ################################ ' FUNCTION CalcRexChecksum$ ( Command$ ) SHARED Debug CONVERT c dummy = 0 FOR i = 1 TO LEN(Command$) c.string = MID$(Command$,i,1) dummy = dummy + c.ubyte NEXT i dummy = dummy MOD 256 IF (Debug>1) THEN PRINT "CalcRexChecksum :",HEX$(dummy,2) ENDIF dummy$ = Command$ + HEX$(dummy,2) + CHR$(10) RETURN dummy$ END FUNCTION END PROGRAM