|
Intro to Physical Computing Syllabus code, circuits, & construction
|
Siteplayer Serial Communication |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
| The siteplayer has a serial port that you can use to send data to a PC or microcontroller, but there are a few tricks that you need to know about to use it. Once you know these tricks, sending serial data is simple. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Connecting to a PC
The siteplayer serial port operates at TTL levels, meaning that it sends data at 5V, and a logic 1 is +5V, and a logic 0 is 0V. To communicate with a PC, which uses RS232 serial levels, you'll need to invert the signal (in RS232, logic 1 is low, and logic 0 is high). To invert the signal, you can use a hex inverter chip (most manufacturers will carry this, search for a 7404 chip. The hex inverter takes the voltage at a given input and inverts it; if the voltage is high, it takes it low, and if it's low, it takes it high. Put both the TX and RX signals through the hex inverter, and connect it to a DB9 serial connector according to this schematic Note the 2.2K pulldown resistor on the serial TX pin. If you forget it, you'll have trouble getting a signal:
|
If you need true RS232 voltage levels, Maxim's MAX232 chip, or their D275 chip (which requires fewer external components) will do the job. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Connecting to a Microcontroller
To connect the siteplayer to a microcontroller such as the PIC or the BX-24, you don't need the hex inverter. Most microcontrollers operate at TTL levels already, so they'll read the siteplayer's serial signals just as is. Make sure you have the 2.2K resistor on the TX pin, though. Then connect the TX pin directly to the microcontroller's serial receive (RX) pin. Then program. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sending serial data out
Sending serial data out of the siteplayer in response to a web hit is simple. There is a variable called COM. You need to include the pcadef.inc file in your .spd file to use it. Put this line at the top of your .spd: $Include "C:\Program Files\Siteplayer\pcadef.inc" Then, to send data out, just set the COM variable equal to what you want to sent out. In the examples below, the COM object is changed by calling an SPI file called counter.spi. All the SPI file does is redirect the browser to the index page. It's the actual hit on the link that causes siteplayer to send the data. The link to send out a string in the index.htm file looks like this: <a href="counter.spi?com=Hello%20World">send Hello World!</a> Note that any character is interpreted as an ASCII value. If you want to send raw values, you need to send them in hexadecimal format, and precede them with a % symbol. In the example above, %20 is decimal 32, and 32 is the ASCII value for a space. Here's another example: <a href="counter.spi?com=%01%FF">send the value 1 and the value 255 (FF in hexadecimal)</a> The siteplayer Manual has more notes on the COM object, including a table of how to set the baud rate. Here's a link example to set it to 9600 baud: <a href="counter.spi?baud=65406">Click to set Baudrate to 9600</a> And here's the code that you'd put in your .spd file if you wanted it to default to 9600 baud on startup: org 0FF1Ah baudrate dw 65015 ; set baud rate to 2400 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Receiving the data on a microcontroller
To receive the data from the siteplayer on a microcontroller, you need to make sure you are receiving at the same baud rate as the siteplayer is sending at. Here's a quick example in picBasic Pro for the PIC: ' open the file with the definitions that we need: INCLUDE "modedefs.bas" ' Set Debug pin port DEFINE DEBUG_REG PORTB ' Set Debug pin BIT (RB0 in this case) DEFINE DEBUG_BIT 0 ' Set Debug baud rate DEFINE DEBUG_BAUD 9600 ' Set Debug mode: 0 = true, 1 = inverted DEFINE DEBUG_MODE 1 ' set variables: inData VAR BYTE inData = 0 Pause 500 main: ' listen for 100 milliseconds for incoming data. ' if there is none, go to the "onward" label: Serin2 PORTC.7, 84, 100, onward, [inData] Debug "indata", inData goto main onward: debug "no data" goto main |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Receiving Serial Data
The siteplayer can receive serial data as well. You can use the serial port to just exchange bytes (read and write from memory), or you can send serial commands to do other things, like send UDP messages, return the status of the siteplayer, and so forth. Though all the possible commands are listed below, we will deal mainly with reading from and writing to the siteplayer's memory here. First you send a byte indicating what serial function you want to use (read data from a register, write data to a register, etc.), then you send how many bytes you plan to read or write, then you send the bytes. A typical command is a few bytes long and looks like this: [Command byte] [address in memory to deal with] [data bytes] Following are all of the serial commands:
NOP No operation. Makes the siteplayer do nothing. It's recommended that when you start talking to a siteplayer, you start with 20 bytes of value 00h. The siteplayer will interpret these as NOPs, and serial communication will be properly established. Status Returns the status of the siteplayer. The siteplayer will send back one byte in response to this command. See manual for details Reset Reboots the siteplayer. ComParams If you need to change the baudrate of the serial port on the fly, this is how you'd do it. See manual for details. Write, WriteX Writes values to the siteplayer. You send what address you want to write to and how many bytes you want to write. Write works for the first 255 addresses, and writeX works for the higher addresses (extended addresses require more than one byte to identify, because their addresses are above 255.) The number of bytes you want to read is sent in the command byte. The formula is as follows: Command + number of bytes to send - 1. So to send one byte, the command is 80h, for two bytes it's 81h, for three bytes, it's 82h, and so forth. The bytes you send are sent low order to high order, in other words, in the reverse order that you read them. So if you were putting a value into a word-sized variable in the siteplayer (which is two bytes), you'd put the low byte first and the high byte second. For example, the value 261: 261 = 0105h You'd reverse the bytes, and using picBasic Pro, it'd look like this: ' put two bytes ($81) at siteplayer address 01h ($01): Serout2 PORTC.6, 84, [$81, $01, $05, $01] Note that to use writeX, you'd usd the command $90 instead of $80, and you'd send two address bytes (in reverse order) instead of one, like so: ' put one byte ($90) at siteplayer address $FF20h ($20 $FF) ' (note: this particular line enables UDP receives, ' because address $FF20h is the UDPrcvr register): Serout2 PORTC.6, 84, [$90, $20, $FF, $01] Read, ReadX Read bytes from the siteplayer's memory. You send the number of bytes you want to read and the address to start reading from, and siteplayer sends the bytes from that address. Although it's easier to used the COM object to send serial out of the siteplayer, there may be times when you need data from the siteplayer, and there's no way to get it into the COM object. Similar to write and writeX, read handles lower addresses (0 - 255) and readX handles higher addresses (above 255). Also similar, the formula is as follows: Command + number of bytes to send - 1. So to send one byte, the command is C0h, for two bytes it's C1h, for three bytes, it's C2h, and so forth. The siteplayer will send bytes back starting with the lowest order byte. So if you ask for four bytes starting at address 47h, you'd get the value of 47h, then the value of 48h 49h, and 4Ah, in that order. The command would look like this: C3 47 or in picBasic Pro: Serout2 PORTC.6, 84, [C3, $47] You'd need to do a serin command right after the serout to get the reply. UDPSend Causes the siteplayer to send a UDP message, using whatever values are stored in the UDP special function registers. See the UDP notes for more detail. ReadBit, WriteBit, ToggleBit The siteplayer has a special area of memory that it addresses only as bit variables (02E0h - 02FFh). This is useful for storing things like the values of radio buttons and checkboxes on web pages. These commands are for addressing those areas. They work similar to the read and write commands. See the software manual for details. |
Note: the Serial Tester program is a good tool to learn what the byte values are for sending serial commands. In the bottom window, it lists the actual byte values it's sending to the siteplayer through the serial port, in hexadecimal notation. |