1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 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
|
Download lan380-send.bas
(7.7 KB , May 19, 2009)