Ga naar inhoud


Aanbevolen berichten

Geplaatst:

Ik denk, ga zelf effe proberen zo'n SC8IN1 mekaar te flansen maar loop al vast op wat linux C code kan iemand me vertellen wat de mcu hier antwoord op instuctie 0x47.???

 

 

Code:
   // get SC8in1 info  tmp[0]=0x47;  res=write(fd,tmp,1);  res=read(fd,tmp,10);  if ( res==0 ) return(-1); // ERROR !  // switch SC8in1 to normal mode  RESETDTR(fd);  // restore data  memcpy(&termio,&termiobackup,sizeof(termio));  if (tcsetattr(fd,TCSANOW,&termio) < 0) {    printf("ERROR: RS232 attributes\n");    return(-1);  }  if (tmp[res-7]!=0x90) return(-1); // ERROR !  // return result byte  return(tmp[res-6]); 

 

Wat is 'res' na de read functie.. 10 ofzo..

 

En returned ie hier 'return(tmp[res-6]); ' byte 4..

 

 

Ik hoop dat iemand het begrijpt...


Geplaatst:

Volgens mij gebeurt het volgende:

 

tmp[0]=0x47;

- Karakter 0 van array tmp op 0x47 gesteld.

 

res=write(fd,tmp,1);

- Schrijf 1 byte van array tmp (0x47), res is feitelijk aantal geschreven bytes

 

res=read(fd,tmp,10);

- Lees 10 bytes in array tmp, res is feitelijk aantal gelezen bytes

 

if ( res==0 ) return(-1); // ERROR !

- Indien het gelezen aantal bytes 0 is -1 teruggeven (Lezen is dus mislukt)

 

if (tmp[res-7]!=0x90) return(-1); // ERROR !

- Indien tmp[res-7] ongelijk aan 0x90 -1 teruggeven

 

return(tmp[res-6]);

- Geef byte tmp[res-6] retour

 

 

 

Conclusie: Het is dus afhankelijk van de inhoud van tmp wat er geretourneerd wordt.

 

Citaat:
Wat is 'res' na de read functie.. 10 ofzo..

Er worden 10 bytes gevraagd, echter res hoeft niet perse 10 te worden, wordt helaas niet op gecheckt.

 

Citaat:
En returned ie hier 'return(tmp[res-6]); ' byte 4..

Ja, of bagger als het aantal gelezen bytes groter is dan 0, maar kleiner dan 5.

Geplaatst:

Oke dan, je bevestigt m'n vermoeden. Ik weet alleen niet waarom hij nou 10 bytes wil. De aanwezige Kaarten worden in 1 byte gesteld.

 

Bijgevoegd de hele code welke in sommige CS Servers zit, voor als je het e.v.t. wilt zien.

 

