Binaries

11 messages
Open this post in threaded view
|

Binaries

 If I have a number, say 1450000001 and I want to represent that as a binary in the form   <<16#14,16#50,16#00,16#00,16#01>> what's the best way. I'm not sure what list_to_binary(integer_to_list(1450000001)) which prints as <<"1450000001">> produces but I guess its 10 bytes not 5. BobC _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

Re: Binaries

 The integer-unit bit syntax should help you.http://erlang.org/doc/programming_examples/bit_syntax.html#segmentsCheers,-- Pierre FenollOn Tue, 19 Jun 2018 at 17:33, Bob Cowdery <[hidden email]> wrote:If I have a number, say 1450000001 and I want to represent that as a binary in the form   <<16#14,16#50,16#00,16#00,16#01>> what's the best way. I'm not sure what list_to_binary(integer_to_list(1450000001)) which prints as <<"1450000001">> produces but I guess its 10 bytes not 5. BobC _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

Re: Binaries

 In reply to this post by Bob Cowdery-4 On Tue, Jun 19, 2018 at 04:32:49PM +0100, Bob Cowdery wrote: > If I have a number, say 1450000001 and I want to represent that as a > binary in the form > >  <<16#14,16#50,16#00,16#00,16#01>> what's the best way. > > I'm not sure what list_to_binary(integer_to_list(1450000001)) which > prints as <<"1450000001">> produces but I guess its 10 bytes not 5.    This is basically just binary coded decimal, isn't it?    You should be able to do this with a binary comprehension: 1> << <<(C-\$0):4>> || C <- "12345678">>. <<18,52,86,120>>    Hugo. -- Hugo Mills             | Great oxymorons of the world, no. 7: hugo@... carfax.org.uk | The Simple Truth http://carfax.org.uk/  | PGP: E2AB1DE4          | _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions signature.asc (853 bytes) Download Attachment
Open this post in threaded view
|

Re: Binaries

 In reply to this post by Bob Cowdery-4 If you mean 16#1450000001, then <<16#1450000001:40/big>> will give you a 5 byte (40 bits) big endian format <<16#14,16#50,16#00,16#00,16#01>> If you mean 1450000001 as a decimal integer, then my guess is you do not actually want that. integer_to_list produces ASCII characters. /Sverker On tis, 2018-06-19 at 16:32 +0100, Bob Cowdery wrote: > If I have a number, say 1450000001 and I want to represent that as a  > binary in the form > >   <<16#14,16#50,16#00,16#00,16#01>> what's the best way. > > I'm not sure what list_to_binary(integer_to_list(1450000001)) which  > prints as <<"1450000001">> produces but I guess its 10 bytes not 5. > > BobC > > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions_______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

