converting basic stamp2 codes into arduino codes

' ====================================================================================
'
' File...... rfid_rw_test.bs2
' Purpose... System-level test code for the Parallax RFID Read/Write Module
' Author.... Joe Grand, Grand Idea Studio, Inc. [www.grandideastudio.com]
' E-mail.... support@parallax.com
' Updated... 15 Dec 2009
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' ====================================================================================
' -----[ Program Description ]--------------------------------------------------------
'
' This program performs a system-level test of the Parallax RFID Read/
' Write Module by:
'
' 1) Reading tag's unique ID
' 2) Writing and verifying a block of data to the tag
' -----[ I/O Definitions ]------------------------------------------------------------
RFID_TX PIN 0 ' Connects to RFID R/W Module SIN
RFID_RX PIN 1 ' Connects to RFID R/W Module SOUT
' -----[ Constants ]------------------------------------------------------------------
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T9600 CON 84
#CASE BS2SX, BS2P
T9600 CON 240
#ENDSELECT
Baud CON T9600
' RFID R/W Module Commands
' Number of bytes returned in ()
RFID_Read CON $01 ' Read data from specified address,
' valid locations 1 to 33 (5)
RFID_Write CON $02 ' Write data to specified address,
' valid locations 3 to 31 (1)
RFID_Login CON $03 ' Login to tag with password (1)
RFID_SetPass CON $04 ' Change tag's password from old to new (1)
RFID_Protect CON $05 ' Enable/disable password protection (1)
RFID_Reset CON $06 ' Reset tag (1)
RFID_ReadLegacy CON $0F ' Read unique ID from EM4102 read-only tag
' (for backwards compatibility with Parallax
' RFID Card Reader, #28140 and #28340) (12)
' Memory map/address locations for EM4x50 tag
' Each address holds/returns a 32-bit (4 byte) value
ADDR_Password CON 0 ' Password (not readable)
ADDR_Protect CON 1 ' Protection Word
ADDR_Control CON 2 ' Control Word
' ADDR 3-31 are User EEPROM area
ADDR_Serial CON 32 ' Device Serial Number
ADDR_DeviceID CON 33 ' Device Identification
Copyright © Parallax Inc. RFID Read/Write Module, Serial (#28440) v1.0 6/29/2010 Page 10 of 10
' Status/error return codes
ERR_OK CON $01 ' No errors
ERR_LIW CON $02 ' Did not find a listen window
ERR_NAK CON $03 ' Received a NAK, could be invalid command
ERR_NAK_OLDPW CON $04 ' Received a NAK sending old password (RFID_SetPass),
' could be incorrect password
ERR_NAK_NEWPW CON $05 ' Received a NAK sending new password (RFID_SetPass)
ERR_LIW_NEWPW CON $06 ' Did not find a listen window after sending
' old password (RFID_SetPass)
ERR_PARITY CON $07 ' Parity error when reading data
' -----[ Variables ]------------------------------------------------------------------
buf VAR Byte(12) ' data buffer
idx VAR Byte ' index
idy VAR Byte
' -----[ EEPROM Data ]----------------------------------------------------------------
' -----[ Initialization ]-------------------------------------------------------------
Initialize:
PAUSE 250 ' let DEBUG open
DEBUG CLS ' clear the screen
DEBUG "Parallax RFID Read/Write Module Test Application", CR,
"------------------------------------------------", CR, CR
' -----[ Program Code ]---------------------------------------------------------------
Main:
DEBUG "Reading tag's unique serial number..."
Read_Tag:
SEROUT RFID_TX, Baud, ["!RW", RFID_Read, ADDR_Serial] ' Read tag's serial number
SERIN RFID_RX, Baud, [STR buf\5] ' Get status byte and data bytes
IF buf(0) <> ERR_OK THEN Read_Tag ' If we get an error, keep trying
' until the read is successful
FOR idx = 1 TO 4 ' Print data
DEBUG HEX2 buf(idx)
NEXT
DEBUG CR
DEBUG "Writing and verifying data to tag..."
Write_Tag:
SEROUT RFID_TX, Baud, ["!RW", RFID_Write, 3, $FE, $ED, $BE, $EF] ' Write $FEEDBEEF
' into address 4 (user EEPROM area)
SERIN RFID_RX, Baud, [buf(0)] ' Wait for status byte
IF buf(0) <> ERR_OK THEN Write_Tag ' If we get an error, keep trying
' until the write is successful
DEBUG "Success!", CR
DEBUG "End of test.", CR
END
' -----[ End of File ]----------------------------------------------------

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Need help with something?

This actually looks fairly straightforward to convert. I'd actually encourage the OP to find a tutorial or such on PBASIC, and sit down and try to convert the code over to Arduino C/C++; you'll learn an absolute ton in the process. Once you have something (even if it doesn't work or seems/feels "wrong"), post it here, and maybe we can help you take it the rest of the way.

Some hints for conversion:

  1. Anything at the "top" of this code with "CON" in it, refers to a "constant"; most of the constants have to do with the RFID reader/writer commands, but a few have to do with the serial hookup (those can be mostly ignored; you really just need to set up a SoftwareSerial connection @ 9600 baud, 8n1 - I believe)

  2. The section marked "variables" is simply defining a few global variables for use in the code; including one byte array

  3. Anything with a single quote is mainly a comment (so heed them!)

  4. Things like "Initialize:" and "Main:" are "labels"; they are something like functions in C/C++ (much more limited, though - this is BASIC after all)

  5. A lot of the things inside and outside of the "Initialize:" section should be done in setup() in your Arduino code

  6. The rest could also be called as functions from setup(); leaving loop() blank

  7. If you use loop(), just make sure to have an infinite loop at the very end to "halt" execution

  8. SEROUT/SERIN would be serial input/output commands; do similar things with the software serial library

  9. DEBUG is the same as a Serial.println() command

So - in effect, choose a couple of pins for TX/RX for the software serial, set it up for 9600 baud, 8n1 to communicate to the RFID hardware, then write the code converted from the PBASIC example; use Serial.println() to output data back to the Arduino terminal via the virtual USB serial port...

tnx for the hints, i'll try to convert these codes into arduino....if i ever encountered errors or probs i'll post em ryt away...

@Coding Badly.... hey sir...could you please take a look in our codings....the
5 volts dc to VCC
Pin 6 to SIN
Pin 8 to SOUT
0 volts to GROUND

Here is the sample code.....

#include <NewSoftSerial.h>
#define RFID_WRITE 0x02
#define txPin 6
#define rxPin 8


#define mem 7


#define first 3
#define second 12
#define third 4
#define fourth 16


NewSoftSerial mySerial(rxPin, txPin);


void setup()
{
Serial.begin(9600);
Serial.println("RFID Write Test");
mySerial.begin(9600);
pinMode(txPin, OUTPUT); 
pinMode(rxPin, INPUT); 
}





void loop()
{
int val;

mySerial.print("!RW");
mySerial.print(RFID_WRITE, BYTE);
mySerial.print(mem, BYTE);
mySerial.print(first, BYTE);
mySerial.print(second, BYTE);
mySerial.print(third, BYTE);
mySerial.print(fourth, BYTE);


if(mySerial.available() > 0) { 
val = mySerial.read();
if (val == 1) //If data was written successfully
{ Serial.println("Data written succesfully!");

}
else Serial.println("Error Code: ");
//If an error occured during writing, discard all data recieved from the RFID writer
}
delay(250);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

the following code generates and output of
Erro Code: 2
Erro Code: 2
Erro Code: 2
Erro Code: 2
Erro Code: 2

//////////////////////////////////////////////////////////////////////////////////////////////////////////
and according to the datasheet of the parallax 28440
Error Checking

Value Name Description
0x01 ERR_OK No Errors
0x02 ERR_LIW Could not find a Listen Window (LIW) from the tag
0x03 ERR_NAK Received a No Acknowledge (NAK), possible communication error or
invalid command/data
0x04 ERR_NAK_OLDPW Received a No Acknowledge (NAK) sending the current password during
the RFID SetPass command, possible incorrect password
0x05 ERR_NAK_NEWPW Received a No Acknowledge (NAK) sending the new password during the
RFID SetPass command
0x06 ERR_LIW_NEWPW Could not find a Listen Window (LIW) from the tag after setting the new
password during the RFID SetPass command
0x07 ERR_PARITY Parity error when reading data from the tag

///////////////////////////////////////////////////////////////////////////////
the error above correspond to the error 0x02 which is the ERR_LIW could not find a listen Window(LIW) from the tag...
i don't know what is the meaning of this..can somebody give me an advice how to solved it.... thank you in advance.....

///////////////////////////////////////////////////////////////////////////////

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

the following code generates and output of
Erro Code: 2

The code you presented cannot possibly output "Erro Code:" nor can it output " 2".

  • pls help
  • needed badly

I want to vote for both of those. How do I do that?

I'll vote for one, and you vote for the other.