Basic stamp tokenizer
I have tried to make a compiler with c# for the BS2 Basic Stamp. I was able
to get the Tokenizer working and the port control working but I do
not know what format to send the data down to the BS2 controller.
to get the Tokenizer working and the port control working but I do
not know what format to send the data down to the BS2 controller.

Comments
The Tokenizer documents describe how to download the tokenized program and it consists of a reset , Stamp identification and then writing the tokenized code to the Stamp's EEPROM.
Each write is an eighteen byte packet which consists of a 1 byte EEPROM address , 16 byte data and 1 byte checksum.
Here is an example of something I have used for the reset and the ID routines using VB
Private Sub reset() _port.DtrEnable = True Thread.Sleep(2) _port.DtrEnable = False _port.BreakState = True Thread.Sleep(52) _port.BreakState = False Thread.Sleep(50) _port.ReadExisting() End Sub Private Sub ID_BS2() _port.Write("B") / Wait for 2 byte reply _port.Write("S") / Wait for 2 byte reply _port.Write(2) / Wait for 2 byte reply _port.Write(Chr(0)) / Wait for 2 byte reply End SubThat is followed by the write routine , I believe I had a 50 mSec pause between each write , I'm not sure whether thats neccessary or not.
Jeff T.
Actually, I made reset and ID check program.
How can I make eighteen byte packet?
[noparse][[/noparse]Each write is an eighteen byte packet which consists of a 1 byte EEPROM address , 16 byte data and 1 byte checksum.]
do I have to send EEPROM data to BS2 controller?
I tryied to send Packetbuffer to BS2 controller.
give me a advice.
Jeff T.
·
Here is PBASIC code snippet that shows how to form the packet. That's right, one BASIC Stamp can be used to program another one. The formation of the packet would be the same in C#
You can find more information here.
FOR ix=0 TO 127 ' send 128 blocks of 16 bytes ' total 2048 bytes ' can be modified to send only ' occupied blocks (per ALT-M). cksm=ix+128 ' initialize checksum ' first block is adrs 128. SEROUT td,$4054,[noparse][[/noparse]ix+128] ' send block adrs. FOR jx=0 TO 15 ' 16 bytes per block. READ ix*16+jx,bite ' get byte from adrs. cksm=cksm+bite ' update checksum. SEROUT td,$4054,[noparse][[/noparse]bite] ' xmit byte to clone. NEXT ' next byte. SEROUT td,$4054,[noparse][[/noparse]-cksm//256] ' xmit checksum, note the math twos comp mod 256 ' now, target programs this eeprom block ' which usually takes 3-4 milliseconds. ' It returns a 0-1-0 pulse (ascii null, high pulse 0.9375 millisecond) ' when it is ready for next block. PULSIN rd,1,handshake ' wait for handshake, 9 bit periods 0.9375 milisecond high, could wait for ascii null IF handshake=0 THEN failed ' failed if no handshake, communication error,▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Packetbuffer is actual packet data to download to the BASIC stamp. and this data begins at element 0 and
constists of packets of 18 bytes each.
ELEMENT 0...17 : Packet number 1
...
ELEMENT 2286...2303 : Packet number 128
This means I should donwload packetbuffer to Basic stamp.
If it is correct, I have some question.
PBASIC program ;
' {$STAMP BS2}
' {$PORT COM3}
' {$PBASIC 2.5}
x var word
here:
for x = 1 to 100
pulsout 8, 500
pause 10
next
pause 500
for x = 1 to 100
pulsout 8, 1000
pause 10
next
pause 500
goto here
And I programmed c# as follows, it just download part, and Packetcount was 4;
unsafe void Compile_button_Click(object sender, EventArgs e)
{
bool ret;
TModuleRec tModRec = new TModuleRec();
string s = richTextBox1.Text;
tModRec.SourceSize = (int)(s.Length);
ret = tokenizer_compile(&tModRec, s, false, true, null);
MessageBox.Show(ret.ToString());
SerialPort SP = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
SP.Open();
SP.DtrEnable = true;
SP.BreakState = true;
Thread.Sleep(2);
SP.DtrEnable = false;
Thread.Sleep(36);
SP.BreakState = false;
Thread.Sleep(20);
SP.DiscardInBuffer();
SP.WriteLine("B");
SP.DiscardInBuffer();
SP.ReadLine();
Console.WriteLine(SP.ReadByte());
SP.WriteLine("S");
SP.DiscardInBuffer();
SP.ReadLine();
Console.WriteLine(SP.ReadByte());
SP.WriteLine("2");
SP.DiscardInBuffer();
SP.ReadLine();
Console.WriteLine(SP.ReadByte());
SP.WriteLine("0");
SP.DiscardInBuffer();
SP.ReadByte();
Console.WriteLine(SP.ReadByte());
byte[noparse]/noparse p0 = new byte[noparse][[/noparse]72];
for (i = 0; i < 72; i++)
{
richTextBox2.Text += "$" + System.Convert.ToString(tModRec.PacketBuffer, 16);
p0[noparse][[/noparse]i] += tModRec.PacketBuffer[noparse][[/noparse]i];
}
SP.Write(p0, 0, 18);
SP.DiscardInBuffer();
SP.ReadByte();
Console.WriteLine(SP.ReadByte());
SP.Write(p0, 18, 18);
SP.DiscardInBuffer();
SP.ReadByte();
Console.WriteLine(SP.ReadByte());
SP.Write(p0, 36, 18);
SP.DiscardInBuffer();
SP.ReadByte();
Console.WriteLine(SP.ReadByte());
SP.Write(p0, 54, 18);
SP.DiscardInBuffer();
SP.ReadByte();
Console.WriteLine(SP.ReadByte());
SP.WriteLine("0");
MessageBox.Show(SP.ReadExisting());
SP.Close();
}
motor dosn't work and console shows as follows;
190
173
206
0
64
232
51
Please, give me an advice.
Post Edited (k lee) : 9/28/2009 2:04:41 AM GMT