#DIM ALL #COMPILE DLL FUNCTION GetTempHum(BYREF Temperature AS DOUBLE, BYREF Humidity AS DOUBLE, BYVAL ComPortNo AS INTEGER) EXPORT AS INTEGER 'This function retrieves temperature and humidity data from the RH#160 module by phanderson.com 'It expects ComPortNo to be the com port (ie. 1, 2, whatever) 'It returns Temperature and Humidity variables 'Result codes are 1 (success), 0 (comm error usually), -XXX (error code) ON ERROR GOTO fail DIM Buffer AS STRING 'Buffer for retrieving response DIM Bsize AS QUAD 'For reading pending buffer size DIM ctr AS INTEGER 'Counter variable for read loop (don't wait more than a second for response) DIM ComPort AS STRING 'Com port (textual name; ie. COM1) DIM FF AS INTEGER 'Pointer at free file handle ComPort = "COM" & FORMAT$(ComPortNo) 'Create textual name of com port ff = FREEFILE 'Find next available file handle COMM OPEN ComPort AS #ff 'Open port 'Set basic comm port parameters COMM SET #ff, BAUD = 9600 '9600 baud COMM SET #ff, BYTE = 8 '8 bits COMM SET #ff, PARITY = 0 'No parity COMM SET #ff, STOP = 0 '1 stop bit 'Initialize TX/RX buffers COMM SET #ff, TXBUFFER = 2048 COMM SET #ff, RXBUFFER = 4096 'Turn off ALL flow control COMM SET #ff, CTSFLOW = 0 COMM SET #ff, RTSFLOW = 0 COMM SET #ff, XINPFLOW = 0 COMM SET #ff, XOUTFLOW = 0 COMM SET #ff, DSRFLOW = 0 COMM SET #ff, DSRSENS = 0 COMM SET #ff, DTRFLOW = 0 COMM SET #ff, DTRLINE = 0 COMM SET #ff, XINPFLOW = 0 COMM SET #ff, XOUTFLOW = 0 COMM PRINT #ff, " "; 'Should respond with (temp) (ascii 9) (humidity) DO Bsize = COMM(#ff, RXQUE) 'Anything in the buffer? IF Bsize > 0 THEN 'if so... SLEEP 50 ' Pause for a tiny fraction of a second to fill buffer COMM RECV #ff, Bsize, Buffer ' Retrieve buffer, EXIT DO ' and we're done. ELSE 'if not... SLEEP 100 ' Pause for 1/10th of a second INCR ctr ' Increment counter IF ctr > 10 THEN EXIT DO 'Abort if more than a second has passed END IF LOOP COMM CLOSE #ff 'Terminate commnication 'Break apart data and convert to numerics Temperature = VAL(PARSE$(Buffer, CHR$(9), 1)) Humidity = VAL(PARSE$(Buffer, CHR$(9), 2)) IF LEN(Buffer) AND Temperature <> -88.88 THEN 'Valid data recieved (-88.88 is a com error) FUNCTION = 1 ' Success! ELSE 'Data receieved is invalid or incomplete FUNCTION = 0 ' Failed... END IF EXIT FUNCTION 'And we're done! fail: FUNCTION = -(ERR) 'Error codes: most are com port errors END FUNCTION