<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16809" name=GENERATOR></HEAD>
<BODY>
<DIV>Thank you Per. That was the good solution. I understood my faults now
...</DIV>
<DIV>John</DIV>
<DIV> </DIV>
<DIV>hat is because you are sending the following binary encoded
term:<BR>"$AVRMC,1234,151736" and you are matching that to
{<<H,U,T>>} and a string does not match a one-tuple with a three
byte binary as only element.<BR><BR>what you want is probably something like
this:<BR><BR>-module(aa).<BR>-define(X,<<131,107,0,18,36,65,86,82,77,67,44,49,50,51,52,44,49,53,49,55,51,54>>).<BR>-export([start/0]).<BR><BR>start()
-><BR> case string:tokens(binary_to_term(?X), ",")
of<BR> [H,U,T] -><BR>
io:format("Header = ~s~n",[H]),<BR> io:format("Unit ID =
~s~n",[U]),<BR> io:format("Time = ~s~n",[T])<BR>
end.<BR><BR>The question is if the wire protocol isn't given this is not the
easiest way to have two Erlang nodes talk. It would be far easier to say that
the nodes should be sending binary encoded three-tuples to each other then you'd
have something like this:<BR><BR>-module(ab).<BR>-define(X,
term_to_binary({"$AVRMC", 1234, 151736})).<BR>-export([start/0]).<BR><BR>start()
-><BR> case binary_to_term(?X) of<BR> {H,U,T}
-><BR> io:format("Header =
~s~n",[H]),<BR> io:format("Unit ID = ~p~n",[U]),<BR>
io:format("Time = ~p~n",[T])<BR> end.<BR>
<BR>Note that in this case Unit ID and Time would be numbers rather than
strings.<BR><BR>None of the code above has actually been tested so it might not
compile.<BR><BR>Per<BR><BR><BR></DIV>
<DIV class=gmail_quote>2009/4/3 Gamoto <SPAN
dir=ltr><gamoto@bluewin.ch></SPAN><BR>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">-module(aa).<BR>-define(X,<<131,107,0,18,36,65,86,82,77,67,44,49,50,51,52,44,49,53,49,55,51,54>>).<BR>-export([start/0]).<BR><BR>start()-><BR>
case binary_to_term(?X) of<BR>
{<<H,U,T>>}-><BR>
io:format("Header = ~s~n",[H]),<BR>
io:format("Unit ID = ~s~n",[U]),<BR>
io:format("Time = ~s~n",[T])<BR>
end.<BR><BR>This gives me an error.<BR>I was waiting for<BR>Header =
$AVRMC<BR>Unit ID = 1234<BR>Time = 151736<BR><BR>The most important for
me is to obtain H,U and T<BR>
<DIV>
<DIV></DIV>
<DIV class=h5>><BR>><BR>>> I receive from several machines
messages and I would like to extract data.<BR>>> My first approach
was:<BR>>><BR>>> handler(Data)-><BR>>> case
Data of<BR>>> <><BR>>>
etc...<BR>>><BR>>> My problem is that some
segment have a fixed length (header, Status) and some<BR>>> others a
variable length (Time <= 6 bytes, UnitID <= 8 bytes).<BR>>> My
approach is bad. I thought to do: Segments =
string:tokens(Data,",")<BR>>> This separates the segments but I don't
have their name<BR>>> (header,unitid,time,status) !<BR>>> Could
you advice me a better approach. My goal is to check each segment and
make<BR>>> actions according to their values.<BR>><BR>>You need to
define a message format such that the required information is present to
determine how to parse the rest of the message. This isn't really an
erlang-specific problem but erlang is very good at supporting
this.<BR>><BR>>As an example you could use erlang:term_to_binary() and
binary_to_term() for the message encoding on the wire. binary_to_term() might
fail if the message is incomplete. This is where the {packet, N} inet options
for connect/listen come in, eg. {packet,2} will only return complete messages;
the length header is added automatically on send and stripped on receive so
you only need to do, eg.<BR>><BR>>gen_tcp:send( Sock, term_to_binary(
MessageTerm ) )<BR>><BR>>and if you receive {tcp, Sock, Data}, then it
is safe to call<BR>><BR>>MessageTerm =
binary_to_term(Data)<BR>><BR>>Check out the inet module documentation.
There are lots of useful options.<BR>><BR>>Hope that is
useful<BR>><BR>>--<BR>> Rich<BR>><BR>><BR>>
The new Internet Explorer 8 optimised for Yahoo!7: Faster, Safer,
Easier.<BR><BR>_______________________________________________<BR>erlang-questions
mailing list<BR><A
href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</A><BR><A
href="http://www.erlang.org/mailman/listinfo/erlang-questions"
target=_blank>http://www.erlang.org/mailman/listinfo/erlang-questions</A><BR></DIV></DIV></BLOCKQUOTE></DIV><BR></BODY></HTML>