I have a device that sends ASCII error codes to BRX via Modbus TCP. Then I have a CSV file that references said error code to a error description. Currently, I am hosting this CSV on a webserver, sending a HTTPCMD containing error code, cross referencing this to error description, and returning that to the BRX. Works great.
However, due to changing network architecture, I would like to do this all on the BRX, have the lookup table on the SDCard, and use FILEREAD to do the looking up. How would I go about doing this?
How large is this database? Is it just a few hundred error codes and 80 characters per error message? You can do that easily with just 2 data blocks, one with an error code number and one with strings, up to 80 characters long, where the index of each correspond to each other. No need for the SD Card/File system (unless you need it to LOAD the data on first scan). If it is static, stick it in code, don't bother with the SD card. Initialize the 2 blocks in $tFirstScan via ladder logic, definitely no SD card needed there.
To look up the error code text, use FOR/NEXT loop to find error code in the error code block, get that V index, and use that index in the string error text table.
STRPRINT ErrorText "Unknown Error:" ErrorCode // assume ErrorCode is not in our table
// loop through ErrorCodeTable, looking for ErrorCode
FOR V42 0 199
STRE ErrorCode ErrCodeTable[V42] // found it?
STRCOPY ErrorTable[V42] ErrorText // copy it to ErrorText
BREAK // done! break the FOR/NEXT loop
NEXT
// ErrorText will either be assigned the found error code, or it will be "Unknown Error
RETURN
If the error codes are sequential, say 0..199 (i.e. they are not sparse), you do not even need the error code table, the error code IS the string table V array index.
However, if there are HUGE number of error codes (more than 500) with HUGE error messages (some with more than 500 characters), then the SD Card mechanism will be necessary, parsing the text. But there are ways to implement a database lookup function in a PLC (e.g. have an Error code table with a corresponding FILE OFFSET DWORD table, where the lookup doesn't return a string, it returns the INDEX INTO THE FILE WHERE THAT ERROR TEXT EXISTS. That's sort of what databases do on Server (table "keys" => corresponding data locations).