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
#3
(26.05.2020, 11:57)admin Wrote: Since you have a log call between write and flush you basically have adding a random delay. Flush will discard all buffered data but the TV might not have sent the response completely.

You should remove flush after write and use read with timeout. Something like this:
Code:
res, err = port:read(100, 1)
log(res, err)
Hello
I have already successfully communicated with the TV and it responds perfectly to commands.
I have removed the logs and adjusted the number of bytes read and the timeout.
I have added a delay after the power on command, as the TV takes time to power on and then a command to select HDMI 1 input.
I have also added the control of the correct answer I have incorrect, repeated in both commands. Would there be a way to put it only once and it would work for both commands? In the style of an external function or an internal jump and that returns the value, and thus not repeat it for each command.
Thank you very much Admin for your help.
a greeting
Code:
value = event.getvalue() -- Se obtiene el valor del objeto de escenas y se guarda en la variable value
if (value == 3) then -- Si el valor de la variable value es igual a 3 (número de escena), entonces enviamos el comando de encendido
  if not port then   -- Si el puerto no está ya abierto entonces se abre y configura
    require('serial') --Es necesario incluir la librería de las funciones de comunicación serie
    port = serial.open('/dev/RS232', { -- Configuración y apertura del puerto serie
          baudrate = 9600,
          databits = 8,
             stopbits = 1,
          parity = 'none',
          duplex = 'full'
            })
    port:flush() --  Limpiar bytes no leídos o enviados
    TV_ON = string.char(0x6B, 0x61, 0x20, 0x30, 0x31, 0x20, 0x30, 0x31, 0x0D) -- Escribir comando encender TV en la variable "TV_ON"
    port:write(TV_ON) -- Escribir contenido variable "TV_ON" el puerto serie para encender la TV
   end
  ack = port:read(10,2) --Leer la confirmación
    if ack == 'a 01 OK01x' -- Si la respuesta de la TV es OK, entonces
    then alert('TV encendida') -- Activar objeto estado TV encendida
     elseif ack == 'a 01 NGx'
    then alert('TV error comunicación')
    end
  sleep(5) -- Esperamos a que la TV se encienda.
  hdmi1 = string.char(0x78, 0x62, 0x20, 0x30, 0x31, 0x20, 0x39, 0x30, 0x0D) -- Escribir comando cambio entrada HDMI 1
  port:write(hdmi1) -- Escribir contenido variable en el puerto serie para cambio entrada HDMI 1
  ack = port:read(10,2) --Leer la confirmación
    if ack == 'a 01 OK01x' -- Si la respuesta de la TV es OK, entonces
    then alert('HDMI 1 Seleccionado') -- Activar objeto estado TV encendida
     elseif ack == 'a 01 NGx'
    then alert('TV error comunicación')
    end
  port:close() -- Se cierra el puerto serie.   
end
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: