@@ -890,6 +890,67 @@ - (BOOL) setTAGWRDAT:(UInt16)TAGWRDATAddr data_word:(UInt16)word data_offset:(UI
890890 }
891891}
892892
893+ - (BOOL ) TAGWRDAT_SEL : (UInt16)bank {
894+
895+ @synchronized (self) {
896+ if (connectStatus!=CONNECTED)
897+ {
898+ NSLog (@" Reader is not connected or busy. Access failure" );
899+ return false ;
900+ }
901+ connectStatus=BUSY;
902+ }
903+ [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
904+ [self .recvQueue removeAllObjects ];
905+ [self .cmdRespQueue removeAllObjects ];
906+
907+ // Initialize data
908+ CSLBlePacket* packet= [[CSLBlePacket alloc ] init ];
909+ NSData * payloadData;
910+
911+ NSLog (@" ----------------------------------------------------------------------" );
912+ NSLog (@" TAGWRDAT_SEL - Used to access the tag write buffer. The buffer is set up as a 16 register array." );
913+ NSLog (@" ----------------------------------------------------------------------" );
914+ unsigned char TAGWRDAT_SEL[] = {0x80 , 0x02 , 0x70 , 0x01 , 0x08 , 0x0A , bank & 0x7 , 0x00 , 0x00 , 0x00 };
915+ packet.prefix =0xA7 ;
916+ packet.connection = Bluetooth;
917+ packet.payloadLength =0x0A ;
918+ packet.deviceId =RFID;
919+ packet.Reserve =0x82 ;
920+ packet.direction =Downlink;
921+ packet.crc1 =0 ;
922+ packet.crc2 =0 ;
923+ packet.payload =[NSData dataWithBytes: TAGWRDAT_SEL length: sizeof (TAGWRDAT_SEL)];
924+
925+ NSLog (@" BLE packet sending: %@ " , [packet getPacketInHexString ]);
926+ [self sendPackets: packet];
927+
928+ for (int i=0 ;i<COMMAND_TIMEOUT_5S;i++) { // receive data or time out in 5 seconds
929+ if ([self .cmdRespQueue count ] != 0 )
930+ break ;
931+ [NSThread sleepForTimeInterval: 0 .001f ];
932+ }
933+ if ([self .cmdRespQueue count ] != 0 )
934+ payloadData = ((CSLBlePacket *)[self .cmdRespQueue deqObject ]).payload ;
935+ else
936+ {
937+ NSLog (@" Command timed out." );
938+ connectStatus=CONNECTED;
939+ [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes
940+ return false ;
941+ }
942+ connectStatus=CONNECTED;
943+ [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
944+ if (memcmp ([payloadData bytes ], TAGWRDAT_SEL, 2 ) == 0 && ((Byte *)[payloadData bytes ])[2 ] == 0x00 ) {
945+ NSLog (@" TAGWRDAT_SEL sent OK" );
946+ return true ;
947+ }
948+ else {
949+ NSLog (@" TAGWRDAT_SEL sent FAILED" );
950+ return false ;
951+ }
952+ }
953+
893954- (BOOL ) sendHostCommandWrite {
894955
895956 @synchronized (self) {
@@ -1534,14 +1595,17 @@ - (BOOL) startTagMemoryWrite:(MEMORYBANK)bank dataOffset:(UInt16)offset dataCoun
15341595
15351596 result = [self TAGACC_DESC_CFG: true retryCount: 7 ];
15361597 result = [self TAGACC_BANK: bank acc_bank2: 0 ];
1537- result = [self TAGACC_PTR: 0 ];
1598+ result = [self TAGACC_PTR: offset ];
15381599 result = [self TAGACC_CNT: count secondBank: 0 ];
15391600 result = [self TAGACC_ACCPWD: password];
15401601
1602+
15411603 ptr=0 ; ptr2=0 ;
1542- ptr+=offset;
15431604 while ([data length ] > ptr2) {
1544- if (count >= ptr) {
1605+ // set TAGWRDAT bank
1606+ [self TAGWRDAT_SEL: (ptr2/32 )];
1607+
1608+ if ([data length ] > ptr2) {
15451609 result = [self setTAGWRDAT: 0x0A09 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
15461610 ptr++; ptr2+=2 ;
15471611 }
@@ -1570,58 +1634,90 @@ - (BOOL) startTagMemoryWrite:(MEMORYBANK)bank dataOffset:(UInt16)offset dataCoun
15701634 ptr++; ptr2+=2 ;
15711635 }
15721636 if ([data length ] > ptr2) {
1573- result = [self setTAGWRDAT: 0x0A0A data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1637+ result = [self setTAGWRDAT: 0x0A10 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
15741638 ptr++; ptr2+=2 ;
15751639 }
1576-
1577- result = [self sendHostCommandWrite ];
1578-
1579- // wait for the command-begin and command-end packet
1580- for (int i=0 ;i<COMMAND_TIMEOUT_5S;i++) { // receive data or time out in 5 seconds
1581- if ([self .cmdRespQueue count ] >= 2 )
1582- break ;
1583- [NSThread sleepForTimeInterval: 0 .001f ];
1640+ if ([data length ] > ptr2) {
1641+ result = [self setTAGWRDAT: 0x0A11 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1642+ ptr++; ptr2+=2 ;
15841643 }
1585-
1586- if ([self .cmdRespQueue count ] < 2 ) {
1587- NSLog (@" Tag read command timed out." );
1588- connectStatus=CONNECTED;
1589- [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
1590- return false ;
1644+ if ([data length ] > ptr2) {
1645+ result = [self setTAGWRDAT: 0x0A12 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1646+ ptr++; ptr2+=2 ;
15911647 }
1592-
1593- // command-begin
1594- recvPacket = ((CSLBlePacket *)[self .cmdRespQueue deqObject ]);
1595- if (([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 02" ] &&
1596- [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0080" ]) ||
1597- ([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 01" ] &&
1598- [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0000" ])
1599- ) {
1600- self.lastMacErrorCode =0x0000 ;
1601- NSLog (@" Receive read command-begin response: OK" );
1648+ if ([data length ] > ptr2) {
1649+ result = [self setTAGWRDAT: 0x0A13 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1650+ ptr++; ptr2+=2 ;
16021651 }
1603- else
1604- {
1605- NSLog (@" Receive read command-begin response: FAILED" );
1606- connectStatus=CONNECTED;
1607- [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
1608- return FALSE ;
1652+ if ([data length ] > ptr2) {
1653+ result = [self setTAGWRDAT: 0x0A14 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1654+ ptr++; ptr2+=2 ;
16091655 }
1610-
1611- // decode command-end
1612- recvPacket = ((CSLBlePacket *)[self .cmdRespQueue deqObject ]);
1613- if (
1614- ([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 02" ] && [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0180" ] && ((Byte *)[recvPacket.payload bytes ])[14 ] == 0x00 && ((Byte *)[recvPacket.payload bytes ])[15 ] == 0x00 ) ||
1615- ([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 01" ] && [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0100" ] && ((Byte *)[recvPacket.payload bytes ])[14 ] == 0x00 && ((Byte *)[recvPacket.payload bytes ])[15 ] == 0x00 )
1616- )
1617- NSLog (@" Receive read command-end response: OK" );
1618- else
1619- {
1620- NSLog (@" Receive read command-end response: FAILED" );
1621- connectStatus=CONNECTED;
1622- [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
1623- return FALSE ;
1656+ if ([data length ] > ptr2) {
1657+ result = [self setTAGWRDAT: 0x0A15 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1658+ ptr++; ptr2+=2 ;
1659+ }
1660+ if ([data length ] > ptr2) {
1661+ result = [self setTAGWRDAT: 0x0A16 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1662+ ptr++; ptr2+=2 ;
1663+ }
1664+ if ([data length ] > ptr2) {
1665+ result = [self setTAGWRDAT: 0x0A17 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1666+ ptr++; ptr2+=2 ;
16241667 }
1668+ if ([data length ] > ptr2) {
1669+ result = [self setTAGWRDAT: 0x0A18 data_word: ((((Byte*)[data bytes ])[ptr2] << 8 )+((Byte*)[data bytes ])[ptr2+1 ]) data_offset: ptr];
1670+ ptr++; ptr2+=2 ;
1671+ }
1672+ }
1673+
1674+ result = [self sendHostCommandWrite ];
1675+
1676+ // wait for the command-begin and command-end packet
1677+ for (int i=0 ;i<COMMAND_TIMEOUT_5S;i++) { // receive data or time out in 5 seconds
1678+ if ([self .cmdRespQueue count ] >= 2 )
1679+ break ;
1680+ [NSThread sleepForTimeInterval: 0 .001f ];
1681+ }
1682+
1683+ if ([self .cmdRespQueue count ] < 2 ) {
1684+ NSLog (@" Tag read command timed out." );
1685+ connectStatus=CONNECTED;
1686+ [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
1687+ return false ;
1688+ }
1689+
1690+ // command-begin
1691+ recvPacket = ((CSLBlePacket *)[self .cmdRespQueue deqObject ]);
1692+ if (([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 02" ] &&
1693+ [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0080" ]) ||
1694+ ([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 01" ] &&
1695+ [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0000" ])
1696+ ) {
1697+ self.lastMacErrorCode =0x0000 ;
1698+ NSLog (@" Receive read command-begin response: OK" );
1699+ }
1700+ else
1701+ {
1702+ NSLog (@" Receive read command-begin response: FAILED" );
1703+ connectStatus=CONNECTED;
1704+ [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
1705+ return FALSE ;
1706+ }
1707+
1708+ // decode command-end
1709+ recvPacket = ((CSLBlePacket *)[self .cmdRespQueue deqObject ]);
1710+ if (
1711+ ([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 02" ] && [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0180" ] && ((Byte *)[recvPacket.payload bytes ])[14 ] == 0x00 && ((Byte *)[recvPacket.payload bytes ])[15 ] == 0x00 ) ||
1712+ ([[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (4 , 2 )] isEqualToString: @" 01" ] && [[recvPacket.getPacketPayloadInHexString substringWithRange: NSMakeRange (8 , 4 )] isEqualToString: @" 0100" ] && ((Byte *)[recvPacket.payload bytes ])[14 ] == 0x00 && ((Byte *)[recvPacket.payload bytes ])[15 ] == 0x00 )
1713+ )
1714+ NSLog (@" Receive read command-end response: OK" );
1715+ else
1716+ {
1717+ NSLog (@" Receive read command-end response: FAILED" );
1718+ connectStatus=CONNECTED;
1719+ [self .delegate didInterfaceChangeConnectStatus: self ]; // this will call the method for connections status chagnes.
1720+ return FALSE ;
16251721 }
16261722
16271723 connectStatus=CONNECTED;
@@ -1687,7 +1783,7 @@ - (BOOL)stopTagSearch {
16871783 [self performSelectorInBackground: @selector (stopTagSearchBlocking ) withObject: (nil )];
16881784
16891785 for (int i=0 ;i<COMMAND_TIMEOUT_5S;i++) { // receive data or time out in 3 seconds
1690- ([[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.1 ]]);
1786+ ([[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.001 ]]);
16911787 if (connectStatus == CONNECTED)
16921788 break ;
16931789 }
0 commit comments