XBee Hell

Discussion specific to the DIP and TQFP packaged ZX devices like the ZX-40, ZX-44, ZX-32 and ZX-328 series. The differences between these devices is primarily the packaging and pinout so most issues will apply to all devices.
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

XBee pro for 2.4GHz... line of sight range of course depends on the XBee Pro/Non-pro and choice of antennas on each end. I've used many different types, often with high gain on one end and on-PCB chip/wire antennas on the other. Even with line of sight, don't expect more than 1500-2000 ft. Also depends on antenna elevation (Fresnel zone effect).

In line of sight, the range is easily calculated for a prediction using an ordinary RF link budget.
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

I felt confident and attempted to change the Baud speed from 19200 to 115200 on all elements (ZX-40n chip, IDE, and XBee Pro). But got stuck on the Zbasic side.

I started with the ZX-40n device, disconnected the XBee, and re-installed the MAX232. I opened the ZBasic IDE and reverted back to the standard DTR mode (unchecked the "Use ATN character" box). I also changed the serial port back to the hard serial line. Then, under "Execution Options", I increased the Baud Rate to 115,200. I recompiled and downloaded. No problems - all as well.

I repeated the process with "Use ATN character", but sadly got the dreaded "Device failed to respond to ATN character (31) on COM3." Again, this is with the hard serial line. I changed the AtnChar several times with no luck. I added the "option com1speed" in the program consistent with the IDE serial options, but no luck. I slowed the Baud speed - no luck. I repeated these changes in DTR mode and all worked fine! Then I went back to 19200 baud rate, and the ATN method functioned properly.

Did I inadvertently lock the ZX-40n chip to the 19200 baud rate?
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

Once the ATN character is set and baud rate is set on a ZX-40n chip, how can these be changed without getting the "Device failed to respond to ATN character..." error? I'm getting this error with a direct serial line.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:Once the ATN character is set and baud rate is set on a ZX-40n chip,
To be clear, the only thing that needs to be set on the ZX device is the ATN character, which must match that specified in the IDE. The download baud rate isn't "set" in the ZX. Rather, one of two scenarios occur:
  • If the ZX device enters "command mode" by way of DTR toggling, the ZX sets the baud rate to 115.2K baud.
  • If the ZX device enters "command mode" due to reception of the previously programmed ATN character, the baud rate is not changed, leaving the serial port running at whatever speed it was at. Then, the IDE may or may not send the ZX a command to change the baud rate depending on how the IDE is configured.
It is important to note that when converting to using an ATN character for downloading, you must download a program that was compiled with the directive Option ATNChar with the desired ATN character specified. This download must be performed (the first time, at least) using DTR toggling. Once that step is completed, the ZX will jump to command mode when the specified ATN character is received. This important aspect was not clearly described, I think, in your earlier step-by-step instructions.
liam.zbasic wrote:how can these be changed without getting the "Device failed to respond to ATN character..." error?
You can change the ATN character setting in the ZX device by downloading a program (by either the DTR toggling or the ATN character method) that was compiled with the new ATN character specified in the Option ATNChar directive. After that program is downloaded successfully, you must then re-configure the IDE to use the new ATN character for any further downloads.

It is probably simpler to understand the process using DTR toggling so I will enumerate the steps to change (or initially set) the ATN character by that means first.
  • Connect the ZX to your PC by a direct serial link, i.e. direct connections (through a level converter, if necessary) from the PC serial port or USB-serial converter. The DTR signal must be connected (through the normal ATN circuitry) to the ZX reset pin.
  • Configure the IDE for dowloading by DTR toggling, i.e. uncheck the "Use ATN character" checkbox on the Serial Port Options dialog.
  • Specify the desired ATN character in the Option ATNChar directive. This can be done with any compilable program, the minimum form of which is shown below. Compile and download the program.
  • Next, test that downloading can be accomplished without DTR by disconnecting the DTR signal, checking the "Use ATN character" checkbox on the Serial Port Options dialog (ensuring that the ATN character specified there matches the one you specified in the Option ATNChar directive), and download again.
Once you have proven that downloading using the ATN character you can replace the direct serial connection with any other compatible connection (e.g a radio link).

