Skip to content

Commit

Permalink
Update init code to error out if we cannot connect
Browse files Browse the repository at this point in the history
  • Loading branch information
ctennis committed Feb 5, 2010
1 parent b60a836 commit bb3eab5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.*.swp
15 changes: 10 additions & 5 deletions src/modbus_device.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ write_hreg(Device, Offset, Value) ->


init([Type, Host, Port, DeviceAddr]) ->
% TODO: If we fail to connect, we should return {stop, Reason} from this function.
{ok, Sock} = gen_tcp:connect(Host, Port, [{active,false}, {packet, 0}]),
State = #modbus_state{type=Type,sock=Sock,device_address=DeviceAddr,tid=1},
{ok, State, 5000}.

Retval = gen_tcp:connect(Host, Port, [{active,false}, {packet, 0}]),

case Retval of
{ok, Sock} ->
State = #modbus_state{type=Type,sock=Sock,device_address=DeviceAddr,tid=1},
{ok, State, 5000};
{error,ErrorType} ->
{stop,{error,ErrorType}}
end.

handle_call({read_hreg_16, Offset}, _From, State) ->
Request = #rtu_request{address=State#modbus_state.device_address,function_code=?FC_READ_HREGS,start=Offset,data=1},
NewState = State#modbus_state{tid=State#modbus_state.tid + 1},
Expand Down

0 comments on commit bb3eab5

Please sign in to comment.