Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NOAAPORT Receiver in Xamarin C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.IO;
OpenConnection();
void OpenConnection() {
TcpClient nbsp = new TcpClient();
nbsp.Connect("1.nbsp.inoaaport.net", 2210);
if (nbsp.Connected)
Console.WriteLine("Success! Connecting as NBS1 Slave!");
NetworkStream ns = nbsp.GetStream();
try {
byte[] strProtocol = System.Text.Encoding.ASCII.GetBytes("NBS1");
for (int i = 0; i < strProtocol.Length; i++) {
ns.WriteByte(strProtocol[i]);
}
Console.WriteLine("Connected as NBS1 Slave Server!");
int bytes = 0;
int loops = 0;
int maxLoops = 6;
bool showTextOutput = true;
do {
// int startIndex = 7 + 8;
byte[] buffer = new byte[66];
bytes = ns.Read(buffer, 0, buffer.Length);
int p = 0;
long data_id = unpack_uint32(buffer, 0);
if (data_id == 1) {
Console.WriteLine("Detected Data ID: " + data_id);
if (data_id == 1)
Console.WriteLine("The data packet is correct!");
p += 12;
long seq_number = unpack_uint32(buffer, p);
Console.WriteLine("Detected Sequence Number: {0:d}", seq_number);
p += 4;
byte product_type = buffer[p];
Console.WriteLine("Product Type: {0:d}", product_type);
p++;
byte product_category = buffer[p];
Console.WriteLine("Product Category: {0:d}", product_category);
p++;
byte product_code = buffer[p];
Console.WriteLine("Product Code: {0:d}", product_code);
p++;
byte np_channel_index = buffer[p];
Console.WriteLine("NP Channel Index: {0:d}", np_channel_index);
p++;
string strFileName = System.Text.Encoding.ASCII.GetString(buffer, p, 37);
Console.WriteLine(strFileName);
p += 37;
byte f_zip = buffer[p];
p++;
Console.WriteLine("Compressed: {0:d}", f_zip);
uint num_block = unpack_uint16(buffer, p);
p += 2;
Console.WriteLine("Number of Blocks: " + num_block);
uint block_number = unpack_uint16(buffer, p);
p += 2;
Console.WriteLine("Block Number: " + block_number);
long block_size = unpack_uint32(buffer, p);
p += 4;
Console.WriteLine("Block Size: " + block_size);
Console.WriteLine(bytes + " header + envelope bytes processed");
if ((int)block_size > 0) {
int data_bytes = 0;
MemoryStream ms_data = new MemoryStream();
// data_bytes = ns.Read(data_buffer, 0, data_buffer.Length);
// ms_data.Write(data_buffer, 0, data_bytes);
for (int i = 0; i < (int)block_size; i++) {
byte[] data_buffer = new byte[1];
int c_bytes = ns.Read(data_buffer, 0, 1);
ms_data.Write(data_buffer, 0, c_bytes);
data_bytes++;
}
if (showTextOutput) {
string b64_data = pack_base64(ms_data.ToArray());
b64_data = "data:text/plain;base64," + b64_data;
Console.WriteLine(" -- Begin Data -- ");
Console.WriteLine(b64_data);
Console.WriteLine(" -- End Data -- ");
}
Console.WriteLine("Total Bytes Read of Data: {0}", data_bytes);
}
Console.WriteLine("Loops: {0} / {1}", loops + 1, maxLoops);
loops++;
}
} while (loops < maxLoops);
}
catch (Exception ex) {
Console.WriteLine("Error " + ex.Message);
}
finally {
nbsp.Close();
}
if (!nbsp.Connected)
Console.WriteLine("Connection Closed!");
}
string pack_base64(byte[] buffer) {
return System.Convert.ToBase64String(buffer);
}
uint unpack_uint16(byte[] buffer, int start) {
long u;
u = (buffer[start] << 8);
u += (buffer[start + 1]);
return (uint)u;
}
long unpack_uint32(byte[] buffer, int start) {
long u;
u = (buffer[start] << 24);
u |= (long)(buffer[start + 1] << 16);
u |= (long)(buffer[start + 2] << 8);
u |= (long)(buffer[start + 3]);
return u;
}
12:34:13 PM NoaaportReceiver.csx ran
Success! Connecting as NBS1 Slave!
Connected as NBS1 Slave Server!
Detected Data ID: 1
The data packet is correct!
Detected Sequence Number: 38422461
Product Type: 4
Product Category: 1
Product Code: 17
NP Channel Index: 0
lfpw_safr31+metar.141630_38422461
Compressed: 0
Number of Blocks: 1
Block Number: 1
Block Size: 470
66 header + envelope bytes processed
-- Begin Data --
data:text/plain;base64,QAwAAVJVS1dCQwIAAAAQCA4QIgFLREVOU0FGUjMxIExGUFcgMTQxNjMwDQ0KTUVUQVIgTEZMTCAxNDE2MzBaIDIzMDA0S1QgMTgwVjI3MCBDQVZPSyAzMy8xMCBRMTAxOSBOT1NJRz0NCk1FVEFSIExGTU4gMTQxNjMwWiAxNzAwNktUIENBVk9LIDI2LzIwIFExMDIwIE5PU0lHPQ0KTUVUQVIgTEZQRyAxNDE2MzBaIDAyMDA5S1QgMzQwVjA4MCBDQVZPSyAyNi8xMyBRMTAyMiBOT1NJRz0NCk1FVEFSIExGUE8gMTQxNjMwWiAzNjAwOEtUIENBVk9LIDI3LzEzIFExMDIyIE5PU0lHPQ0KTUVUQVIgTEZRUSAxNDE2MzBaIEFVVE8gMzUwMTFLVCAzMzBWMDUwIDk5OTkgRkVXMDM4IDIxLzE0IFExMDI0DQpOT1NJRz0NCk1FVEFSIExGUkIgMTQxNjMwWiBBVVRPIDAyMDEwS1QgQ0FWT0sgMjMvMTUgUTEwMjMgTk9TSUc9DQpNRVRBUiBMRlJTIDE0MTYzMFogQVVUTyAwNDAwNktUIDM0MFYwOTAgQ0FWT0sgMzEvMTIgUTEwMTkgTk9TSUc9DQo=
-- End Data --
Total Bytes Read of Data: 470
Loops: 1 / 6
Detected Data ID: 1
The data packet is correct!
Detected Sequence Number: 38422462
Product Type: 4
Product Category: 1
Product Code: 17
NP Channel Index: 0
lfpw_safr21+metar.141630_38422462
Compressed: 0
Number of Blocks: 1
Block Number: 1
Block Size: 217
66 header + envelope bytes processed
-- Begin Data --
data:text/plain;base64,QAwAAVJVS1dCQwIAAAAQCA4QIgFLREVOU0FGUjIxIExGUFcgMTQxNjMwDQ0KTUVUQVIgTEZPQSAxNDE2MzBaIEFVVE8gMDIwMDhLVCAzMzBWMDYwIENBVk9LIDMyLzE0IFExMDE5PQ0KTUVUQVIgTEZQQyAxNDE2MzBaIEFVVE8gMDIwMDlLVCBDQVZPSyAyNC8xMyBRMTAyMj0NCk1FVEFSIExGUFYgMTQxNjMwWiBBVVRPIDM1MDA3S1QgMzIwVjAzMCBDQVZPSyAyNi8xMiBRMTAyMj0NCg==
-- End Data --
Total Bytes Read of Data: 217
Loops: 2 / 6
Detected Data ID: 1
The data packet is correct!
Detected Sequence Number: 38422463
Product Type: 4
Product Category: 1
Product Code: 17
NP Channel Index: 0
lfpw_safr34+metar.141630_38422463
Compressed: 0
Number of Blocks: 1
Block Number: 1
Block Size: 1012
66 header + envelope bytes processed
-- Begin Data --
data:text/plain;base64,QAwAAVJVS1dCQwIAAAAQCA4QIgFLREVOU0FGUjM0IExGUFcgMTQxNjMwDQ0KTUVUQVIgTEZDUiAxNDE2MzBaIEFVVE8gMTQwMDVLVCAwODBWMTkwIENBVk9LIDMwLzA3IFExMDIxIE5PU0lHPQ0KTUVUQVIgTEZKTCAxNDE2MzBaIEFVVE8gMzEwMDdLVCAyODBWMzQwIENBVk9LIDI4LzEyIFExMDIxIE5PU0lHPQ0KTUVUQVIgTEZMUCAxNDE2MzBaIEFVVE8gMjIwMDZLVCBDQVZPSyAyOS8xMiBRMTAyMSBOT1NJRz0NCk1FVEFSIExGTFMgMTQxNjMwWiBBVVRPIDIzMDA2S1QgMTkwVjI1MCBDQVZPSyAzMC8xMSBRMTAyMSBOT1NJRz0NCk1FVEFSIExGTFUgMTQxNjMwWiBBVVRPIDE4MDExS1QgQ0FWT0sgMzEvMTIgUTEwMTk9DQpNRVRBUiBMRkxZIDE0MTYzMFogQVVUTyAxNTAwNUtUIDEyMFYxOTAgQ0FWT0sgMzMvMTAgUTEwMTkgTk9TSUc9DQpNRVRBUiBMRk1UIDE0MTYzMFogMjEwMDdLVCAxNzBWMjQwIENBVk9LIDI1LzE0IFExMDIxIE5PU0lHPQ0KTUVUQVIgTEZNVSAxNDE2MzBaIEFVVE8gMTUwMDdLVCAxMjBWMTgwIENBVk9LIDI1LzEzIFExMDIwIE5PU0lHPQ0KTUVUQVIgTEZPQiAxNDE2MzBaIEFVVE8gMDQwMDZLVCAzNjBWMDcwIENBVk9LIDI0LzEzIFExMDIzIE5PU0lHPQ0KTUVUQVIgTEZPSCAxNDE2MzBaIEFVVE8gMDIwMTNLVCBDQVZPSyAyMS8xNCBRMTAyNCBOT1NJRz0NCk1FVEFSIExGT0sgMTQxNjMwWiBBVVRPIDM2MDEwS1QgQ0FWT0sgMjYvMTMgUTEwMjIgTk9TSUc9DQpNRVRBUiBMRk9QIDE0MTYzMFogQVVUTyAwMjAwNUtUIDMzMFYwNjAgOTk5OSBTQ1QwNDYgMjQvMTMgUTEwMjMNCk5PU0lHPQ0KTUVUQVIgTEZQQiAxNDE2MzBaIEFVVE8gMDMwMDhLVCAzNDBWMDYwIENBVk9LIDI2LzEyIFExMDIyIE5PU0lHPQ0KTUVUQVIgTEZRQSAxNDE2MzBaIEFVVE8gMzUwMDdLVCAzMjBWMDYwIENBVk9LIDI3LzEyIFExMDIyPQ0KTUVUQVIgTEZTTiAxNDE2MzBaIEFVVE8gMzMwMDdLVCAzMDBWMzYwIENBVk9LIDI5LzEyIFExMDIxPQ0KTUVUQVIgTEZRQiAxNDE2MzBaIE5JTD0NCg==
-- End Data --
Total Bytes Read of Data: 1012
Loops: 3 / 6
Detected Data ID: 1
The data packet is correct!
Detected Sequence Number: 38422464
Product Type: 4
Product Category: 1
Product Code: 17
NP Channel Index: 0
skbo_spco20+speci.141635_38422464
Compressed: 0
Number of Blocks: 1
Block Number: 1
Block Size: 105
66 header + envelope bytes processed
-- Begin Data --
data:text/plain;base64,QAwAAVJVS1dCQwIAAAAQCA4QIgFLREVOU1BDTzIwIFNLQk8gMTQxNjM1DQ0KU1BFQ0kgU0tDTCAxNDE2MzBaIFZSQjAyS1QgOTk5OSBGRVcwNDAgU0NUMTIwIDMxLzE4IEEyOTk1PQ0K
-- End Data --
Total Bytes Read of Data: 105
Loops: 4 / 6
Detected Data ID: 1
The data packet is correct!
Detected Sequence Number: 38422465
Product Type: 4
Product Category: 1
Product Code: 15
NP Channel Index: 0
ukdv_faur56+ukfv.141600_38422465
Compressed: 0
Number of Blocks: 1
Block Number: 1
Block Size: 689
66 header + envelope bytes processed
-- Begin Data --
data:text/plain;base64,QAwAAVJVS1dCQwIAAAAQCA4QIgFLREVORkFVUjU2IFVLRFYgMTQxNjAwDQ0KVUtGViBHQU1FVCBWQUxJRCAxNDE4MDAvMTQyNDAwIFVLRFYtDQpTSU1GRVJPUE9MIEZJUi8xDQpTRUNOIEkNClNGQyBXSU5EOiBJU09MIDMzMC8xM0cxOE1QUw0KU0ZDIFZJUzogMzAwME0gQlIgSVNPTCAwNTAwTSBNT0QgU0hSQSBGRw0KTVQgT0JTQzogQUJWIDUwME0gQU1TTA0KU0lHIENMRDogT0JTQyBJU09MIENCIDYwMC9BQlYgMzA1ME0gQUdMDQpCS04gMDgwLzMwME0gQUdMIElOIFNIUkEgRkcgQU5EIEJSDQpNT04gT0JTQyBJU09MIENCIDgwMC9BQlYgMzA1ME0gQU1TTA0KQktOIDUwMC84MDBNIEFNU0wNClRVUkI6IE1PRCBTRkMvRkwxMDANClNFQ04gSUkNClBTWVM6IFJFQVIgUEFSVCBPRiBUUk9VR0gNClNGQyBXSU5EOiAzMjAvMDdHMTNNUFMNCldJTkQvVDoNCjMwME0gQU1TTCAzMDAvNDBLTUggUFMxOQ0KNjAwTSBBTVNMIDMwMC80MEtNSCBQUzE3DQoxNTAwTSBBTVNMIDMwMC80MEtNSCBQUzA3DQozMDAwTSBBTVNMIDI4MC80MEtNSCBQUzAzDQpDTEQ6IEJLTiBTVCAzMDAvNjAwTSBBR0wNCkJLTiBBQyAyMDAwL0FCViAzMDUwTSBBR0wNCk1PTiBCS04gQUMgMjIwMC9BQlYgMzA1ME0gQU1TTA0KRlpMVkw6IEFCViAzMDUwTSBBTVNMDQpNTk0gUU5IOiAxMDEzIEhQQSAvNzU5IE1NIEhHDQpNTk0gU0ZDIFQ6IFBTMTQ9DQo=
-- End Data --
Total Bytes Read of Data: 689
Loops: 5 / 6
Detected Data ID: 1
The data packet is correct!
Detected Sequence Number: 38422466
Product Type: 4
Product Category: 1
Product Code: 17
NP Channel Index: 0
lfpw_safr33+metar.141630_38422466
Compressed: 0
Number of Blocks: 1
Block Number: 1
Block Size: 579
66 header + envelope bytes processed
-- Begin Data --
data:text/plain;base64,QAwAAVJVS1dCQwIAAAAQCA4QIgFLREVOU0FGUjMzIExGUFcgMTQxNjMwDQ0KTUVUQVIgTEZLQiAxNDE2MzBaIDExMDAzS1QgMDUwVjE1MCBDQVZPSyAyOC8yMCBRMTAyMCBOT1NJRz0NCk1FVEFSIExGS0MgMTQxNjMwWiAzNjAwNktUIDMyMFYwMjAgOTk5OSBGRVcwNjYgMjcvMTQgUTEwMjEgTk9TSUc9DQpNRVRBUiBMRktGIDE0MTYzMFogQVVUTyAxNjAwNUtUIDEwMFYyMDAgQ0FWT0sgMzAvMTIgUTEwMjAgTk9TSUc9DQpNRVRBUiBMRktKIDE0MTYzMFogQVVUTyAyMDAwOEtUIENBVk9LIDI5LzE5IFExMDIwIE5PU0lHPQ0KTUVUQVIgTEZMQyAxNDE2MzBaIEFVVE8gVlJCMDVLVCA5OTk5IDAyNTAgUjI2L1AyNDAwIFIwOC9QMjQwMCAvLw0KTlNDIDMyLzEzIFExMDE5PQ0KTUVUQVIgTEZNUCAxNDE2MzBaIEFVVE8gMDcwMTBLVCAwNDBWMTAwIENBVk9LIDI1LzE4IFExMDIwIE5PU0lHPQ0KTUVUQVIgTEZNViAxNDE2MzBaIEFVVE8gMjQwMDlLVCAyMDBWMjcwIENBVk9LIDMxLzA4IFExMDIwIE5PU0lHPQ0KTUVUQVIgTEZTTCAxNDE2MzBaIEFVVE8gMDgwMDVLVCAwNTBWMTcwIENBVk9LIDMzLzExIFExMDE5IE5PU0lHPQ0K
-- End Data --
Total Bytes Read of Data: 579
Loops: 6 / 6
Connection Closed!
@czenzel

This comment has been minimized.

Copy link
Owner Author

commented Aug 11, 2016

Tested on Continuous .NET C# and F# IDE by Krueger Systems, Inc.
https://appsto.re/us/cf6rbb.i

The code does not use System.Text.Encoding.ASCII.GetString to parse data to text because of an issue of the app on the iPad crashing. This script was coded while traveling to see if this was possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.