@@ -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
769771static 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
792794static 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
798801static 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
805809static 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