chipKIT® Development Platform

Inspired by Arduino™

writeStream for integers?

Created Mon, 07 May 2012 22:43:02 +0000 by radiosky


radiosky

Mon, 07 May 2012 22:43:02 +0000

I want to collect integer data from the ADC and put it into an array. I would then like to send the array using TcpClient as shown in the TCPEchoServer example.

The problem is that the writeStream method does not seem to like the integer array and only accepts byte*. I am new to this language and do not know a way to cast around this. Am I going to have to somehow stuff integers into a byte array?

Thanks in advance for any suggestions.


dangeljs

Tue, 08 May 2012 01:16:46 +0000

What if you did a union? Something like:

union {
byte asBytes[20];
int asInt[10];
} foo;

I don't know much about what is the limiting factor in the 'writeStream', but if it is limited to only send a byte at a time serially this may work for you.


radiosky

Tue, 08 May 2012 06:40:35 +0000

Thank you for the suggestion. I will give that a try. I don't see Union in the Arduino Reference, but I assume that it doesn't bother to cover all of the basic C conventions. I tried:

[color=#0040FF]union comb { int datInt[10]; byte datByte[20]; };[/color]

and the compiler seemed happy enough. Tomorrow I will give it a test with writeStream.

Thanks again jim


dangeljs

Tue, 08 May 2012 13:36:21 +0000

I just realized that I may be setting you up for a little trouble if you didn't catch it. An integer on Chipkit is four bytes, so you may not have your memory space overlapped properly. The two options are:

union comb {
int16_t datInt[10];
byte datByte[20];
};

or use the full 4 bytes

union comb {
int datInt[5]
byte datByte[20];
};

I'm kind of blindly leading you as I haven't used it this way before, so I hope it works/helps.

Jason


davervw

Wed, 09 May 2012 00:04:04 +0000

You are allowed to cast your own data to a byte ptr to send it to your stream.

example using dummy methods:

// declaration of library function or such void fn_accepts_byte_ptr(byte* ptr, int size);

// client code void foo() { int my_value = 0; fn_accepts_byte_ptr((byte*)&my_value, sizeof(my_value)); }


radiosky

Fri, 11 May 2012 07:01:19 +0000

Thanks to you both for your suggestions. I could not get the Union to work, though it sounded to me like a great idea. I was able to get the casting method to work but not in a way that solves my problem.

I need a fairly compact stream of integer values to be sent to a PC via Ethernet. It needs to send samples as 16 bit integers. That does not seem to exist in the MPIDE. I also tried putting the ADC value into a word array, but it looks like that also takes 4 bytes per sample.

I can't find where the MAX32 & MPIDE data types are described. Any idea? Maybe a byte is 16 bits Ha!


radiosky

Fri, 11 May 2012 07:15:22 +0000

OK the answer was a combination of the two replys. The first example used a int16_t in the declaration. I did not see documentation for that. When I put it in the MPIDE it did not color code it. I assumed it was for C under MPLAB.

Anyway, I tried int16_t in the sample array and used the cast to a byte pointer in the writeStream method.

So thanks again to you both! :D


Ryan K

Fri, 11 May 2012 07:58:08 +0000

Hello,

A byte is 8 bits. It is found here:

MPIDE\hardware\pic32\cores\pic32\wiring.h typedef uint8_t byte;

There's a wiki about types and such:

http://en.wikipedia.org/wiki/C_data_types

int16_t are included in the inttypes.h header which is included in most C. Similar data types are:

uintN_t (unsigned int of N bits) intN_t (signed int of N bits)

normally N is defined for 8,16,32 and 64. This was just if you were interested :)

Best Regards, Ryan K Digilent