Bailey O.H.Embedded systems.Desktop integration.2005
.pdf
310 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
Figure 8-30
In Figure 8-30 we have allocated the BS2 pins 0-3 to the 4-Wire TTL serial interface. Our pin assignments are: P0 = TX, P1 = RX, P2 = CTS, and P3 = RTS. We will also connect the VSS from the BS2 to Gnd on the SB72. In Chapter 7 we covered how the RS-232 interface works. The following listing will echo any inbound data to the debug terminal screen.
Listing 8-4
//{$STAMP BS2p}
//{$PBASIC 2.5}
//Test Program for RS-232 TTL I/O using RTS Handshaking
//for Embedded Systems Desktop Integration Chapter 8
//Copyright 2004 - Oliver H. Bailey
//
// Pins are assigned in the following order:
// Pin 0 - |
Transmit Data |
(TX or TD) => |
||
// Pin 1 - |
Receive Data |
(RX or RD) <= |
||
// Pin 2 |
- |
Clear To Send |
(CTS) |
=> |
// Pin 3 |
- Request To Send (RTS) |
<= |
||
// |
|
|
|
|
TX CON 0 |
|
|
|
|
RX CON 1
CTS CON 2
312 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
|
|
extern "C" { |
|
|
void UserMain(void * pd); |
|
|
} |
|
|
const char * AppName="serialtest"; |
|
|
void UserMain(void * pd) |
|
|
{ |
|
|
InitializeStack(); |
// Initialize TCP/IP |
|
OSChangePrio(MAIN_PRIO); |
// Change to priority 50 |
|
EnableAutoUpdate(); |
// Allow program updates |
|
SerialClose(DATAPORT); |
// Close current Data Port |
|
SerialClose(DEBUGPORT); |
// Close current Debug Port |
|
InitGDBStubNoBreak(DEBUGPORT, 57600); |
// Initialize Debugger |
|
int fddata = OpenSerial(DATAPORT, 9600, 1, 8, eParityNone); writestring(fddata, "Application started\r\n"); EnableTaskMonitor();
while (1)
{
OSTimeDly(20);
writestring(fddata, "Hello from Embedded Systems!\r\n");
}
}
Notice how we close both data and debug serial ports and then reopen them. You’ll also notice that we are using the writestring C function instead of printf or iprintf. This is because we have not redirected where the stdin and stdout go. Instead, writestring uses the file descriptor from the OpenSerial function call. We could check fddata to see it is valid, but the purpose is to demonstrate the process. If you have mtty attached you will see the message “Hello from Embedded Systems!” displayed every 20 clock ticks. The same data is being sent to the BS2 via the TTL level RS-232.
Now that we’ve tested receiving data from the BS2 it’s time to test sending data from the BS2. Since we don’t need the debugger right now, we can disconnect the power from the SB72 I/O card and disconnect the card from the main SB72 board. Our first task is to connect the power and ground wires to the SB72 from our BS2 board. The center pin of the J8 connector on the SB72 is ground. Either of the outside pins on J8 are +5. Make
314 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
#define MSG_TO_CLOSED "Connection Timeout, Connection will be closed\r\n"
#define DATAPORT 0 |
// Data com port |
#define DEBUGPORT 1 |
// Data debug port |
#define BAUDRATE 9600 |
// Serial baud rate |
#define STOPBITS 1 |
// Stop bits |
#define DATABITS 8 |
// Data bits |
#define TCPIP_PORT 23 |
// Telnet port |
extern "C" { |
|
void UserMain(void * pd); |
|
} |
|
const char * AppName="TestTCP"; |
// Set Application Name |
void UserMain(void * pd) |
|
{ |
|
InitializeStack(); |
// Initialize TCP/IP Stack |
OSChangePrio(MAIN_PRIO); |
// Move priority up to 50 |
EnableAutoUpdate(); |
// Allow Ethernet based updates |
EnableTaskMonitor(); |
// Enable Task Monitor |
SerialClose(DATAPORT); |
// Close and reopen data port with |
|
// new parameters |
int fdserial = OpenSerial(DATAPORT, BAUDRATE, STOPBITS, DATABITS, eParityNone);
writestring(fdserial, "Application Starting\r\n"); |
// Display start message |
|
int fdlisten = listen(INADDR_ANY, TCPIP_PORT, 5); |
// listen for any remote request |
|
if (fdlisten > 0 ) |
// Connection request = fdlisten > 0 |
|
{ |
|
|
IPADDR host_addr; |
// address of connecting client |
|
WORD port; |
// port request came in on |
|
writestring(fdserial, MSG_WAITING); |
// display wait message |
|
while (1) |
// Do forever |
|
{ |
|
|
int fdnet = accept(fdlisten, &host_addr, &port, 0); |
// accept connection request |
|
writestring(fdserial, MSG_CONNECT); |
// Display connect message |
|
writestring(fdnet, MSG_CONNECT); |
// Display message to telnet |
|
int ip_timeout = 0; |
// Set timeout to zero |
|
while (fdnet > 0) |
// While active socket connection |
|
{ |
|
|
fd_set read_file; |
// create read file descriptor |
|
fd_set error_file; |
// create error file descriptor |
|
316 |
|
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
|
|
|
|
|
|
|
|
|
close(fdnet); |
// close telnet port after message |
|
|
fdnet = 0; |
// Zero file descriptor for socket |
|
} |
|
|
|
} |
|
|
|
else |
// Else we have no pending events so |
|
|
{ |
|
|
|
ip_timeout++; |
// increment ip_timout counter |
|
|
if(ip_timeout > TIMEOUT) |
// timeout period exceeded |
|
|
{ |
|
|
|
|
writestring(fdserial, MSG_TO); |
// Send Stamp timeout message |
|
|
writestring(fdnet, MSG_TO); // send connected socket device timeout message |
|
|
|
close(fdnet); |
// close socket |
|
|
writestring(fdserial, MSG_TO_CLOSED);// send close message to BS2 |
|
|
|
fdnet = 0; |
// Set file descriptor for socket to zero |
|
} |
|
|
|
} |
|
// End of else statement |
|
} |
|
// End of open socket handler |
|
} |
|
// End of While (1) loop |
} |
|
|
// End of Connection Request Loop |
} |
|
|
// End of main program |
Every connected device has an associated file descriptor once opened. In Listing 8-6 the BS2 is the fdserial device and the telnet program is the fdnet device. The fdlisten descriptor is the socket waiting for a connection request, whereas the fdnet descriptor is the opened socket device. The call to select is looking for an event change so further processing can take place. If an fdnet device is opened and receiving data, a timeout counter is kept. If no data is received before the counter expires, the opened socket is closed.
For the sake of clarity I used text messages for status, but since we are communicating between machines, we can replace those messages with single-byte numeric values in the finished prototype. If you compile and download this program, you need to connect the mtty program to port 0, at 9600 baud, 8 data bits, 1 stop bit, and no parity. On the Windows OS, open a command box and execute the telnet program as shown in Figure 8-31.
318 |
Chapter 8 / The BASIC Stamp 2p Prototype |
|
|
bracket). Pressing these control keys will allow you to enter telnet command sequences that are not echoed to the SB72. Remember, the telnet program is a simple Ethernet-based terminal program. It will display characters sent from the Stamp and the Stamp will echo characters received from telnet. I kept the mtty program attached to monitor data traffic. Figure 8-32 shows the data that was seen by the BASIC Stamp.
When the telnet program opened a socket to the SB72 a “Connection Opened” message was sent to the BS2. I typed “This is a test” two times and the BS2 responded with “This is the BS2p Stamp” message. Nothing further was sent from the telnet program, so after a couple of minutes the SB72 closed the connection. The first message was a “Connection Timeout” message, which was followed by a close message. Once closed, the SB72 goes back into listen mode. The final code listing for the BS2 is in Listing 8-7.
Listing 8-7
//{$STAMP BS2p}
//{$PBASIC 2.5}
//Test Program for RS-232 TTL I/O using RTS/CTS Handshaking
//for Embedded Systems Desktop Integration Chapter 8
//Copyright 2004 - Oliver H. Bailey
//
// Pins are assigned in the following order:
// Pin 0 - |
Transmit Data |
(TX or TD) |
=> |
||
// Pin 1 |
- |
Receive Data |
(RX or RD) |
<= |
|
// Pin 2 |
- |
Clear To Send |
(CTS) |
=> |
|
// Pin 3 |
- Request To Send (RTS) |
<= |
|||
// |
|
|
|
|
|
TX |
CON 0 |
|
|
|
|
RX |
CON 1 |
|
|
|
|
CTS CON 2
RTS CON 3
InData VAR Byte
OutData VAR Byte
