# Network library Netstation

**URL:** <https://discourse.processing.org/t/network-library-netstation/14365>\
**Category:** Libraries\
**Created:** [October 2, 2019, 8:28am UTC](https://discourse.processing.org/t/network-library-netstation/14365 "2019-10-02T08:28:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kb12592](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@kb12592](https://discourse.processing.org/u/kb12592)\
**Post date:** [October 2, 2019, 8:28am UTC](https://discourse.processing.org/t/network-library-netstation/14365/1 "2019-10-02T08:28:00Z")

</div>

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:_

```auto
//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:_

```auto
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](https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html)

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 2, 2019, 9:44am UTC](https://discourse.processing.org/t/network-library-netstation/14365/2 "2019-10-02T09:44:07Z")

</div>

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

```auto
// 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?

---

<div class="post-metadata">

**Author:** ![kb12592](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@kb12592](https://discourse.processing.org/u/kb12592)\
**Post date:** [October 2, 2019, 11:09am UTC](https://discourse.processing.org/t/network-library-netstation/14365/3 "2019-10-02T11:09:57Z")

</div>

Thanks for your reply.

Netstation is expecting a data event command in the format:

 ![Data%20event%20command](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4bdd36e69660942ebb66a9f872b3b35e9f263a6e.png)

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 2, 2019, 11:47am UTC](https://discourse.processing.org/t/network-library-netstation/14365/4 "2019-10-02T11:47:13Z")

</div>

see  
[https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html](https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html)

also from processing

 ![numinfo](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bc081c94f32a54b96ae5be7604fffe6abf7ae8f0.png)

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

---

<div class="post-metadata">

**Author:** ![kb12592](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@kb12592](https://discourse.processing.org/u/kb12592)\
**Post date:** [October 2, 2019, 2:54pm UTC](https://discourse.processing.org/t/network-library-netstation/14365/5 "2019-10-02T14:54:08Z")

</div>

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!
