Serial com ports rs-232 / USB HID lua modules

Plugins y todo lo relacionado para Autoplay Media Studio.
Releasing two lua modules to use serial (rs232/bluetooth/usb2serial...) and usb-hid (raw) suitable for most IoT devices like arduino, esp8266, raspberry pi... and interfacing any usb HID device such as arduino 32u, gamepads, controllers, inputs, and wide variety of HID usb devices, even with obscure protocols that can be reversed with wireshark/usbpcap.

Serial
file: serial.dll (100kb)
Source: https://gist.github.com/pabloko/e9c19f6 ... 3549cc1bb5
require('serial')
count = Serial.Enumerate()
Serial.Terminate()
name = Serial.GetPortName(id) --nil if invalid id
internal_name = Serial.GetInternalName(id) --nil if invalid id
id=Serial.FindPort(name)
Serial.Open(id, baud_rate, flow_control) -- Flow control PARITY_NONE, PARITY_EVEN, PARITY_ODD...
Serial.Close(id)
Serial.CloseAll()
written = Serial.Write(id, data) --data is a literal string
data = Serial.Read(id) --data is a literal string
data = Serial.ReadBlocking(id,timeout_ms) --data is a literal string
state = Serial.SetDtr(id, bool_state)
state = Serial.SetRts(id, bool_state)
...
HID
file: usbhid.dll (90kb)
Source: https://gist.github.com/pabloko/e9c19f6 ... 3549cc1bb5
require('usbhid')
table = HID.Enumerate(vid, pid) --use 0 for wildcard
--#table= table count, table[1]=first device... table[1].path = path of first device...
device = HID.Open(vid,pid,serial) --serial can be 0, check device not nil
device = HID.OpenPath(path) --check device not nil
written=HID.Write(device, data) --data is a literal string
data = HID.ReadTimeout(device, timeout_ms) --data is a literal string
data = HID.Read(device) --data is a literal string
err = SetNonBlocking(device, bool_state)
HID.Close(device)
...
DOWNLOAD
HIDE: ON
Hidebb Message Hidden Description
Great Job :pc:
interesante trabajo, gracias por compartirlo.
great job Pabloko as always... Thank you
thanks for share
Hi pabloko how are you?
Thanks for sharing this dll and i play around with it using esp8266,
however i have notice this scenario..
Once i connect the serial com to the specific com example 5:
i can send and receive data on but but when i leave it connected for couple of minutes and try to send data again the device wont response anymore.. i need to close and connect it again to have the communications..

Do you have any idea how to avoid this lose of communication to the device?

Thank you.

Sendai
sendai escribió:
05 Jul 2021 06:33
i can send and receive data on but but when i leave it connected for couple of minutes and try to send data again the device wont response anymore.. i need to close and connect it again to have the communications..
Hmm quite strange... usually sockets timeout if theres no traffic in some amount of time, but this is usually true for network sockets, it could be the case here too. You could implement a heartbeat data packet to keep the connection opened and ping the device each few seconds
Pabloko escribió:
06 Jul 2021 10:57
sendai escribió:
05 Jul 2021 06:33
i can send and receive data on but but when i leave it connected for couple of minutes and try to send data again the device wont response anymore.. i need to close and connect it again to have the communications..
Hmm quite strange... usually sockets timeout if theres no traffic in some amount of time, but this is usually true for network sockets, it could be the case here too. You could implement a heartbeat data packet to keep the connection opened and ping the device each few seconds
Hi Pabloko thanks for your time to response.
i just play around with the serial.dll and used esp8266 with some relay on it..

here is the code:

tSerial = Serial.Open(port, 9600, PARITY_NONE)
--data = Serial.Read(3)
data = Serial.ReadBlocking(port,100)

if tSerial == 1 then
written = Serial.Write(port, "1")
data = Serial.ReadBlocking(port,100)
Input.SetText("Input1", data)
tTest = 0
Page.StartTimer(1000, 1)
else
Input.SetText("Input1", tSerial)
end


