
MODEM
Modems usually are destined for remote sites where you don't want any glitches or difficulties. Here are a few tricks that help make the BS2 system with a modem reliable and user-friendly. With a modem, you should never trust that the connection is going to go by the book. If anything can go wrong, sooner or later, it certainly will.
It pays to have a technical manual for the modem you are using. Modems vary widely in the commands they support ,and the default values for the parameters. True, you can discover those things by trial and error, but who needs it?
If you want to use the "system" port (pins 1 & 2) on the BS2 for a modem, the trick is to preinitialize the modem (by sending the "E0" command from a regular PC, see below) so that it does not echo commands. Both the BS2 system port and the modem normally echo the characters they receive, so the first command sent from the BS2 to the modem will lock your system up in infinite reverb, unless the echo is turned off.
The concern in the previous paragraph does not apply if the data lines to the modem will be connected to one of regular stamp pins, P0 to P15. Those pins do not echo.
You should find a modem that allows you to store a power-up configuration in its eeprom. This is essential if you plan to use the system serial port on BS2 pins 1 and 2. Some cheaper modems do not implement the configuration storage feature. The configuration memory in the modem can relieve the BS2 from having to do the configuration each time at power up. The configuration memory can also store phone numbers for automatic dialling.
There are some commands that you will want to store in this non-volatile memory in the modem. That way you can be sure the modem is always initialized in the way that works with your BS2 system. I suggest the following command string: AT &F E0 Q1 S0=1 &C1 &D3 &W0 &Y0 ^M
where
&F brings in the factory defaults
E0 turns off command echo
Q1 turns off result reporting
S0=1 to autoanswer on the first ring
&C1 enable DCD pin to follow actual carrier status (see below)
&D3 enable hardware reset via DTR drop (see below)
&W0 stores the configuration in eeprom set #0
&Y0 chooses eeprom set #0 as the power-up default.
Some modems don't support &Yn command and always use set #0 at powerup.
Result codes are a problem in systems that are set up to auto-answer incoming calls, when using the system port on BS2 pins 1 and 2. On many modems, the echo of the "RING" result code prevents the incoming call from connecting. This is less of a problem on the regular BS2 pins. Any data received from the BS2 during a connection attempt will usually abort the connection.
Use the S0=1 or S0=2 command if you want the modem to answer the call on the first or second ring. Use S0=0 if the BS2 will originate the calls (no autoanswer).
If the BS2 will originate calls, you may also want to use the modem's capability to store a phone numbers in its memory. The numbers are usually stored with the "Zn" command.
The options for the &C1 and &D3 are discussed in more detail below in connection with the hardware installation.
IMPORTANT: If command echo and result codes are disabled, be sure to attach an advisory note to the modem to that effect. The note should tell the curious to issue the following command: AT &F &W0 ^M
which resets the modem to its factory defaults. Otherwise someone at a future date will think the modem is busted.
Unless you really want to get in deep, you may want to turn off the advanced features of data compression and error correction (if it supports them to begin with). The commands to turn off these features are \N0 turns off error checking
%C0 turns off data compression
Include these in the above string. Be sure to include this in the initialization of the modems on both ends of the communications. In hyperterminal on the PC, this command can be entered inproperties/connect to/configure/advanced-extra settings=\N0%C0
Here is another command supported by some modems: S37=6 fixes the first-try baud rate to 2400
This fixes the baud rate at 2400. In hyperterminal, you can enter this in the setup:properties/connect-to/configure/general="2400baud"+"connect only at this speed".
Use the appropriate value for the connection speed you want to establish. By fixing this value, the modems will negotiate the connection more quickly and reliably than if they have to seek out the common ground. Issue the same commands both to the modem attached to the BS2 and to the modem at the other end of the connection. Quite often older modems are blown away by the bizarre sounds coming from an advanced modem. Do all that you can to match the settings at both ends from the outset.
I usually use the DCD (data carrier detect) line to signal to the BS2 that the modem is on line and ready to talk. The DCD line goes high when the modem detects a carrier (when &C1 is set). The DCD line can either go into a regular BS2 pin, to be detected by polling, and/or it can go via a capacitor into the ATN input, to wake up the BS2 for immediate action. I put in both options, so that the program can test the DCD line to help determine the cause of the reset, and branch to the modem routine if appropriate. The RD (ring detect) line from the modem can be added into the mix to give advance warning that the modem is going to be attempting a connection.
It is important not to send anything out through the modem port while the modem is negotiating a connection. Many modems, if they receive characters from the BS2 before the connection is established will instantly abort and hang up. Your program must be designed not to send characters to the serial port except when DCD is high..
The DCD line goes from high to low when the modem loses its carrier. The setting in the S10 modem register determines how long after loss of carrier it is before the DCD line goes back low.
Some modems stay on line, receiving noise, even if they lose the carrier. What are the consequences of that? Suppose the BS2 SERIN command will receive one or more bytes. The serin command will probably have a timeout associated with it, so that if it does not receive datawithin a period of time, it will branch to a special routine to handle the timeout. However, this is far from foolproof with a modem involved. If the modem loses carrier, but still remains connected to the line, then noise from the line can generates random data. It can fool the serin command, so that it never branches to its timeout routine. Any bit of noise is enough to send the serin timer back to the beginning. The upshot is that the system needs a way to reset the BS2 in case anything becomes stuck. One way to do this is to have the DCD line reset the processor when it goes from high (online) to low (offline).
One option for hanging up the modem under software control is the +++ ATH0 command. However, that sequence is never a sure thing. The sequence can be corrupted by noise, and sometimes the modem does not "hear" the +++ escape sequence. There is a much more reliable way to reset the modem, via hardware and the DTR line. The DTR line is an input to the modem, and so long as it is kept high, the modem will stay connected. But when the DTR line is brought from high to low, the modem hangs up, resets and executes the initialization commands. That is what happens if the &D3 option is set. There are other options for handling the DTR line, but &D3 option is the most reliable. Hardware reset option is by far the most reliable to guarantee that the modem will be left in a state ready for the next call.