Here is the minimal program that must be downloaded to set the ZX ATN character:

Code: Select all

Option ATNChar &H04
Sub Main()
End Sub
As mentioned earlier, it is possible to change the ATN character via downloading using the current ATN character. Of course, this requires that downloading without DTR toggling is currently working; if not you must use the previously described method.
  • Edit the Option ATNChar directive to reflect the new ATN character.
  • Without changing the IDE configuration, compile and download the program.
  • At this point, the ZX ATN configuration and IDE ATN configuration no longer match. Further downloading will not be possible until you reconfigure the IDE to use the new ATN character.
  • Compile and download the program again to confirm that downloading is working using the new ATN character.
  • If downloading no longer works, you will most likely have to perform the download using DTR as described above.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

I tied the ZX40n device directly to my CyberSerial PCI card. The first image shows the "Serial Port Options" selected to download code by DTR toggling. The second image shows that the following code compiles fine, but displays random characters for inexplicable reasons.

Code: Select all

Option ATNChar &H04 
Sub Main() 
End Sub
Strangely, if I go back to "Serial Port Options" and simply change Baud Rate to 19200 under "Execution Options", the random characters do not appear in the debug window. This is why I thought the baud rate was somehow burned in the ZX device. Are the random characters normal?

At higher baud rates, the random characters will disappear if I explicitly add "Option com1speed" to my program AND set the "Execute Options" baud rate to the same value as "com1speed".