i also try to put timer in every second to send
written = Serial.Write(port, "0")

but after few minutes the communication will become lost..

when i try to click a button to write again it will not response anymore..
sendai escribió:
06 Jul 2021 12:53
but after few minutes the communication will become lost..
when i try to click a button to write again it will not response anymore..
So, the first thing to note here, is that strings of data are treated as ascii text, so your firmware should also handle ascii text, but if not, you can always use lua string methods like

string.char(1,2,3,4...) to generate an ascii string literal to send, or string.byte(data,pos) to read a byte from a recieved string.

second, if device got disconnected after some amount of time, that could mean a lot of things, for example youre sending packets but not reading on the device, filling the txbuffer to overflow, or could mean that device isnt sending struff and its stuck when you use readblocking that can infinitely block (try using read with timeout) or maybe the watchdog timer is reset on the esp8266 from some memleak bug in the fw...

try looking on windows error log and checking if theres some meaningful info there, and if the same thing happen using other serial apps other than the plugin.
Also, as general walktrought for most of old windows apis, you can try to wrap your calls between calls to SetLastError(0) / hr = GetLastError() and checking the result codes here: https://docs.microsoft.com/en-us/window ... rror-codes

DLL.CallFunction("Kernel32.dll", "SetLastError", "0", DLL_RETURN_TYPE_INTEGER, DLL_CALL_CDECL);
--Your code
err = DLL.CallFunction("Kernel32.dll", "GetLastError", "", DLL_RETURN_TYPE_INTEGER, DLL_CALL_CDECL);
Thank You Pablo
i tried to get back to Arduino Software and run the Serial Monitor i send data of 1 and leave almost 1 hr gap then send data again and the device still response..

so possible no problem with my device and fw.

also in the code of arduino i convert the incoming string to char and all works fine..

im still stuggeling to locate the error in AMS because when communication lost and i run the dll getlast error only result 0.

is there a way we can flush all data every after data is send to the device? Sorry for the term i used..

i get the value from this
written = Serial.Write(port, sData)

if return 1 of the write is successful

and when no more communication it result 0..

after the result is 0 i try to Serial.Close(port)
and execute the Serial.Open(port, 9600, PARITY_NONE) but no luck...
it will back to normal once i exit the application and open the port again...

thanks..
sendai escribió:
07 Jul 2021 11:07
and execute the Serial.Open(port, 9600, PARITY_NONE) but no luck...
it will back to normal once i exit the application and open the port again...
thanks..
Im not sure about what is causing the connection to stuck, but the library im using is quite straight using windows API to command the serial port.

It seems the internal HANDLE of the port is getting corrupted/lost after some time, the library uses <SetCommTimeouts> function to apply infinite timeout, that is tuned every time you call ReadBlocking and reverted back to infinite.

I can only suggest to keep sending data periodically on both sides to keep the socket open.

You can find full source used here: https://github.com/mrh1997/rs232/blob/a ... s232-win.c
You can also refer to this documentation: https://docs.microsoft.com/en-us/window ... mmtimeouts

If you find whats wrong, i can recompile it for you. Im actually only using the HID plugin for now
Hi @Pabloko thanks for your response..

Maybe this <SetCommTimeouts> cause the port being lost, but i am not really sure about it..

here is some changes i made in the code..

in the device side i remove all the Serial.print()
so that it will not print anything back to AMS.

and in the AMS side i also remove the Serial.Read(port) and Serial.ReadBlocking(port, 500) so it means no more reading from the software side..

the data is from software to device > written = Serial.Write(port, 1) and i put a timer to keep it sending to the device in every seconds or 1000:

if e_ID ==1 then
written = Serial.Write(port, 0)
end

if have also this code for checking

if written == 0 then
Dialog.Message("Notice", "SERIAL WAS DISCONNECTED", MB_OK, MB_ICONSTOP, MB_DEFBUTTON1);
Serial.Close(port)
tSerial = Serial.Open(port, 9600, PARITY_NONE)
Serial.Write(port, sData)
Application.ExitScript()
end

