This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

RS232 read problem
#1
Hello

I have written the following script to control the power off of an LG TV using the RS232 serial port.

The scrip works perfectly and the TV turns on, the command changes the value to turn it off and on, to write without problem. I have a log of the message that is sent and it is correct.


Code:
-- Se obtiene el valor del objeto de escenas y se guarda en la variable value
value = event.getvalue()

-- si el valor de la variable value es igual a 3 (número de escena), entonces enviamos el comando de encendido
if (value == 3) then
 
  -- Es necesario incluir la librería de las funciones de comunicación serie
    if not port then
    require('serial')
    -- Configuración y apertura del puerto serie
    port = serial.open('/dev/RS232', {
  baudrate = 9600,
  databits = 8,
  stopbits = 1,
  parity = 'none',
  duplex = 'full'
    })
    --  Limpiar bytes no leídos o enviados
    port:flush() 
    -- Escribir datos en el puerto serie para encender la TV
    msg = string.char(0x6B, 0x61, 0x20, 0x30, 0x31, 0x20, 0x30, 0x31, 0x0D)
    port:write(msg)
    log (msg) -- Esta línea permite ver el comando en el registro para comprobar que se está enviando
   port:flush()
    res = port:read(10)
    log (res) -- Esta línea permite ver el comando en el registro para comprobar que se está recibiendo
   port:flush()
    port:close()
    end
end


Now what I want is to manage the confirmation of messages, according to the TV manual, the response has the following format:
[Comando 2][Espacio][Set ID][OK][Dato][x] --- for correct reception.
[b][Comando 2][Espacio][Set ID][NG][Dato][x] --- for incorrect reception.[/b]


This is when the problems come, I read 10 bytes from the port, the question is that the answers obtained in the log appear bytes from previous answers, so I don't know how to discriminate them.

This is the result of the log, after sending several commands to turn on in a row.

RS232 26.05.2020 13:30:03
* string: ka 01 01


RS232 26.05.2020 13:30:03
* string: 01 NGxa 01


RS232 26.05.2020 13:30:35
* string: ka 01 01


RS232 26.05.2020 13:30:35
* string:  OK01xa 01




I have tried the flush function before and after reading but nothing.

Or do I have to reset the variable "res" before reading the port?
Any ideas?


Thank you.
Reply


Messages In This Thread
RS232 read problem - by JRP - 26.05.2020, 11:43
RE: RS232 reed problem - by admin - 26.05.2020, 11:57
RE: RS232 reed problem - by JRP - 01.06.2020, 09:10

Forum Jump: