Hello! I am sorta new to KAREL(only have read the manual about 50times trying to figure this out ) but am getting quite familiar since I have been working with trying to get a system up an running.
I have an established socket messaging situation where I need to send a Tag (4 byte), Length (4byte) and Value (n* byte determined by length) (TLV COMMAND) to a port. I have already established connection and have data passing back and forth. The issue is that the pc that is taking the data from the message is reading it as little endian format.
For example, I want to send the no. 20401. As hex that number is 4FB1. But the way the pc needs to see it is: B1 4F 00 00 (little endian).
So, the question is how to I send a code of 20401(00 00 4F B1 -> B1 4F 00 00), length of 14 (00 00 00 0E-> 0E 00 00 00) through a FILE WRITE send an array of bytes or a structure of the whole TLV command through that port?
In python and even ABB, you can pack bytes and then send it at once, but when I pack into a variable - array1: ARRAY[4] of INTEGER, I cannot send the entire array at once without my TP giving me an error of INTP-319 Invalid type code. The file type I am sending through doesnt seem to like the array.
Code is below: (connection is established and proven)
--------------------------------------------------------------------------------------------------
ROUTINE convert_to_little_endian(INPUT : INTEGER)
VAR
temp : INTEGER
BEGIN
temp = INPUT
little_endian[1] = temp AND 255
temp = temp DIV 256
little_endian[2] = temp AND 255
temp = temp DIV 256
little_endian[3] = temp AND 255
temp = temp DIV 256
little_endian[4] = temp AND 255
END convert_to_little_endian
ROUTINE array_to_string
VAR
temp_str : STRING[5]
BEGIN
buffer = ''
FOR i = 1 TO 4 DO
CNV_INT_STR(little_endian[i], 3, 0, temp_str)
buffer = buffer + temp_str + ' '
ENDFOR
END array_to_string
BEGIN
SET_FILE_ATR(file_var1, ATR_IA)
SET_VAR(entry,
'*SYSTEM*','$HOSTC_CFG[1].$SERVER_PORT',50000,status)
MSG_DISCO('C1:',status)
-- Connect the tag
WRITE('Connecting..',CR)
MSG_CONNECT('C1:',status)
WRITE(' Connected! Connect Status = ',status,CR)
loop1 = TRUE
IF status = 0 THEN
WHILE loop1 = TRUE DO
CLOSE FILE file_var1
WRITE('Opening File 1..',CR)
OPEN FILE file_var1('rw','C1:')
convert_to_little_endian (20401)
WRITE ('file_var1 was just opened with data=',CR)
WRITE file_var1(little_endian,CR) -- Testing 0550 as a number to send and get it swapped in order
WRITE (little_endian,CR) -- Testing 0550 as a number to send and get it swapped in order