Network library Netstation

I want to be able to send time-stamps from Processing PDE to an EEG acquisition software called NetStation using the network library. NetStation has plug-ins and scripts available for Matlab, E-prime and Python, to send this time-stamp information, and I have been trying to use the matlab and python scripts to work out how to do this via processing – but I’ve had no luck so far, as I’m not familiar with these languages.

I’m pretty sure it is a formatting issue - whereby Netstation expects data to be send in specific C-type data formats.

Python code:

//Here I need to send "Q" as a single byte character and "NTEL" as a C-style string
message = struct.pack("=c4s", "Q", "NTEL") // beging session, send endianess
self._socket.write(message)
return self.GetServerResponse()

self._socket.write('A'); // start recording
message = struct.pack("=l", "T", ms_time); // time sync command - ms_time is long (32int)
self._socket.write(message);

self._socket.write('B'); //start recording

data_string = 'D%s%s%s%s' % (
            struct.pack('h', event_min_size),  // short
            struct.pack('l', current_time), // long
            struct.pack('l', default_duration), //long
            struct.pack('4s', markercode), // 4 character string
        )

self._socket.write(data_string); // where the time-stamp information is sent

self._socket.write('E'); //end recording

Processing IDE:

myClient.write('Q'+"NTEL");

myClient.write('A'); // this command works as expected, and pings back an OK response, which is a  'Z' ASCII or 90 in byte representation

int local_time = System.currentTimeMillis();// get current time in milliseconds 
myClient.write('T' + local_time); // not sure if I should be sending with a '+', and whether processing int is the same as python long - I know the Netstation acquisition software is expecting 4 bytes

myClient.write('B'); //start recording - this works

min_size = 12; number of bytes that will follow this number. Must be send as a python short (16int, expecting 2 bytes). - not sure how to send this, as processing IDE doesnt have a short int
int local_time = System.currentTimeMillis();// get current time in milliseconds 
int duration = 1; // duration is 1 millisecond
myClient.write('D'+min_size + local_time + duration + "CODE");

myClient.write('E'); //end recording - this works

/// also I do not know how struct.pack works in python - and whether this function works to covert the data into bytes before sending over. According to the python website
“By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct.”

Can I use Java ByteBuffer to do a similar thing? Or should I be converting to bytes and sending over in a different way?

Thanks for any help you can give!
https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html

1 Like

do you want to send a string?
try to print it first:

// myClient.write('D'+min_size + local_time + duration + "CODE");
int min_size = 12;
int local_time = millis();
println("local_time: "+local_time);
int duration = 1;
String sendit = 'D'+min_size + local_time + duration + "CODE";
println("sendit: "+sendit);
//myClient.write(sendit);
/*
local_time: 165
sendit: 246CODE
*/

i not think that is what you want?

Thanks for your reply.

Netstation is expecting a data event command in the format:

So rather than a string, it is expecting ‘D’ as a single byte character, followed by an unsigned short, two longs and a 4 character string. I think packing these 4 items in a structure is also an important aspect, but I don’t know how I’d do that in processing. The additional variables in the table are optional information that can also be sent.

1 Like

see
https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html

also from processing

so better avoid the " byte + int " syntax and
use
…write(b);
…write(i);

1 Like

That worked! I converted all my integers to byte arrays of the required size, then sent over each variable in a separate myClient.write(x) line, rather than trying to stitch them together with ‘+’.

Thank you!

1 Like