Jump to content


Photo

Pcduino socket client send() issues


  • Please log in to reply
3 replies to this topic

#1 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 13 February 2015 - 06:57 PM

Hey all,

I am currently using a pcduino v2 and a SeeedStudio NFC shield. I have successfully run the example NFC sketches that come with the pcduino, now I would like to send some data over a socket to my java server when a card is tapped on the NFC. Here is my code so far:

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>


#include <core.h>
#include "Client.h"
#include "Serial.h"
#include "PN532.h"
#include "SPI.h"

#define SCK 13
#define MOSI 11
#define SS 10
#define MISO 12
#define SERVER_PORT htons(5010);

PN532 nfc(SCK, MISO, MOSI, SS);
char str[50];
int sock;

void setup(void) {
  Serial.begin(9600);
 
  sock = socket(AF_INET, SOCK_STREAM, 0);
  sockaddr_in serverAddress;
  serverAddress.sin_port=SERVER_PORT;
  serverAddress.sin_addr.s_addr=inet_addr("localhost");
  

   Serial.println("Hello!");
  Serial.println("Connecting...");      
  connect(sock, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr));
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX);
  
  // configure board to read RFID tags and cards
  nfc.SAMConfig();
}


void loop(void) {
  while(Serial.available() > 0){
      int ret = Serial.read();
      printf("%c", ret, ret);
  }
  uint32_t id;
  // look for MiFare type cards
  id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
  sprintf(str, "&#37;u", id);
  if (id != 0) 
  {
    Serial.print("Read card #"); Serial.println(id);
    
    uint8_t keys[]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
    if(nfc.authenticateBlock(1, id ,0x08,KEY_A,keys)) //authenticate block 0x08
    {
 
    //if authentication successful
    uint8_t block[16];
    //read memory block 0x08
    if(nfc.readMemoryBlock(1,0x08,block))
    {
      
        
       

    //if read operation is successful
      for(uint8_t i=0;i<16;i++)
      {
        //print memory block
        Serial.print(block[i],HEX);
        Serial.print(" ");
      }
        
    }
   
   }
     Serial.print("Sending id....");
     send(sock, str, 500, 0);
     close(sock);
     
  }

 delay(500);
}

Everything seems to work O.K. I get the following output from the LXTerminal BEFORE a card is tapped:

init spi mode: 0x8
bits per word: 8
max speed: 1200000 Hz(12000 KHZ)
setDataMode -> spi mode: 0x8
setBitOrder -> spi mode: 0x8
Hello!
Connecting...
Found chip PN53
Firmware ver. 50.1
Supports 6

However, once a card is tapped on the NFC, the LXTerminal shuts off and nothing else happens. I discovered that the following lines of code are causing the LXTerminal to crash


     send(sock, str, 500, 0);
     close(sock);

Can someone help me figure this out? Thank you!



#2 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 13 February 2015 - 06:57 PM

  send(sock, str, 500, 0);
   //  close(sock); // your program crashed because of this, when the sock is closed, it can not be accessed for the next loop, so send will fail.



#3 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 13 February 2015 - 06:57 PM

Thanks for the quick reply

I tried removing the close() function but the program is still crashing. Any other ideas?



#4 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 13 February 2015 - 06:57 PM

Code is very difficult to follow due to the indentations and empty lines smiley.gif

Congrats to lds for spotting the close in a loop.

I noticed few more problems:
a) how come your 3rd argument to send is 500 when size of str is maximum 50 ?
B) how come you don't check the return values for connect, send, etc, you might be trying to use an invalid socket if you don't check the return values.

Once these are sorted out, then we can proceed onwards ... so far nothing related to pcDuino smiley.gif but to C programming.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users