Author Topic: Act on response from usb to serial device command.  (Read 810 times)

Offline cybrmage

  • Hero Member
  • *****
  • Posts: 1068
  • Karma: +106/-5
Re: Act on response from usb to serial device command.
« Reply #30 on: September 10, 2017, 05:26:19 am »
Although, technically, your proposed code WOULD open a TCP/IP connection that would remain open.... It would cause a number of problems...
1) The connection can close due to a timeout, causing the send function to fail.
2) Any unsolicited data received from the device will cause the receive buffer to be partially filled when the send function is called, causing the received data to be out of sync with the command being sent.
3) If unsolicited data is received, the receive buffer can fill with data before the buffer is (partially) emptied with the socket:receive() function, which can cause the connection to close OR (potentially) for the LuaUPnP engine to crash.

If you want to have a continuous connection to a device, you should really create a plugin so that you can use the LuaUPnP built in IO handling mechanism.
EVL3Vista - Wink Connect - Caseta Connect - Venstar Colortouch - WiFi UDP Switch Controller - Broadlink RM - MyQGateway

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Act on response from usb to serial device command.
« Reply #31 on: September 10, 2017, 03:42:49 pm »
Many thanks for that.

Based on my level of ability, it looks like I've peaked for now and will just send isolated requests etc. :)

As a side point - Octava who make the HDMi Matrix Swithcer this relates to came back to me with an explanation of the "Status Code" numbering structure (see attached).

With that information it would allow me to know the status of the switch.  This is likely overkill for my needs but might be a good learning step to do.  I assume I would grab each number in order, and assess it against an array.
« Last Edit: September 10, 2017, 03:47:30 pm by parkerc »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Act on response from usb to serial device command.
« Reply #32 on: September 12, 2017, 01:08:20 pm »
If we have the code to capture the 15 byte number (presented a value after the word) "Status Code:"

Code: [Select]
   local data, rerr = c:receive(205)
   local result = data: match "Status code:(%d+)" or "error"
   print ("Receive:", data, rerr)
   print ("" ..result)

Looking at the Lua guide on patterns, there does not seem to be anything that allows me to extract a specific digit from a long number e.g the 3rd or 4th, I had hoped something like this would do it, but no.

Code: [Select]
-- extract second digit from result
digit1 = string.char(result .. ,1)
digit2 = string.char(result .. ,2)
-- etc to 15

I'm thinking I am getting my use of "strings" and numbers mixed up, but various Good searches have returned zilch...


Offline akbooer

  • Master Member
  • *******
  • Posts: 5274
  • Karma: +227/-67
  • "Less is more"
Re: Act on response from usb to serial device command.
« Reply #33 on: September 12, 2017, 01:36:13 pm »
You can pull anything you like out of a string.  For example, the first three (contiguous) digits...

Code: [Select]
local a,b,c = string.match (yourString, "(%d)(%d)(%d)")

...end up in variables a,b,c.

I really do recommend you try reading the manual, however hard you find it.  Otherwise, your programming will always remain just guesswork... and there are an awful lot of wrong combinations!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: Act on response from usb to serial device command.
« Reply #34 on: September 12, 2017, 02:35:21 pm »
I'm with you :-) , and I try to read what I can, but without a career change and considering all the other stuff I have to read, I can only focus on key areas that relate to an interest or something I want to do in HA - but I'm getting there. (I have some time off at the moment hence the blitz of updates)

For this particular item - I was working through the online manual and read through http://lua-users.org/wiki/PatternsTutorial but nothing seemed to allow me to extract a digit at a particular point in number e.g the 1st , 2nd or 3rd digit.

I looked at something like local a,b,c = string.match (yourString, "(%d)(%d)(%d)") but there is no way to designate in the word or number where to look for the digit.

Offline jswim788

  • Hero Member
  • *****
  • Posts: 540
  • Karma: +30/-2
Re: Act on response from usb to serial device command.
« Reply #35 on: September 12, 2017, 02:52:54 pm »
Look for string.sub on this page: http://lua-users.org/wiki/StringLibraryTutorial.  It can give you a substring starting at a specific location and length.  But I think you'll find the match method to be cleaner - you can probably find a way to get it to find the location you want in the string and extract as needed.