Jump to content
Sign in to follow this  

Pcduino socket client send() issues

Recommended Posts

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) {
  sock = socket(AF_INET, SOCK_STREAM, 0);
  sockaddr_in serverAddress;

  connect(sock, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr));

  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

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, "%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 read operation is successful
      for(uint8_t i=0;i<16;i++)
        //print memory block
        Serial.print(" ");
     Serial.print("Sending id....");
     send(sock, str, 500, 0);


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
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);

Can someone help me figure this out? Thank you!

Share this post

Link to post
Share on other sites

  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.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this