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
rem lan320-send.bas

rem Posts Data to 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", \
"Duplicate sample", \
"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_DUPLICATE_SAMPLE 5
CONST ERR_SERVER_ERROR = 6
CONST ERR_NOT_IMPLEMENTED = 7
CONST ERR_TIMEOUT = 8
CONST ERR_UNKNOWN = 9
CONST ERR_MALFORMED = 10
CONST MSG_DONE = 11
CONST SPACES = 12

// #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

BITREG &USER_MEMORY_1 = [ |SEND_DATA ]
MEM &USER_MEMORY_1 = 0x0001   // send messages


////////////////////////////////////////////////////////////////////////////////
CUSTOMER_ID_MACRO:
////////////////////////////////////////////////////////////////////////////////
write MSG[SPACES]
append "LAN TexmateDSE upload 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 /up/unit1/<"
        print &YEAR + "-" + &MONTH + "-" + &DATE + " "
        print &HOURS + ":" + &MINUTES + ":" + &SECONDS
        print ";" + &CH1 + ";" + &CH2 + ";" + &CH3 + ";" + &CH4 + "> testkey" + CHR(CR)
        
    endsel

case sWAIT_FOR_REPLY:
    if |RECEIVE_READY = 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
            #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 = "406" then // Duplicate sample
            #state = sERROR
            #MSG_INDEX = ERR_DUPLICATE_SAMPLE
        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

Download lan320-send.bas
(6.9 KB , May 19, 2009)