Mi scuso per l'insistenza ma le mie lacune in programmazione restano,
forse postando un pò di codice ci capiamo meglio. In realtà nn si tratta di un sensore ma di un modulo reader RFID (l'SL018 della Stronglink) tramite il quale devo leggere il codice UID di card di tipologia MIFARE. In genere questo codice è appunto 4 Byte. Di seguito vi riporto il codice tramite il quale si riesce a leggere l'id e stamparlo sulla seriale:
/**
* @title: StrongLink SL018/SL030 RFID reader demo
* @author: marc@marcboon.com
* @see: http://www.stronglink.cn/english/sl018.htm
* @see: http://www.stronglink.cn/english/sl030.htm
*
* Arduino to SL018/SL030 wiring:
* A3/TAG 1 5
* A4/SDA 2 3
* A5/SCL 3 4
* 5V 4 -
* GND 5 6
* 3V3 - 1
*/
#include <Wire.h>
#define TAG 17 // A3
char* mifare[] = { "1K", "Pro", "UltraLight", "4K", "ProX", "DesFire" };
void setup()
{
// Init serial port to host and I2C interface to SL018/SL030
Serial.begin(19200);
Wire.begin();
// Flash red led (only on SL018)
ledOn(true);
delay(500);
ledOn(false);
// Prompt for tag
Serial.println("Show me your tag");
}
void loop()
{
// Wait for tag
while(digitalRead(TAG));
// Read tag ID
readID();
// Wait until tag is gone
while(!digitalRead(TAG));
}
void ledOn(boolean on)
{
// Send LED command
Wire.beginTransmission(0x50);
Wire.send(2);
Wire.send(0x40);
Wire.send(on);
Wire.endTransmission();
}
int readID()
{
// Send SELECT command
Wire.beginTransmission(0x50);
Wire.send(1);
Wire.send(1);
Wire.endTransmission();
// Wait for response
while(!digitalRead(TAG))
{
// Allow some time to respond
delay(5);
// Anticipate maximum packet size
Wire.requestFrom(0x50, 11);
if(Wire.available())
{
// Get length of packet
byte len = Wire.receive();
// Wait until whole packet is received
while(Wire.available() < len)
{
// Quit if no response before tag left
if(digitalRead(TAG)) return 0;
}
// Read command code, should be same as request (1)
byte command = Wire.receive();
if(command != 1) return -1;
// Read status
byte status = Wire.receive();
switch(status)
{
[b] [b]case 0: // Succes, read ID and tag type
{
len -= 2;
// Get tag ID
while(--len)
{
byte data = Wire.receive();
if(data < 0x10) Serial.print(0);
Serial.print(data, HEX);[/b][/b]
}
// Get tag type
byte type = Wire.receive();
if(type > 0 && type < sizeof(mifare))
{
Serial.print(" Mifare ");
Serial.print(mifare[type - 1]);
}
Serial.println();
}
return 1;
case 0x0A: // Collision
Serial.println("Collision detected");
break;
case 1: // No tag
Serial.println("No tag found");
break;
default: // Unexpected
Serial.println("Unexpected result");
}
return -1;
}
}
// No tag found or no response
return 0;
}
La mia domanda è, quale modifica apportare a questa parte di codice
case 0: // Succes, read ID and tag type
{
len -= 2;
// Get tag ID
while(--len)
{
byte data = Wire.receive();
if(data < 0x10) Serial.print(0);
Serial.print(data, HEX);
per creare una stringa col contenuto dell' UID (del tipo EF4B34FF) che attualmente viene solo stampato sulla seriale?? L'obiettivo è inviarla successivamente su in UDP su una socket, procedura che già conosco partendo da una variabile di tipo char o string.
Scusatemi la brutalità dell'esempio, ma forse è l'unico modo per darvi un idea chiara del problema.
Grazie a tutti
EDIT by mod: per favore, racchiudi il codice all'interno degli appositi tag code