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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
rem lan380-receive.bas
rem
rem $Revision: 1.5 $
rem $Date: 2006/01/23 18:50:59 $

rem Gets Data from 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/received

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_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"

REG &SAMPLE_NUM = &AUX1

// #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 download 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 ttp command
    case mSEND_TTP:
        #state = sWAIT_FOR_REPLY
        |ttp_sent = true
        print "TTP /dl/"+&UNIT_NAME+"/last " + &KEY + CHR(CR)
        
    endsel

case sWAIT_FOR_REPLY:
    if |RECEIVE_READY1 = true then
        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>
            if SERIAL_POINTER 1 = "<" then
                #temp = SERIAL_POINTER 1 // Sample number
                if #temp > &SAMPLE_NUM then
                    &SAMPLE_NUM = #temp
                    |new_sample = true
                    // The request returned new data, do something here:
                    if SERIAL_POINTER 1 = "L1=" then
                        &CH2=SERIAL_POINTER 1
                        &SETPOINT1 = &CH2
                    endif
                    if SERIAL_POINTER 1 = "L2=" then
                        &CH3=SERIAL_POINTER 1
                    endif
                    if SERIAL_POINTER 1 = "L3=" then
                        &CH4=SERIAL_POINTER 1
                    endif
                    write &SPACES
                    append "Got Sample " + &SAMPLE_NUM
                    append &SPACES
                else
                    |new_sample = false
                    write &SPACES
                    append "No new sample - " + &SAMPLE_NUM + " is the latest"
                    append &SPACES
                endif
                #state = sRESET // done with receiving data
            else
                #state = sERROR
                &ERR_MSG = &ERR_MALFORMED
            endif
        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 = "404" then // Data not found
            #state = sERROR
            &ERR_MSG = &ERR_NO_DATA
        elsif SERIAL_INPUT = "405" then // Unit not found
            #state = sERROR
            &ERR_MSG = &ERR_UNIT_NOT_FOUND
        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

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-receive.bas
(8.7 KB , March 31, 2009)