Code:
 #include <fcntl.h>#include <stdio.h>#include <stdarg.h>#include <unistd.h>#include <string.h>#include <sys/ioctl.h>#include <termios.h>#include <errno.h>// ----------------------------------------------------------------------------void SETDTR(int fd) {  // sets the SC8in1 in command mode  int lineData;  ioctl(fd, TIOCMGET, &lineData);  lineData |= TIOCM_DTR;  ioctl(fd, TIOCMSET, &lineData);}// ----------------------------------------------------------------------------void RESETDTR(int fd) {  // sets the SC8in1 in normal mode  int lineData;  ioctl(fd, TIOCMGET, &lineData);  lineData &= ~TIOCM_DTR;  ioctl(fd, TIOCMSET, &lineData);}// ----------------------------------------------------------------------------int GETCTS(int fd) {  // returns the SC8in1 Status  // 0= no card was changed (inserted or removed)  // 1= one ore more cards were changed (inserted or removed)  int result;  int lineData;  ioctl(fd, TIOCMGET, &lineData);  result= (lineData & TIOCM_CTS) / TIOCM_CTS;  return(result-1);}// ----------------------------------------------------------------------------int readsc8in1( int fd) {  // Reads the card status  //  // the bits in the return bytes:  // bit0=1 means Slot1=Smartcard inside  // bit1=1 means Slot2=Smartcard inside  // bit2=1 means Slot3=Smartcard inside  // bit3=1 means Slot4=Smartcard inside  // bit4=1 means Slot5=Smartcard inside  // bit5=1 means Slot6=Smartcard inside  // bit6=1 means Slot7=Smartcard inside  // bit7=1 means Slot8=Smartcard inside  int res,i;  unsigned char tmp[128];  struct termios termio, termiobackup;  // backup data  tcgetattr(fd,&termio);  memcpy(&termiobackup,&termio,sizeof(termio));  // switch SC8in1 to command mode  SETDTR(fd);  // set communication parameters  termio.c_cc[VTIME] = 1; // working  termio.c_cflag = B9600|CS8|CREAD|CLOCAL;  if (tcsetattr(fd,TCSANOW,&termio) < 0) {    printf("ERROR: RS232 attributes\n");    return(-1);  }  // get SC8in1 info  tmp[0]=0x47;  res=write(fd,tmp,1);  res=read(fd,tmp,10);  if ( res==0 ) return(-1); // ERROR !  // switch SC8in1 to normal mode  RESETDTR(fd);  // restore data  memcpy(&termio,&termiobackup,sizeof(termio));  if (tcsetattr(fd,TCSANOW,&termio) < 0) {    printf("ERROR: RS232 attributes\n");    return(-1);  }  if (tmp[res-7]!=0x90) return(-1); // ERROR !  // return result byte  return(tmp[res-6]);}// ----------------------------------------------------------------------------int selectslot(int fd, int slot) {  // selects the Smartcard Socket "slot"  //  int res;  unsigned char tmp[128];  struct termios termio, termiobackup;  // backup rs232 data  tcgetattr(fd,&termio);  memcpy(&termiobackup,&termio,sizeof(termio));  // switch SC8in1 to command mode  SETDTR(fd);  // set communication parameters  termio.c_cc[VTIME] = 1; // working  termio.c_cflag = B9600|CS8|CREAD|CLOCAL;  if (tcsetattr(fd,TCSANOW,&termio) < 0) {    printf("ERROR: RS232 attributes\n");    return(-1);  }  // selecd select slot command to SC8in1  tmp[0]=0x53;  tmp[1]=slot&0x0F;  res=write(fd,tmp,2);  // switch SC8in1 to normal mode  RESETDTR(fd);  // restore rs232 data  memcpy(&termio,&termiobackup,sizeof(termio));  if (tcsetattr(fd,TCSANOW,&termio) < 0) {    printf("ERROR: RS232 attributes\n");    return(-1);  }  // return  return(0);}// ----------------------------------------------------------------------------int main (void) {  int fd,result;  printf("sc8in1 test utility\n");  // open com port  //  if((fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY|O_NONBLOCK )) <= 0) { // async#if defined(linux)   if((fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY|O_NONBLOCK )) <= 0) { // async#endif #if defined __WIN32__ || _MSC_VER  if((fd=open("com1",O_RDWR | O_NOCTTY|O_NONBLOCK )) <= 0) { // async#endif    int err = errno;    printf("ERROR: open rs232 port: %s\n", strerror(err));    return(-1);  }  result=readsc8in1(fd);  if ( result==-1 ) {    printf("No sc8in1 found\n");    return(-1);  }  if ( result==0 )  {    printf("No Smartcard inside sc8in1\n");  } else {    if ((result&0x01)!=0) printf("Slot1=Smartcard inside\n");    if ((result&0x02)!=0) printf("Slot2=Smartcard inside\n");    if ((result&0x04)!=0) printf("Slot3=Smartcard inside\n");    if ((result&0x08)!=0) printf("Slot4=Smartcard inside\n");    if ((result&0x10)!=0) printf("Slot5=Smartcard inside\n");    if ((result&0x20)!=0) printf("Slot6=Smartcard inside\n");    if ((result&0x40)!=0) printf("Slot7=Smartcard inside\n");    if ((result&0x80)!=0) printf("Slot8=Smartcard inside\n");  }  return(0);} 
Geplaatst:
Citaat:
Oke dan, je bevestigt m'n vermoeden. Ik weet alleen niet waarom hij nou 10 bytes wil. De aanwezige Kaarten worden in 1 byte gesteld.


Dat klopt, maar het kan zijn dat het commando 0x47 een algemeen Status/Info commando is, waarvan in dit geval alleen het aantal smartcards wordt gebruikt.

Voor de rest doet de code weinig, behalve dan wat veranderen aan de settings van de compoort. Schijnbaar is het commando 0x53 voor het selecteren van de smartcard.
Geplaatst:
Citaat:

Citaat:
En returned ie hier 'return(tmp[res-6]); ' byte 4..

Ja, of bagger als het aantal gelezen bytes groter is dan 0, maar kleiner dan 5.


Sterker nog, dit kan zelfs een SEGFAULT opleveren.
Erg slordig geprogrammeerd...
Geplaatst:

na command 0x47, welke goed binnenkomt op mcu uart, return ik 10 maal 0x90 maar de cs antwoord niet zoals het moet. Ik blijf nog ff puzzelen... <img src="/forums/images/graemlins/lezen.gif" alt="" />

Maak een account aan of log in om te reageren

Je moet een lid zijn om een reactie te kunnen achterlaten

Account aanmaken

Registreer voor een nieuwe account in onze community. Het is erg gemakkelijk!

Registreer een nieuwe account

Inloggen

Heb je reeds een account? Log hier in.

Nu inloggen
  • Wie is er online   0 leden

    • Er zijn geen geregistreerde gebruikers deze pagina aan het bekijken
×
×
  • Nieuwe aanmaken...