I BELIEVE THERE IS A BUG IN THE IDE. WHEN I SWITCH TO "Use ATN character" MODE, I GET THE DREADED "Device failed to respond to ATN character (4) on COM1". IT ONLY WORKS IF THE "EXECUTION OPTIONS" BAUD RATE IS SET TO 19200. No other speed works. Could this be a Windows 7 64-bit problem?
Attachments
ide.png
(75.5 KiB) Downloaded 414 times
serial_options.png
serial_options.png (24.93 KiB) Viewed 4349 times
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:At higher baud rates, the random characters will disappear if I explicitly add "Option com1speed" to my program AND set the "Execute Options" baud rate to the same value as "com1speed".
By default, a ZX device opens serial channel 1 when it begins running. One of the first things it does is to output the "sign on" message at the default baud rate (19.2K baud unless specified otherwise by Option Com1Speed). This output occurs before the Main() subroutine begins executing. If the IDE's execution baud rate setting doesn't match the default baud rate you'll see "garbage characters" in the Debug window as you described.
liam.zbasic wrote:IT ONLY WORKS IF THE "EXECUTION OPTIONS" BAUD RATE IS SET TO 19200.
The "execution baud rate" must match the actual Com1 baud rate on the device or it won't receive the ATN character correctly. A download by the ATN character method works as follows:
  • The ZX is running the program that was last downloaded. Com1 is assumed to be open and operating at some baud rate (either the default of 19.2K baud, the baud rate specified by Option Com1Speed, or the baud rate specified when Com1 was last opened by the application. It is further assumed that the IDE's "execution baud rate" matches the actual baud rate of Com1.
  • When a download is requested, the IDE sends the specified ATN character. If the device receives the character and it matches the ATN character that was specified with the Option ATNChar directive in the program last downloaded, the ZX will enter "command mode" and send an acknowledgement response (the baud rate remains unchanged at this point).
  • If the IDE is configured to download at a baud rate different from the execution baud rate, the IDE sends the ZX a command to tell it to switch to that specific download baud rate and the IDE switches to that same baud rate.
  • The new program is then downloaded.
  • The ZX is sent a command telling it to restart. If the download speed and execution baud rates are different, the IDE then switches to the execution baud rate.
It is important to note that if the newly downloaded program has a different ATN character specified by the Option ATNChar directive, further downloading by the ATN Character method will not work until the IDE is reconfigured to match. Further, if the newly downloaded program has a different Com1 speed (due to removing or changing the Option Com1Speed directive or due to adding, removing or changing an OpenCom() call that affects Com1), further downloading by the ATN Character method will not work until the IDE is reconfigured with a matching execution baud rate.

With the ATN Character downloading method if the execution baud rate of the device is changed, the first time the program runs the IDE will display "garbage" in the Debug window until the IDE is reconfigured to the matching execution speed. As you can see, the procedure is much simpler when using the DTR toggling download method because the IDE's execution baud rate can be changed before the download but with ATN Character downloading the IDE's execution baud rate must be changed after the download.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

Not sure where to proceed from here. Confusion... the following code complies just fine but fails to download - the IDE says "Device failed to respond to ATN...".

Code: Select all

Option ATNChar &H04
Option com1speed 115200

Sub Main() 
End Sub
The "Execute Options" baud rate was set to match the "com1speed" as shown in the enclosed image. I changed the baud rate to all values (and matched it in "com1speed"), but I receive the same download error. ONLY THE BAUD RATE 19200 WORKS (and 19200 "com1speed"). It's as if that value is hardcoded somewhere. On the other hand, the standard DTR toggling approach always works.

My theory is that in Windows 7 (64-bit), the "Execute Options" baud rate is stuck at 19200 regardless of what is selected. Please look into this.

Also, why won't the DTR toggling method work with XBee? Thank you.
Attachments
atn_setup.png
atn_setup.png (62.24 KiB) Viewed 4332 times
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:The "Execute Options" baud rate was set to match the "com1speed" as shown in the enclosed image.
The key point that I think you're missing is that the "Execute Options" baud rate must match the Com1 baud rate at which the device is currently running as opposed to the Com1 baud rate at which the device will run after the program is downloaded.

To further your understanding of this principle and to demonstrate that it works (or doesn't) on Win7 64-bit, perform each of the steps below in the order given. Use the test program shown further below. You may want to change the number in the Debug.Print message each time before you compile/download so that you know that a new program was, in fact, downloaded.
  • Connect the serial port of the PC directly to the device (through level shifters), including the DTR signal.
  • Configure the IDE to use DTR toggling for downloading (uncheck "Use ATN character" on the Serial Port Options dialog).
  • Configure the IDE for an execution baud rate of 19200.
  • Compile and download the program below with the Option Com1Speed directive commented out.
  • Observe that the Debug window contains the signon message and the "Hello, world!" output, confirming that Com1 baud rate is actually 19200.
  • Uncomment the Option Com1Speed directive in the test program. Recompile and download the modified program.
  • Observe that the Debug window contains garbage text, indicating a baud rate mismatch (the device is operating at 9600 baud and the IDE is operating at 19200).
  • Change the "Execution baud rate" in the IDE to 9600. Then, either reset the device (with a reset switch or using "Reset Device" on the Options menu) or just download again.
  • Observe that the latest output in the Debug window is now correct. Note that we could have changed the IDE's execution baud rate before downloading the program containing a Com1 speed change but this will only work correctly if you're using the DTR toggling download method.
  • Disconnect the DTR signal from the device and, optionally, confirm that you can no longer download (Device failed to respond to ATN signal...).
  • Configure the IDE for ATN Character downloading and ensure that the "Change speed for download" checkbox is unchecked.
  • Attempt to download again and observe that it is successful (and that downloading is obviously slower than before - it's being done at 9600 baud) and the Debug window contains the expected output.
  • Change the Option Com1Speed value to 38400 but do not change the IDE's execution baud rate yet.
  • Compile and download the program, observing that the download completes successfully but that the latest output in the Debug window is garbage.
  • Now that the device is operating at 38400, change the IDE's execution baud rate to match. Download again and observe that the latest output in the Debug window is now correct.
The takeaway from this exercise is that when you're using ATN Character downloading, if you change either the ATN character or the Com1Speed you must first download the modified program using the settings in the program that currently resides in the device. After the modified program is downloaded successfully you then must change the IDE to match the resident program in order to be able to download again.

Test Program:

Code: Select all

Option ATNChar &H04
Option Com1speed 9600

Sub Main()
    Debug.Print "Hello, world! 1"
End Sub
liam.zbasic wrote:Also, why won't the DTR toggling method work with XBee
When the ZX detects the DTR toggling sequence, it enters command mode and changes the Com1 speed to 115.2K baud. When downloading by DTR toggling, the IDE executes a system command to change the serial port speed of the PC to 115.2K baud to match and the download communication is carried out at that speed. With a radio link like the XBee, each end of the link has a separately configured baud rate. Changing the baud rate on the PC end may or may not re-configure the link end that is attached to the PC to a matching baud rate but it almost certainly does not change the baud rate of the far end of the radio link. Even if the baud rate of the far end could be changed, it is unlikely that DTR toggling could be performed over the link at the speed required in order for it to be recognized as an ATN signal.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

Yes, I'm now with you on the "hysteresis" effect associated with ATN downloading ("first download the modified program using the settings in the program that currently resides in the device").

I followed steps 1 through 11 as instructed and all behaved as you predicted. But then at step 12 I received the "Device failed to respond to ATN signal..." Step 12 is "Attempt to download again and observe that it is successful (and that downloading is obviously slower than before - it's being done at 9600 baud)...". It's simply not working. I made sure I followed your steps accurately. I also made sure, while in ATN download mode, the "Change speed for download" option was UNCHECKED. After further experimentation, the following steps DID work, but are not desired:

Step-1: Starting with code below, implement DTR download mode. Configure "Serial Port Options" IDE with "Use ATN character" unchecked and "Execution Options" baud rate set at 19200.

Code: Select all

Option ATNChar &H04 
'Option Com1speed 9600 
Sub Main() 
    Debug.Print "Hello, world! 1" 
End Sub
Step-2: Compile and Download. Result:

Code: Select all

Downloading file "C:\$projects\zbasic2\meanbot\mcu\atn.zxb":
...................
Download complete.
Verifying download:
...................
Verification complete.
ZBasic v3.3
Hello, world! 1
Step-3: Configure "Serial Port Options" IDE with "Use ATN character" CHECKED, "Change speed for download" UNCHECKED, and "Execution Options" baud rate at 19200.

Step-4: Compile and download. Result: Same as "Step-2", although slower.

Step-5: Uncomment "com1speed" statement from program.

Step-6: Compile and download. Result is a good download and garbage output from ZX device as expected due to baud rate mismatch:

Code: Select all

Downloading file "C:\$projects\zbasic2\meanbot\mcu\atn.zxb":
...................
Download complete.
Verifying download:
...................
Verification complete.
˜fÌžfææà`~†˜30`f3Ïàžàžþžà†
Step-7: Change "Execution Options" baud rate to 9600. From ZX breadboard, toggle power. Result is good! Same output as step-2. However, if I "Reset Device" from IDE, I get garbage output! Or if I simply "download" from IDE (shift + F5), I get the "Device failed to respond to ATN char...". Then if I toggle the ZX power, I do get the correct output as in Step-2.

Although these steps work, it's undesirable because to download a new program, I need to go through each step. This defeats the purpose for an Xbee device because I'll have to start in DTR download mode, and this requires the hard serial line.

On the other hand, dkinzer steps 1 through 11 and my steps 1 through 7 magically work if the selected baud rate for the program (com1speed) and "Execution Options" is set at 19200 regardless of prior settings in DTR toggling mode. This could be a Windows 7 64-bit issue.

Back to DTR toggling mode for XBee... if the only obstacle is the variable baud rate associated with the IDE, what if I set the "Execution Options" speed to 115200 and configure my Xbee devices to communicate at 115200? Then there is no mismatch. That should work, right?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:This could be a Windows 7 64-bit issue.
We should have a Win7 64-bit system available later this week so we can see if the problem is specific to that environment.
liam.zbasic wrote:[W]hat if I set the "Execution Options" speed to 115200 and configure my Xbee devices to communicate at 115200?
I doubt that you could get sustained data throughput at 115.2K baud. My earlier experiments with XBee topped out at 57.6K baud. Even if you could get it to run at 115.2K baud reliably, I suspect that the radio link would not allow toggling DTR at the rate required in order to be recognized as an ATN request.
- Don Kinzer
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

XBees due to the 802.15.4 protocol being half duplex and in 2.4GHz, a raw bit rate of 250Kbps- can't sustain streaming at 115200. I use XBees at 115200 but it's just to speed transfers to/from the XBee buffer memory in the Digi API mode.

The streaming rate between two 15.4 devices like XBee isn't going to be much more than 57.6Kbaud as said above - and the speed varies greatly due to noise and interference such as WiFi. The 15.4 protocol wasn't intended for high speed streaming- it's for a few packets per second per node.

If your use case is a packet now and then, don't use 115,200. Use half or 1/4 that to simplify and increase margins in baud rate mismatches and character rates. If using a soft-UART in ZBasic, these lower rates, and even lower, are prudent if the AVR is a busy boy.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

dkinzer wrote:We should have a Win7 64-bit system available later this week so we can see if the problem is specific to that environment.
I have confirmed that all of the steps in the exercise I posted on 26 February work correctly on Win7 x64. I used the ZBasic IDE v1.5.12 (the one in the latest installer). Note, however, that Option|Reset Device does not work correctly on that version when ATN Character downloading is selected. We have determined the cause of this and corrected it in our development version.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

I downloaded the latest installer (IDE v1.5.12) and no luck. "Device failed to respond to ATN character..." at dkinzer step-12. And as anomalous as it seems, while in ATN download mode, changing com1speed and Execute Options baud rate to 19200 makes it all work.

* Not sure where to go from here. Anything special required in circuit with dkinzer step-10, "Disconnect the DTR signal from the device.."? I simply pulled the DTR wire from the serial line.

*What ZX40n boot version is required for this thread?

*Any other options?

Thank you.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:[...] changing com1speed and Execute Options baud rate to 19200 makes it all work.
Just to review, changing Option Com1Speed has absolutely no effect on the first immediately following download; but does affect the next download attempt. As described earlier, this seeming anomaly is due to the fact that the download of a newly compiled program must be done at the baud rate specified by the Option Com1Speed that is in effect in the program that was previously downloaded.
liam.zbasic wrote:Anything special required in circuit with dkinzer step-10, "Disconnect the DTR signal from the device.."? I simply pulled the DTR wire from the serial line.
No. You don't even need to disconnect DTR. I included that step merely to prove that DTR wasn't required for the download.
liam.zbasic wrote:What ZX40n boot version is required for this thread?
v1.4
liam.zbasic wrote:Any other options?
It would be interesting to determine the operating baud rate of the device at the point where the download fails. You can do this by closing the serial port in the IDE (Options|Close Comm Port) and then run a terminal emulator (e.g. Bray's Terminal or TeraTerm) connecting to the same Com port, adjusting the baud rate until you find that one that renders the device output correctly after a hard reset. The results that you've reported suggest that the device is running at 19.2K baud (at step 12) but it should be running at 9600.
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

The hysterisis aspect of ATN downloading makes it difficult to explain steps. I'll try my best to clarify with as few steps as possible.

Step-B1: Enter program below. Configure IDE to use DTR-toggling downloading. Execution baud rate set at 19200. Compile & download following code. Result: good download and IDE outputs "Hello World! 1".

Code: Select all

Option ATNChar &H04 
'''Option com1speed 230400
Sub Main() 
    Debug.Print "Hello, world! 1" 
End Sub
Step-B2: Enter program below (uncomment com1speed statement). Re-configure IDE to use ATN downloading and keep Execution baud rate at 19200. Compile & download. Results: GOOD DOWNLOAD. IDE outputs garbage.

Code: Select all

Option ATNChar &H04 
Option com1speed 230400
Sub Main() 
    Debug.Print "Hello, world! 1" 
End Sub
Step-B3: Close ZBasic IDE. Open X-CTU Xbee program, which includes terminal. Set baud rate at 230400. Reset ZX40n device. Result: X-CTU Terminal CORRECTLY outputs "Hello, world! 1".

Step-B4: Close X-CTU. Open Zbasic IDE. Change Execute baud rate to 230400. Keep ATN downloading option. Reset ZX40n device. Result: IDE CORRECTLY outputs "Hello, world! 1".

Step-B5: Re-compile and download code from Step-B2. Result: "Device failed to respond to ATN character (4) on COM1."

I repeated steps-B1 through B5 with the following baud rates: 9600, 19200, 38400, 57600 , 115200, 128000 and 230400. Same results as stated above, with the exception of 19200. What do you think?
Post Reply