0

how to read multiple bytes from MKR1000 WiFi
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

how to read multiple bytes from MKR1000 WiFi

by douglaskbell on Fri Sep 23, 2016 5:00 pm

I want my Arduino MKR1000 WiFi server's client connection to read a chunk of data.
Remote end sends the data chunk to MKR.
How does Arduino read multiple bytes?
I thought it would be something like: client.read( buf, count );

Code: Select all | TOGGLE FULL SIZE
WiFiServer server( port );

void setup()
{
WiFi.begin(ssid, pass);
server.begin();
}

void loop()
{
client = server.available();   // listen for incoming clients
if( client )                             // if you get a client,
{
  while( client.connected() )             // loop while the client's connected
  {
   int bytes_avail = client.available();
    if( bytes_avail )              //bytes available to read from the client,
    {
            unsigned char* buf;
            client.read( buf, bytes_avail );

douglaskbell
 
Posts: 51
Joined: Thu Jul 21, 2016 2:50 pm

Re: how to read multiple bytes from MKR1000 WiFi

by adafruit_support_rick on Sun Sep 25, 2016 1:14 pm

You aren't actually creating a buffer for the data, just an uninitialized pointer The data is just going someplace random.

Code: Select all | TOGGLE FULL SIZE
    if( bytes_avail )              //bytes available to read from the client,
    {
            unsigned char buf[32];  //allocate a buffer of 32 bytes
            client.read( buf, bytes_avail );


Or:
Code: Select all | TOGGLE FULL SIZE
    if( bytes_avail )              //bytes available to read from the client,
    {
            unsigned char buf = malloc(bytes_avail);  //allocate a buffer of bytes_avail bytes
            client.read( buf, bytes_avail );


Just remember to free the buffer when you're done with it.

adafruit_support_rick
 
Posts: 35053
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: how to read multiple bytes from MKR1000 WiFi

by douglaskbell on Mon Sep 26, 2016 4:58 pm

Sorry -- I did not include the allocation source. But it wasn't the problem.

SOLUTION:
int chunk_sequence_index;
unsigned char* data_ptr = (unsigned char*)&chunk_sequence_index;
read_size_bytes = client.read( data_ptr, 1 );

The bug was that I passed '&chunk_sequence_index' to client.read, which caused compile error.

douglaskbell
 
Posts: 51
Joined: Thu Jul 21, 2016 2:50 pm

Please be positive and constructive with your questions and comments.