diff options
Diffstat (limited to 'rsbusserv.erl')
-rw-r--r-- | rsbusserv.erl | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/rsbusserv.erl b/rsbusserv.erl index 997e590..b9eec5b 100644 --- a/rsbusserv.erl +++ b/rsbusserv.erl @@ -17,14 +17,15 @@ %% API %% -export([start/0]). --define(DEVICE,"/dev/ttyUSB0"). --define(SPEED,19200). --define(TIMEOUT,40). % Timeout in milisec --define(REQTIMEOUT,200). +-define(DEVICE,"/dev/ttyACM0"). +-define(SPEED,9600). +-define(TIMEOUT,10). % Timeout in milisec +-define(REQTIMEOUT,1000). -define(STARTBIT,[16#5B,16#AD]). -define(STOPBIT,[16#A4,16#52]). -define(REP,16#FF). +-define(ERR,16#F0). rsbuslistener(Pid) -> SerialPort = serial:start([{speed,?SPEED},{open,?DEVICE}]), @@ -42,12 +43,12 @@ rsbus_rec([H1,H2,H3|Tail],read,Msg,Cmp) -> rsbus_rec([H2,H3|Tail],read,[H1|Msg],Cmp); rsbus_rec([H1,H2,H3,H4|Tail1],wait,[],[H1,H2,H3,H4]) -> rsbus_rec(Tail1,read,[],?STOPBIT); -rsbus_rec([H|Tail],wait,[],Cmp) -> - rsbus_rec(Tail,wait,[],Cmp); +rsbus_rec([H1,H2,H3,H4|Tail],wait,[],Cmp) -> + rsbus_rec([H2,H3,H4|Tail],wait,[],Cmp); rsbus_rec(Rec,State,Msg,Cmp) -> receive {data, Bytes} -> - erlang:display(binary_to_list(Bytes)), + %% io:format("~p\n",[Rec++binary_to_list(Bytes)]), rsbus_rec(Rec++binary_to_list(Bytes),State,Msg,Cmp) after ?TIMEOUT -> {error,timeout} @@ -62,14 +63,15 @@ rsbus_send(SerialPort,Addr,Msg) -> P=rsbus_parity(Msg) bxor Addr, SerialPort ! {send,[P]}, SerialPort ! {send,?STOPBIT}, - io:format("Send Complete!\n"), + %% io:format("Send Complete!\n"), case rsbus_rec([],wait,[],?STARTBIT++[0,Addr]) of {stat,[?REP],RP} -> rsbus_send(SerialPort,Addr,Msg); % Will resend no matter what + {stat,[?ERR],RP} -> {error,parse}; % State Reject {stat,Stat,RP} -> erlang:display(RP), TestP =rsbus_parity([RP|Stat]), if - TestP /= Addr -> {err,parse}; + TestP /= Addr -> {error,parse}; true -> {stat,hd(Stat),tl(Stat)} end; {error,timeout} -> {error,timeout} @@ -78,14 +80,14 @@ rsbus_send(SerialPort,Addr,Msg) -> %% This function can effectively serialize access rsbus_listner(Pid,SerialPort) -> - erlang:display(self()), + %% erlang:display(self()), receive {data, _Bytes} -> %% This shouldn't happen, no process should initiate comm %% Discard everything rsbus_listner(Pid,SerialPort); {PPid,comm,Addr,Msg} -> - io:format("RSBus sending..."), + %% io:format("RSBus sending..."), PPid ! {self(),device,Addr,rsbus_send(SerialPort,Addr,Msg)}, % Process and return rsbus_listner(Pid,SerialPort); {Pid,shutdown} -> @@ -109,9 +111,10 @@ rsbusreq(Addr,Msg) -> receive {PPid,device,Addr,{error,Reason}} -> {error,Reason}; - {PPid,device,Addr,{stat,Type,Msg}} -> - {stat,Type,Msg} + {PPid,device,Addr,Stat} -> + Stat after ?REQTIMEOUT -> + erlang:display("here"), {error,timeout} end. |