but still after couple of minutes the port becomes lost.

other thing i tried is to remove the timer that keep sending data and still the post becomes lost.


Sendai..
I dont know whats wrong, but i have an arduino so if send me an INO and APZ i will try to reproduce here and debug the issue, altough it may take me some time since im kinda busy
Hi pabloko.. i was planning to purchase also and arduino nano for testing, for now i have only NodeMCU esp8266.
sure can send you the apz this is just only test where no other codes except the serial functions...

i can send you the apz link in your inbox..

Thanks

Sendai
The file you sent seems to be removed

Código: Seleccionar todo

The file you requested has been blocked for a violation of our Terms of Service.
Also ill need the .ino i have some nodemcus and arduinos here to test.

The esp8266 for me has been proven to be extremely reliable, one of my devices has been running non-stop since 3-4 years.

In fact i can throw here a small tutorial about creating your own all-in-one IR remote for your home automation for 2$ :)

First of all get from the trash a npn transistor, an IR led and optionally an IR transistor, you can find all of these items on every cheap electronic shit you can find on the trash. Get a small pcb and your esp8266
ImagenImagen

Clean the parts and use a sharpie to poorly draw the routes according the schematic, i prefer to do my own acid with 1/3 peroxide and 2/3 salfuman, solder everything and you will get something like this.
Imagen
I dont even put the resistor or capacitors as its working fine without them :))
Imagen

Im using this library to handle the hardware: https://github.com/crankyoldgit/IRremoteESP8266
In my case, i control an LG tv and a Haier AC, when ive made it the haier protocol was not yet reversed and learned to reverse any remote with this device.

Tv remotes are far easy, they transmit a different value for each key (usually) and most of them uses the NEC protocol. The library is able to tell wich protocol is being sent from the remote to te RX transistor. With AC controllers, usually a longer line of data is sent, and it contain most of the settings there. The best is to capture each mode and record what setting is changed, then start finding differences in the chain.

For example this is the recording from the TV remove key <ON>
Imagen

For the AC protocol, create an spreadsheet and write a binary representation of data
Imagen
You can check wich column changes and start knowing wich column goes to wich setting, like temperature, power, etc... usually those codes also contain a CRC validation. its useful to mark 1's to check similar lines. Reversing a protocol like this can take minutes with this device, wich is why ive made two, one for my house and another for my toolbox.

To use it, you can setup a simple web server to use any device to control any IR stuff.
Imagen

Later of this, ive added a SVG based UI wich looks nice on the phone.
Imagen

Those small projects are fun and take so little time, but the result is impressive as it never failed or stopped working since i embedded it on the wall, ive even had planned to buy a device that does this, but as this seems like its never going to die, i guess ill keep adding features to it.

For example ive hacked an app that broadcast notifications when commercial breaks on tv channels and the remote keep track of the channel and check if channel goes to commercials, silencing the ads or switching to music in aux and then recover when the program is back.
Waahhh thats sounds cool pabloko you are really impressive no wonder..
but for me i haven't try any other project in arduino yet rather than this serial dll and eps8266 maybe in some time, i only learned by my self and in here... so basically no deep base knowledge hahhaah... i found that automations and other stuff is cool thats why i am very interesting to make it work. once it will work properly then maybe i am interesting to send you some bucks for your ir automation project.. i like that also..

anyway sorry i forget to include the ino file..

i will resent it in zip format to insure it will not remove


Sendai
looking at your firmware, youre expecting non-ascii data, so try to create the payload like this
string.char(0x01)
string.char(0x00)
string.char(0x01, 0x02)

ySerial(string.char(0x01))
This generates a string literal containing the bytes, and can contain arbitrary zeros in it.

Maybe this is why some leftover bytes could be causing software disconnect.

Apart from that, ill try to replicate when i have some time
Muchas Gracias