rem lan320-receive.bas rem rem $Revision: 1.4 $ rem $Date: 2006/01/23 18:50:59 $ rem Gets Data from a TexmateDSE server via TTP from a Tiger 320 with rem an Ethernet module on demand when the F1 button is pressed or rem when the Capture Pin is connected to Common. rem LED6 indicates that data is being sent rem The macro makes use of the XPorts modem mode to rem establish a connection to the TexmateDSE server. rem rem XPort modem mode set to 'Without Echo' rem rem Note: the IP and Gateway address of the Tiger have rem to be setup correctly in the Ethernet module // initialize messages DIM MSG[] = [ "", \ "Unauthorized", \ "Unit not found", \ "Data not found", \ "Bad request", \ "Server error", \ "Not implemented", \ "Communication timed out", \ "Unknown response from server", \ "Malformed response received", \ "Done", \ " "] // Error/Message index CONST ERR_UNAUTHORIZED = 1 CONST ERR_UNIT_NOT_FOUND = 2 CONST ERR_NO_DATA = 3 CONST ERR_BAD_REQUEST = 4 CONST ERR_SERVER_ERROR = 5 CONST ERR_NOT_IMPLEMENTED = 6 CONST ERR_TIMEOUT = 7 CONST ERR_UNKNOWN = 8 CONST ERR_MALFORMED = 9 CONST MSG_DONE = 10 CONST SPACES = 11 // #msg values CONST mNONE = 0 CONST mRESTART = 1 CONST mRESET_MODEM = 2 CONST mDISCONNECT = 3 CONST mCONNECT = 4 CONST mSEND_TTP = 5 // #state values CONST sIDLE = 0 CONST sSEND = 1 CONST sWAIT_FOR_REPLY = 2 // wait for reply CONST sWAIT = 3 // pauses for modem commands CONST sERROR = 4 CONST sRESET = 5 // serial setup CONST SLAVE_MODE = 0000 CONST MASTER_MODE = 0002 MEM &CODE3 = SLAVE_MODE MEM &STRING_LENGTH = 0 // replies have variable length MEM &STRING_CHARACTER = 13 // ttp terminator (CR) // reply timeout CONST TIMEOUT = 100 // 10.0 secs // there has to be a pause >1sec before and after the '+++' for disconnect CONST MODEM_TIMEOUT = 12 CONST MAX_RETRIES = 3 REG &SAMPLE_NUM = &AUX1 // Keeps value on power down //REG &SAMPLE_NUM = &VARIABLE_A_INT // Clears value on power down BITREG &USER_MEMORY_1 = [ |SEND_DATA ] MEM &USER_MEMORY_1 = 0x0001 // send messages //////////////////////////////////////////////////////////////////////////////// CUSTOMER_ID_MACRO: //////////////////////////////////////////////////////////////////////////////// write MSG[SPACES] append "LAN TexmateDSE download 320" append MSG[SPACES] END //////////////////////////////////////////////////////////////////////////////// RESET_MACRO: //////////////////////////////////////////////////////////////////////////////// #state = sIDLE #msg = mNONE #retries = 0 |ttp_sent = false &TIMER1 = 0 &CODE3 = SLAVE_MODE // so we can talk to the meter in ASCII mode END //////////////////////////////////////////////////////////////////////////////// F1_BUTTON_MACRO: //////////////////////////////////////////////////////////////////////////////// |UPDATE_DEMAND=true END //////////////////////////////////////////////////////////////////////////////// MAIN_MACRO: //////////////////////////////////////////////////////////////////////////////// select #state case sIDLE: |LED6 = off // send data when F1 button is pressed or capture pin is connected to common if |UPDATE_DEMAND = true or |CAPTURE_PIN = on then if |SEND_DATA = true then |UPDATE_DEMAND = false // Turn "off" F1 button &CODE3 = MASTER_MODE // terminate any connections before we start with a new one #msg = mRESTART #state = sSEND |ttp_sent = false &TIMER1 = 0 #retries = 0 write MSG[SPACES] append "Requesting data" append MSG[SPACES] endif endif case sSEND: if &CODE2 >= 0200 then // in fast mode we have to wait a little for the print command to finish #state = sWAIT endif |LED6 = on &TIMER1 = 0 print "" // reset serial buffer select #retries case MAX_RETRIES: write MSG[SPACES] append "Giving up after 3 attempts" append MSG[SPACES] #state = sRESET #msg = mNONE endsel select #msg // modem commands case mRESTART: // timer is already reset so we just wait before we send '+++' #state = sWAIT case mRESET_MODEM: // wait agagin after the '+++' #state = sWAIT print "+++" case mDISCONNECT: // wait again before we start a new connection #state = sWAIT print "ATH" + CHR(CR) // hang up connection case mCONNECT: #state = sWAIT // Connect to TexmateDSE server: print "ATDT192.168.1.100/9995" + CHR(CR) // actual data to post via ttp case mSEND_TTP: #state = sWAIT_FOR_REPLY |ttp_sent = true // TTP request to send to TexmateDSE Server: print "TTP /dl/unit1/last testkey" + CHR(CR) endsel case sWAIT_FOR_REPLY: if |RECEIVE_READY = true then #state = sSEND // Check response if SERIAL_INPUT = "OK" then // Reply in serial buffer should look like this: // OK <104 2006-01-16 15:50:50 L1=501.1V L2=505.3V L3=499.9V L4=498.8V> #temp = &RECEIVE_RESULT // Sample number is first numeric value if #temp > &SAMPLE_NUM then &SAMPLE_NUM = #temp |new_sample = true // The request returned new data, do something here: write MSG[SPACES] append "Got Sample " + &SAMPLE_NUM append MSG[SPACES] else |new_sample = false write MSG[SPACES] append "No new sample - " + &SAMPLE_NUM + " is the latest" append MSG[SPACES] endif #state = sRESET // done with receiving data elsif SERIAL_INPUT = "400" then // Bad request #state = sERROR #MSG_INDEX = ERR_BAD_REQUEST elsif SERIAL_INPUT = "402" then // Unauthorized #state = sERROR #MSG_INDEX = ERR_UNAUTHORIZED elsif SERIAL_INPUT = "405" then // Unit not found #state = sERROR #MSG_INDEX = ERR_UNIT_NOT_FOUND elsif SERIAL_INPUT = "500" then // Internal error #state = sERROR #MSG_INDEX = ERR_SERVER_ERROR elsif SERIAL_INPUT = "501" then // Not implemented #state = sERROR #MSG_INDEX= ERR_NOT_IMPLEMENTED else #state = sERROR #MSG_INDEX = ERR_UNKNOWN endif elsif &TIMER1 > TIMEOUT then // Timeout #state = sERROR #MSG_INDEX = ERR_TIMEOUT endif case sWAIT: if #msg >= mSEND_TTP and &TIMER1 > 0 then // delay new output to avoid aborting old message (only in fast mode) #state = sSEND elsif &TIMER1 > MODEM_TIMEOUT then // wait before and after the '+++' to terminate the connection if |ttp_sent = true then #state = sRESET else #msg = #msg + 1 #state = sSEND endif endif case sERROR: // terminate current connection #retries = #retries + 1 #msg = mRESTART #state = sSEND |ttp_sent = false write MSG[SPACES] append MSG[#MSG_INDEX] append MSG[SPACES] print "" // reset serial buffer case sRESET: print "" // reset serial buffer &CODE3 = SLAVE_MODE #msg = mNONE #state = sIDLE if #retries = MAX_RETRIES then #retries = 0 else write MSG[SPACES] append MSG[MSG_DONE] append MSG[SPACES] endif endsel END