Skip to content

Commit b555645

Browse files
committed
allow setting read/write timeout
1 parent 2345a78 commit b555645

3 files changed

Lines changed: 55 additions & 17 deletions

File tree

docs/libraries/asyn-library.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,28 @@ port.OutTerminator = "\r\n"
412412
print(port.OutTerminator)
413413
```
414414

415+
### client.ReadTimeout
416+
---
417+
418+
Get or set the read timeout in seconds for this client. Used by
419+
`:read()` and `:writeread()`. Default is 1.0.
420+
421+
```lua
422+
port.ReadTimeout = 5.0
423+
print(port.ReadTimeout)
424+
```
425+
426+
### client.WriteTimeout
427+
---
428+
429+
Get or set the write timeout in seconds for this client. Used by
430+
`:write()`. Default is 1.0.
431+
432+
```lua
433+
port.WriteTimeout = 0.5
434+
print(port.WriteTimeout)
435+
```
436+
415437
### client[addr]
416438
---
417439

docs/libraries/bytestream-library.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ luaAddModule("$(LUA)") -- from iocsh, or luaAddModule(LUA) from Lua
3030
local bs = require("bytestream")
3131
```
3232

33-
Dependencies (lpeg and asyn) are loaded lazily on first use -- requiring
34-
bytestream does not force asyn to load until a client is created.
35-
36-
All functions that encounter errors raise them via `error()`. When used from
37-
DTYP "lua" device support, raised errors are caught by `lua_pcall` and mapped
38-
to record alarm states automatically.
39-
40-
4133
Format Specifiers
4234
-----------------
4335

@@ -308,7 +300,7 @@ local val = dev:write("MEAS?"):read("%f")
308300
| ... | varies | Values to format (only when using format mode). |
309301

310302
The `OutTerminator` property is appended automatically by the underlying
311-
asyn port -- do not include terminators in the write data.
303+
asyn port.
312304

313305
<br>
314306

@@ -369,12 +361,15 @@ The following properties are delegated to the underlying `asyn.client`:
369361
| - | - | - |
370362
| `InTerminator` | string | Get or set the input end-of-string terminator. |
371363
| `OutTerminator` | string | Get or set the output end-of-string terminator. |
364+
| `ReadTimeout` | number | Get or set the read timeout in seconds (default 1.0). |
365+
| `WriteTimeout` | number | Get or set the write timeout in seconds (default 1.0). |
372366
| `portName` | string | Read-only. The port name this client is connected to. |
373367
| `addr` | number | Read-only. The address this client is connected to. |
374368

375369
```lua
376370
dev.OutTerminator = "\r\n"
377371
dev.InTerminator = "\r\n"
372+
dev.ReadTimeout = 5.0
378373
print(dev.portName) -- "SERIAL1"
379374
```
380375

luaApp/src/libs/lasynlib.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ typedef struct {
764764
asynOctetClient* client;
765765
char portName[64];
766766
int addr;
767+
double readTimeout;
768+
double writeTimeout;
767769
} ClientUD;
768770

