Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Simple XBee code not working

by SquareJ (Novice)
on Jun 20, 2012 at 21:33 UTC ( [id://977479]=perlquestion: print w/replies, xml ) Need Help??

SquareJ has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to simply send a message to another XBee device using the XBee module. I have cut and paste from the module page but cannot get past the simple error below. syntax error at moduletest.pl line 12, near ") {" Execution of moduletest.pl aborted due to compilation errors. Also, does anyone have some working code to send data to and from XBees? Perl examples seem to be non existent : (
use strict; use Win32::SerialPort; use Device::XBee::API; use Data::Dumper; my $rx; my $serial_port_device = Win32::SerialPort->start ("tpj4.cfg") || die; my $api = Device::XBee::API->new( { fh => $serial_port_device } ) || +die $!; if ( !$api->tx( { sh => 0, sl => 0 }, 'hello world!' ) { die "Transmit failed!"; } $rx = $api->rx(); die Dumper($rx);
Using the following for tpj4.cfg
Win32::SerialPort_Configuration_File -- DO NOT EDIT -- \\.\COM3 CFG_1,none eol,10 clear,-@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@- RCONST,1000 istrip,0 CFG_2,none XOFFCHAR,19 PARITY_EN,0 WCONST,200 intr,3 U_MSG,1 STOP,1 XONLIM,2048 erase,8 XONCHAR,17 BINARY,1 RTOT,0 echonl,0 XOFFLIM,512 icrnl,0 inlcr,0 READBUF,4096 igncr,0 EOFCHAR,0 WRITEBUF,0 RINT,4294967295 ocrnl,0 bsdel,  opost,0 echoke,1 PARITY,none HNAME,localhost echoctl,0 CFG_3,none EVTCHAR,0 icanon,0 isig,0 HADDR,0 E_MSG,1 DATA,8 DVTYPE,none echo,0 quit,4 s_eof,26 s_kill,21 ERRCHAR,0 onlcr,1 ALIAS,COM3 HSHAKE,none DATYPE,raw echok,1 echoe,1 BAUD,57600 WTOT,10

Replies are listed 'Best First'.
Re: Simple XBee code not working
by Argel (Prior) on Jun 20, 2012 at 21:50 UTC
    You are missing the closing paren for the if statement:
    if ( !$api->tx( { sh => 0, sl => 0 }, 'hello world!' ) ) { 1 2 1 ^-- add
    Don't feel too bad that you missed it. Sometimes the easy ones like these are the hardest to see. That's why a second pair of eyes can be so important!

    Elda Taluta; Sarks Sark; Ark Arks
    My deviantART gallery

      With some help from JEagle himself i am now able to talk to remote XBees too. Below is a simple AT get request. Jason
      #!/usr/local/bin/perl use warnings; use strict; use Win32::SerialPort; use Device::XBee::API; #Tuna - 13A200 40475351 my $serial_port_device = Win32::SerialPort->start ("tpj4.cfg") || die; # Create the API object my $api = Device::XBee::API->new( { fh => $serial_port_device, packet_timeout => 10}) || die $!; my ($High, $Low, $Network) = (0x13A200, 1083841427, 0xFFFE); #Requests Temperature built in - TP my $at_frame_id = $api->remote_at( { sh => $High, sl => $Low }, 'tp' ) +; # Receive the reply my $rx = $api->rx_frame_id( $at_frame_id ); # Prints ALL hash values and keys my (%rx, $temp, $key, $value); while (($key,$value) = each $rx) { print "$key $value \n"; } # Prints results print 'RX return code:' . $rx->{status} . " \n\n"; print 'Data received:' . $rx->{data_as_int} . " \n\n"; my $CTemperature = sprintf("%x",$rx->{data_as_int}); my $FTemperature = $CTemperature * 9 / 5 + 32; #F = 9 / 5 C + 32 print "\n\nTemperature IS - " . $CTemperature . "C or " . "$FTempe +rature F\n\n"; # IF Errors die "No reply received" if !$rx; if ( $rx->{status} != 0 ) { die "API error" if $rx->{is_error}; die "Invalid command" if $rx->{is_invalid_command}; die "Invalid parameter" if $rx->{is_invalid_parameter}; die "Unknown error"; } __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://977479]
Approved by Argel
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found