rem lan380-send.bas rem Posts Data to a TexmateDSE server via TTP from a Tiger 380 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 REG &ERR_MSG = &TEXT_VARIABLE8 REG &SPACES = &TEXT_VARIABLE7 REG &ERR_UNAUTHORIZED = &USER_TEXT1 REG &ERR_UNIT_NOT_FOUND = &USER_TEXT2 REG &ERR_NO_DATA = &USER_TEXT3 REG &ERR_BAD_REQUEST = &USER_TEXT4 REG &ERR_SERVER_ERROR = &USER_TEXT5 REG &ERR_NOT_IMPLEMENTED = &USER_TEXT6 REG &ERR_TIMEOUT = &USER_TEXT7 REG &ERR_UNKNOWN = &USER_TEXT8 REG &ERR_MALFORMED = &USER_TEXT9 REG &MSG_DONE = &USER_TEXT10 REG &UNIT_NAME=&USER_TEXT11 REG &KEY=&USER_TEXT12 REG &SERVER=&USER_TEXT13 // initialize text registers MEM &ERR_UNAUTHORIZED = "Unauthorized" MEM &ERR_UNIT_NOT_FOUND = "Unit not found" MEM &ERR_NO_DATA = "Data not found" MEM &ERR_BAD_REQUEST = "Bad request" MEM &ERR_DUPLICATE_SAMPLE = "Duplicate sample" MEM &ERR_SERVER_ERROR = "Server error" MEM &ERR_NOT_IMPLEMENTED = "Not implemented" MEM &ERR_TIMEOUT = "Communication timed out" MEM &ERR_UNKNOWN = "Unknown response from server" MEM &ERR_MALFORMED = "Malformed response received" MEM &MSG_DONE = "Done" // #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 &SERIAL_MODE1 = SLAVE_MODE MEM &STRING_LENGTH1 = 0 // replies have variable length MEM &STRING_CHARACTER1 = 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 BITREG &USER_MEMORY_1 = [ |SEND_DATA ] MEM &USER_MEMORY_1 = 0x0001 // send messages //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // The following section needs to be entered by the user. Each string can // be up to 31 characters long. MEM &UNIT_NAME = "unit1" // Unit Name for Data upload/retrieval MEM &KEY = "testkey" // Unit Key for Data upload/retrieval MEM &SERVER = "192.168.1.100/9995" // Your TexmateDSE Server IP and Port //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //////////////////////////////////////////////////////////////////////////////// CUSTOMER_ID_MACRO: //////////////////////////////////////////////////////////////////////////////// write &SPACES append "LAN TexmateDSE upload 380" append &SPACES END //////////////////////////////////////////////////////////////////////////////// RESET_MACRO: //////////////////////////////////////////////////////////////////////////////// #state = sIDLE #msg = mNONE #retries = 0 |ttp_sent = false &TIMER1 = 0 &SERIAL_MODE1 = SLAVE_MODE // so we can talk to the meter in ASCII mode &SPACES = " " // spaces for scrolling text messages 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 &SERIAL_MODE1 = MASTER_MODE // terminate any connections before we start with a new one #msg = mRESTART #state = sSEND |ttp_sent = false &TIMER1 = 0 #retries = 0 write &SPACES append "Sending data" append &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 &SPACES append "Giving up after 3 attempts" append &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 "ATDT" + &SERVER + CHR(CR) // actual data to post via ttp case mSEND_TTP: #state = sWAIT_FOR_REPLY |ttp_sent = true print "TTP /up/" + &UNIT_NAME + "/<" print &YEAR + "-" + &MONTH + "-" + &DATE + " " + &HRS_MIN_SEC print ";" + &CH1 + ";" + &CH2 + ";" + &CH3 + ";" + &CH4 + "> " + &KEY+CHR(CR) endsel case sWAIT_FOR_REPLY: if |RECEIVE_READY1 = true then #state = sSEND // Check response if SERIAL_INPUT = "OK" then // The upload went well, we can exit now #state = sRESET elsif SERIAL_INPUT = "400" then // Bad request #state = sERROR &ERR_MSG = &ERR_BAD_REQUEST elsif SERIAL_INPUT = "402" then // Unauthorized #state = sERROR &ERR_MSG = &ERR_UNAUTHORIZED elsif SERIAL_INPUT = "405" then // Unit not found #state = sERROR &ERR_MSG = &ERR_UNIT_NOT_FOUND elsif SERIAL_INPUT = "406" then // Duplicate Sample #state = sERROR &ERR_MSG = &ERR_DUPLICATE_SAMPLE elsif SERIAL_INPUT = "500" then // Internal error #state = sERROR &ERR_MSG = &ERR_SERVER_ERROR elsif SERIAL_INPUT = "501" then // Not implemented #state = sERROR &ERR_MSG = &ERR_NOT_IMPLEMENTED else #state = sERROR &ERR_MSG = &ERR_UNKNOWN endif elsif &TIMER1 > TIMEOUT then // Timeout #state = sERROR &ERR_MSG = &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 &SPACES append &ERR_MSG append &SPACES print "" // reset serial buffer case sRESET: print "" // reset serial buffer &SERIAL_MODE1 = SLAVE_MODE #msg = mNONE #state = sIDLE if #retries = MAX_RETRIES then #retries = 0 else write &SPACES append &MSG_DONE append &SPACES endif endsel END