769771
static ClientUD* check_clientud(lua_State* state, int idx)
@@ -792,20 +794,23 @@ static void push_clientproxy(lua_State* state, const char* portName, int addr, c
792794
static int l_client_read(lua_State* state)
793795
{
794796
ClientUD* ud = check_clientud(state, 1);
797+
ud->client->setTimeout(ud->readTimeout);
795798
return asyn_read(state, ud->client);
796799
}
797800

798801
static int l_client_write(lua_State* state)
799802
{
800803
ClientUD* ud = check_clientud(state, 1);
801804
const char* data = luaL_checkstring(state, 2);
805+
ud->client->setTimeout(ud->writeTimeout);
802806
return asyn_write(state, ud->client, data, strlen(data));
803807
}
804808

805809
static int l_client_writeread(lua_State* state)
806810
{
807811
ClientUD* ud = check_clientud(state, 1);
808812
const char* data = luaL_checkstring(state, 2);
813+
ud->client->setTimeout(ud->readTimeout);
809814
return asyn_writeread(state, ud->client, data, strlen(data));
810815
}
811816

@@ -911,6 +916,10 @@ static int l_client_index(lua_State* state)
911916
if (strcmp(key, "portName") == 0) { lua_pushstring(state, ud->portName); return 1; }
912917
if (strcmp(key, "addr") == 0) { lua_pushinteger(state, ud->addr); return 1; }
913918

919+
/* Timeouts */
920+
if (strcmp(key, "ReadTimeout") == 0) { lua_pushnumber(state, ud->readTimeout); return 1; }
921+
if (strcmp(key, "WriteTimeout") == 0) { lua_pushnumber(state, ud->writeTimeout); return 1; }
922+
914923
/* Terminators */
915924
if (strcmp(key, "InTerminator") == 0)
916925
{
@@ -948,6 +957,14 @@ static int l_client_newindex(lua_State* state)
948957
const char* eos = lua_tostring(state, 3);
949958
ud->client->setOutputEos(eos, strlen(eos));
950959
}
960+
else if (strcmp(key, "ReadTimeout") == 0)
961+
{
962+
ud->readTimeout = luaL_checknumber(state, 3);
963+
}
964+
else if (strcmp(key, "WriteTimeout") == 0)
965+
{
966+
ud->writeTimeout = luaL_checknumber(state, 3);
967+
}
951968

952969
return 0;
953970
}
@@ -960,6 +977,8 @@ static void push_clientproxy(lua_State* state, const char* portName, int addr, c
960977
strncpy(ud->portName, portName, sizeof(ud->portName) - 1);
961978
ud->portName[sizeof(ud->portName) - 1] = '\0';
962979
ud->addr = addr;
980+
ud->readTimeout = DEFAULT_TIMEOUT;
981+
ud->writeTimeout = DEFAULT_TIMEOUT;
963982

964983
{
965984
char errbuf[256] = { '\0' };
@@ -997,14 +1016,16 @@ static void push_clientproxy(lua_State* state, const char* portName, int addr, c
9971016
lua_pushstring(state, ".addr -- address (property)"); lua_rawseti(state, -2, 2);
9981017
lua_pushstring(state, ".InTerminator -- get/set input terminator"); lua_rawseti(state, -2, 3);
9991018
lua_pushstring(state, ".OutTerminator -- get/set output terminator"); lua_rawseti(state, -2, 4);
1000-
lua_pushstring(state, ":read()"); lua_rawseti(state, -2, 5);
1001-
lua_pushstring(state, ":write(data)"); lua_rawseti(state, -2, 6);
1002-
lua_pushstring(state, ":writeread(data)"); lua_rawseti(state, -2, 7);
1003-
lua_pushstring(state, ":flush()"); lua_rawseti(state, -2, 8);
1004-
lua_pushstring(state, ":trace(mask) -- error=0x1, device=0x2, filter=0x4, driver=0x8, flow=0x10, warning=0x20"); lua_rawseti(state, -2, 9);
1005-
lua_pushstring(state, ":traceio(mask) -- nodata=0x0, ascii=0x1, escape=0x2, hex=0x4"); lua_rawseti(state, -2, 10);
1006-
lua_pushstring(state, ":setOption(key, val)"); lua_rawseti(state, -2, 11);
1007-
lua_pushstring(state, "[addr] -- index by address"); lua_rawseti(state, -2, 12);
1019+
lua_pushstring(state, ".ReadTimeout -- get/set read timeout (seconds)"); lua_rawseti(state, -2, 5);
1020+
lua_pushstring(state, ".WriteTimeout -- get/set write timeout (seconds)"); lua_rawseti(state, -2, 6);
1021+
lua_pushstring(state, ":read()"); lua_rawseti(state, -2, 7);
1022+
lua_pushstring(state, ":write(data)"); lua_rawseti(state, -2, 8);
1023+
lua_pushstring(state, ":writeread(data)"); lua_rawseti(state, -2, 9);
1024+
lua_pushstring(state, ":flush()"); lua_rawseti(state, -2, 10);
1025+
lua_pushstring(state, ":trace(mask) -- error=0x1, device=0x2, filter=0x4, driver=0x8, flow=0x10, warning=0x20"); lua_rawseti(state, -2, 11);
1026+
lua_pushstring(state, ":traceio(mask) -- nodata=0x0, ascii=0x1, escape=0x2, hex=0x4"); lua_rawseti(state, -2, 12);
1027+
lua_pushstring(state, ":setOption(key, val)"); lua_rawseti(state, -2, 13);
1028+
lua_pushstring(state, "[addr] -- index by address"); lua_rawseti(state, -2, 14);
10081029
lua_setfield(state, -2, "_doc");
10091030
}
10101031
lua_setmetatable(state, -2);

0 commit comments

Comments
 (0)