Re: Binaries

 In reply to this post by Bob Cowdery-4 Thanks for all the suggestions. Still a little confused. The number is an integer, actually a frequency in Hz plus a command byte at the end which is being sent over a serial connection in hex format using gen_serial. This command works: gen_serial:bsend(P, <<16#14,16#50,16#00,16#00,16#01>>). where P is the open Port. However, when I use any of the methods to create a hex version they all end up with <<20,80,0,0,1>> which to my mind is the decimal equiv of above. If I fire that I get: 5> gen_serial:bsend(P,<<20,80,0,0,1>>). ** exception error: bad argument       in function  port_command/2          called as port_command(#Port<0.470>,[<<"d">>|<<20,80,0,0,1>>])       in call from gen_serial:send/2 (gen_serial.erl, line 624)       in call from gen_serial:bsend/3 (gen_serial.erl, line 706) What is <<"d">> doing in there? On 6/19/2018 4:32 PM, Bob Cowdery wrote: > If I have a number, say 1450000001 and I want to represent that as a > binary in the form > >  <<16#14,16#50,16#00,16#00,16#01>> what's the best way. > > I'm not sure what list_to_binary(integer_to_list(1450000001)) which > prints as <<"1450000001">> produces but I guess its 10 bytes not 5. > > BobC > > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions_______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

Re: Binaries

 I think something got messed up. It's not failing now but just does nothing. So the encoding on the wire is different for the two versions that are numerically the same. On 6/19/2018 5:22 PM, Bob Cowdery wrote: > Thanks for all the suggestions. Still a little confused. The number is > an integer, actually a frequency in Hz plus a command byte at the end > which is being sent over a serial connection in hex format using > gen_serial. > > This command works: gen_serial:bsend(P, > <<16#14,16#50,16#00,16#00,16#01>>). where P is the open Port. > > However, when I use any of the methods to create a hex version they > all end up with <<20,80,0,0,1>> which to my mind is the decimal equiv > of above. > > If I fire that I get: > > 5> gen_serial:bsend(P,<<20,80,0,0,1>>). > ** exception error: bad argument >      in function  port_command/2 >         called as port_command(#Port<0.470>,[<<"d">>|<<20,80,0,0,1>>]) >      in call from gen_serial:send/2 (gen_serial.erl, line 624) >      in call from gen_serial:bsend/3 (gen_serial.erl, line 706) > > What is <<"d">> doing in there? > > > On 6/19/2018 4:32 PM, Bob Cowdery wrote: >> If I have a number, say 1450000001 and I want to represent that as a >> binary in the form >> >>  <<16#14,16#50,16#00,16#00,16#01>> what's the best way. >> >> I'm not sure what list_to_binary(integer_to_list(1450000001)) which >> prints as <<"1450000001">> produces but I guess its 10 bytes not 5. >> >> BobC >> >> _______________________________________________ >> erlang-questions mailing list >> [hidden email] >> http://erlang.org/mailman/listinfo/erlang-questions> > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions_______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

Re: Binaries

 In reply to this post by Bob Cowdery-4 That's how erlang part communicates with C part.-define(PACKET_DATA, \$d).send(#gen_serial{port = Port}, Data) ->  true = port_command(Port, [<> | Data]),  ok.Maybe port_command does not expect an improper list — try one of these:* gen_serial:bsend(P,[<<20,80,0,0,1>>]) % Your binary wrapped in a list* gen_serial:bsend(P,[20,80,0,0,1]) % data as list instead of binaryOn Tue, Jun 19, 2018 at 7:22 PM Bob Cowdery <[hidden email]> wrote:Thanks for all the suggestions. Still a little confused. The number is an integer, actually a frequency in Hz plus a command byte at the end which is being sent over a serial connection in hex format using gen_serial. This command works: gen_serial:bsend(P, <<16#14,16#50,16#00,16#00,16#01>>). where P is the open Port. However, when I use any of the methods to create a hex version they all end up with <<20,80,0,0,1>> which to my mind is the decimal equiv of above. If I fire that I get: 5> gen_serial:bsend(P,<<20,80,0,0,1>>). ** exception error: bad argument       in function  port_command/2          called as port_command(#Port<0.470>,[<<"d">>|<<20,80,0,0,1>>])       in call from gen_serial:send/2 (gen_serial.erl, line 624)       in call from gen_serial:bsend/3 (gen_serial.erl, line 706) What is <<"d">> doing in there? On 6/19/2018 4:32 PM, Bob Cowdery wrote: > If I have a number, say 1450000001 and I want to represent that as a > binary in the form > >  <<16#14,16#50,16#00,16#00,16#01>> what's the best way. > > I'm not sure what list_to_binary(integer_to_list(1450000001)) which > prints as <<"1450000001">> produces but I guess its 10 bytes not 5. > > BobC > > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions -- Danil Zagoskin | [hidden email] _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

Re: Binaries

 The first does nothing and the second hangs. It's not gen-serial that's having a problem with it. It's sending it and giving ok but when decoded in the radio which obviously I can't look at it isn't a valid command. The encoding of <<16#14,16#50,16#00,16#00,16#01>> and <<20,80,0,0,1>> is somehow different. On 6/19/2018 5:37 PM, Danil Zagoskin wrote: That's how erlang part communicates with C part. -define(PACKET_DATA, \$d). send(#gen_serial{port = Port}, Data) ->   true = port_command(Port, [<> | Data]),   ok. Maybe port_command does not expect an improper list — try one of these: * gen_serial:bsend(P,[<<20,80,0,0,1>>]) % Your binary wrapped in a list * gen_serial:bsend(P,[20,80,0,0,1]) % data as list instead of binary On Tue, Jun 19, 2018 at 7:22 PM Bob Cowdery <[hidden email]> wrote: Thanks for all the suggestions. Still a little confused. The number is an integer, actually a frequency in Hz plus a command byte at the end which is being sent over a serial connection in hex format using gen_serial. This command works: gen_serial:bsend(P, <<16#14,16#50,16#00,16#00,16#01>>). where P is the open Port. However, when I use any of the methods to create a hex version they all end up with <<20,80,0,0,1>> which to my mind is the decimal equiv of above. If I fire that I get: 5> gen_serial:bsend(P,<<20,80,0,0,1>>). ** exception error: bad argument       in function  port_command/2          called as port_command(#Port<0.470>,[<<"d">>|<<20,80,0,0,1>>])       in call from gen_serial:send/2 (gen_serial.erl, line 624)       in call from gen_serial:bsend/3 (gen_serial.erl, line 706) What is <<"d">> doing in there? On 6/19/2018 4:32 PM, Bob Cowdery wrote: > If I have a number, say 1450000001 and I want to represent that as a > binary in the form > >  <<16#14,16#50,16#00,16#00,16#01>> what's the best way. > > I'm not sure what list_to_binary(integer_to_list(1450000001)) which > prints as <<"1450000001">> produces but I guess its 10 bytes not 5. > > BobC > > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions -- Danil Zagoskin | [hidden email] _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

Re: Binaries

 <<16#14,16#50,16#00,16#00,16#01>> and <<20,80,0,0,1>> are identical. If you're experiencing any difference in behavior when using those expressions, your problem is really somewhere else. Perhaps the device is stateful and not getting properly reset from one call to the next, or the port P was closed because the process died with an exception.On Tue, Jun 19, 2018 at 9:53 AM Bob Cowdery <[hidden email]> wrote: The first does nothing and the second hangs. It's not gen-serial that's having a problem with it. It's sending it and giving ok but when decoded in the radio which obviously I can't look at it isn't a valid command. The encoding of <<16#14,16#50,16#00,16#00,16#01>> and <<20,80,0,0,1>> is somehow different. On 6/19/2018 5:37 PM, Danil Zagoskin wrote: That's how erlang part communicates with C part. -define(PACKET_DATA, \$d). send(#gen_serial{port = Port}, Data) ->   true = port_command(Port, [<> | Data]),   ok. Maybe port_command does not expect an improper list — try one of these: * gen_serial:bsend(P,[<<20,80,0,0,1>>]) % Your binary wrapped in a list * gen_serial:bsend(P,[20,80,0,0,1]) % data as list instead of binary On Tue, Jun 19, 2018 at 7:22 PM Bob Cowdery <[hidden email]> wrote: Thanks for all the suggestions. Still a little confused. The number is an integer, actually a frequency in Hz plus a command byte at the end which is being sent over a serial connection in hex format using gen_serial. This command works: gen_serial:bsend(P, <<16#14,16#50,16#00,16#00,16#01>>). where P is the open Port. However, when I use any of the methods to create a hex version they all end up with <<20,80,0,0,1>> which to my mind is the decimal equiv of above. If I fire that I get: 5> gen_serial:bsend(P,<<20,80,0,0,1>>). ** exception error: bad argument       in function  port_command/2          called as port_command(#Port<0.470>,[<<"d">>|<<20,80,0,0,1>>])       in call from gen_serial:send/2 (gen_serial.erl, line 624)       in call from gen_serial:bsend/3 (gen_serial.erl, line 706) What is <<"d">> doing in there? On 6/19/2018 4:32 PM, Bob Cowdery wrote: > If I have a number, say 1450000001 and I want to represent that as a > binary in the form > >  <<16#14,16#50,16#00,16#00,16#01>> what's the best way. > > I'm not sure what list_to_binary(integer_to_list(1450000001)) which > prints as <<"1450000001">> produces but I guess its 10 bytes not 5. > > BobC > > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions -- Danil Zagoskin | [hidden email] _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions