Using C# and Netduino to log data - c#

I am developing a telemetry platform for a nano-printing project. My team is using a Netduino 2 plus (not my first choice, but what are you going to do?) I am not at all familiar with C# and am a novice programmer, to be certain.
We have some code written that successfully polls an I2C temperature sensor and uses debug.print to write to the console. I would like this data written to a file, instead.
There are examples out there to transfer files from the SD card to a PC, but this seems unnecessary to me (though it may be completely necessary in order to not overrun the buffer?). Is there a call that will simply write the data to a file instead of writing to the console?
It is my understanding that we may need an application to listen to the serial port on the PC. It seems we would also need a corresponding application to write from the hardware. I've used microcontrollers in the past that simply open a serial and send over USB to a file location.
Here is the code we are using to print the data to the console:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
namespace NetduinoApplication1
{
public class Program
{
public static void Main()
{
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
OutputPort p = new OutputPort(Pins.GPIO_PIN_SDA, true);
p.Write(false);
p.Dispose();
// write your code here
byte[] Addr = new byte[1];
Addr[0] = 0x07;
byte[] TxBuff = new byte[9];
byte[] RxBuff = new byte[9];
I2CDevice.Configuration I2C_Configuration = new I2CDevice.Configuration(0x5A, 100);
I2CDevice I2C1 = new I2CDevice(I2C_Configuration);
I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateWriteTransaction(TxBuff) };
I2CDevice.I2CTransaction[] ReadTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateReadTransaction(RxBuff) };
while (true)
{
int iWriteCount = I2C1.Execute(WriteTran, 1000);
//Debug.Print("Write Count: " + iWriteCount.ToString());
led.Write(true);
Thread.Sleep(200);
int iReadCount = I2C1.Execute(ReadTran, 1000);
if (iReadCount >= 2)
{
int iVal = RxBuff[1] * 256 + RxBuff[0];
double Temperature = ((iVal * 0.02) - 273.15) * 9.0 / 5.0 + 32.0;
Debug.Print("Temperature: " + Temperature.ToString() + " deg F");
}
led.Write(false);
Thread.Sleep(200);
}
}
}
}
If I need to create an application, I'd like to do it, but I could really use some direction. A little experience here would go a long way.

So, a couple things come to mind.
If your Netduino has network connectivity, you could write data to a file on the SD card and then transfer the file out via FTP. There are plenty of libraries to do this with.
Same as 1, except push the data to a web service. There is an HTTPRequest library you could use to do up a quick JSON post to a web service. You would need to write the web service. But there are also plenty of free services out there that will take data feeds and store them like ubidots.
If you really want to go the serial port route, the bad news is that the Netduino's USB Serial port is reserved for debugging. But you can get a cheap little USB UART adapter to do this. There is a great blog post here that explains it.
Good Luck!

Related

UWP/C# - Issue with AQS and USB Devices

I have an issue with a UWP app that I am trying to write. I am connecting to a custom embedded USB Bulk device that I have programmed (it is actually an out of the box example from Cypress Semiconductor). I am using the WinUSB.sys driver using the embedded MS OS string in the device to allow the device to be used with out having to write a custom INF file to call the WinUSB.sys driver.
In my code, I am using the UsbDevice.GetDeviceSelector method to return an AQS that can then be passed into DeviceInformation.FindAllAsync to begin communicating with the device in my app. I have confirmed that the device shows up in the device manager without any issues, and I have checked in the registry to ensure that it has an Interface GUID. I have a screenshot from USBViewer to show the configuration of the device. This method for finding and connecting with USB devices is from this MSDN example found here.
When I use the UsbDevice.GetDeviceSelector method, it returns a GUID that is not associated with this device. The GUID that it returns is actually associated with Lumia Phones (DEE824EF-729B-4A0E-9C14-B7117D33A817). Because of this, it does not find my device connected to the system.
To troubleshoot, I have both called the DeviceInformation.FindAllAsync with out any arguments to see if my device is listed, and it does find the device (amongst over 1000 other devices that have been connected ever to my machine). I then wrote a custom AQS string without the help of the GetDeviceSelector method, starting with just the GUID. Doing this returned 27 devices, but when I tried to add the VID and PID to this AQS string, nothing returned.
I have also made sure that the device that I want to use is listed in the app manifest by its appropriate VID and PID as this is required for a device with a Custom Class of 0xFF. I have used the Custom USB UWP device example and it can find the device, though it uses a completely different method with a device picker, which I will go to if needed, but this is not my desire as it makes that part of the app not as clean of a solution.
I have posted this question over in the MSDN forums here with more information, but I have not gotten a lot of engagement there. Any help would be appreciated. I know that I must be missing something simple.
Adam
private async void button_Click(object sender, RoutedEventArgs e)
{
//UInt32 vid = 0x04B4;
//UInt32 pid = 0x00F0;
UInt32 vid = uint.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber);
UInt32 pid = UInt32.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);
Guid winusbInterfaceGuid = new Guid("a5dcbf10-6530-11d2-901f-00c04fb951ed");
//string aqs = UsbDevice.GetDeviceSelector(vid, pid);
string aqs = UsbDevice.GetDeviceSelector(winusbInterfaceGuid);
var myDevices = await DeviceInformation.FindAllAsync(aqs, null);
//var myDevices = await DeviceInformation.FindAllAsync();
var myDevicesCount = myDevices.Count;
if (myDevicesCount >= 1)
{
textBlock2.Text = "Device Found";
} else
{
textBlock2.Text = "Searching";
await Task.Delay(1000);
textBlock2.Text = "looking for device";
}
}
just dropped you a mail asking about progress (I think, had to guess your mail address), but now it seems I found a solution myself. Please see my answer on UWP app cannot find/connect to USB device
In short, you have to create an inf for installing the winusb driver. I have no clue why, but that did the trick for me (and someone else, see Cannot create UsbDevice from DeviceInformation.Id)
The Guid DEE824EF-729B-4A0E-9C14-B7117D33A817 is actually the standard WinUSB Guid. I don't think it has anything to do with Lumia Phones. I don't know why it is not documented anywhere. I think that the Guid a5dcbf10-6530-11d2-901f-00c04fb951ed you specified is actually a red herring. I mistakenly used that as well, but it just led me down the garden path. It shows up USB interfaces, but I can't connect to them.
You might want to try this class https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Usb.Net.UWP/UWPUsbDevice.cs .
Here is how it gets the device:
public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(uint? vendorId, uint? productId)
{
var aqsFilter = "System.Devices.InterfaceClassGuid:=\"{DEE824EF-729B-4A0E-9C14-B7117D33A817}\" AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True AND " + $" System.DeviceInterface.WinUsb.UsbVendorId:={vendorId.Value} AND System.DeviceInterface.WinUsb.UsbProductId:={productId.Value}";
var deviceInformationCollection = await wde.DeviceInformation.FindAllAsync(aqsFilter).AsTask();
//TODO: return the vid/pid if we can get it from the properties. Also read/write buffer size
var deviceIds = deviceInformationCollection.Select(d => new DeviceDefinition { DeviceId = d.Id, DeviceType = DeviceType.Usb }).ToList();
return deviceIds;
}
This sample connects to a device and I think you'll be able to connect to the device in the same way:
private static async Task InitializeTrezor()
{
//Register the factory for creating Usb devices. This only needs to be done once.
UWPUsbDeviceFactory.Register();
//Register the factory for creating Usb devices. This only needs to be done once.
UWPHidDeviceFactory.Register();
//Note: other custom device types could be added here
//Define the types of devices to search for. This particular device can be connected to via USB, or Hid
var deviceDefinitions = new List<DeviceDefinition>
{
new DeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x" },
new DeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, ReadBufferSize=64, WriteBufferSize=64, Label="Trezor One Firmware 1.7.x" },
new DeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C0, ReadBufferSize=64, WriteBufferSize=64, Label="Model T" }
};
//Get the first available device and connect to it
var devices = await DeviceManager.Current.GetDevices(deviceDefinitions);
var trezorDevice = devices.FirstOrDefault();
await trezorDevice.InitializeAsync();
//Create a buffer with 3 bytes (initialize)
var buffer = new byte[64];
buffer[0] = 0x3f;
buffer[1] = 0x23;
buffer[2] = 0x23;
//Write the data to the device
await trezorDevice.WriteAsync(buffer);
//Read the response
var readBuffer = await trezorDevice.ReadAsync();
}
If you connect to the device in this way, you'll get Windows classic, and Android support for free with Device.Net (https://github.com/MelbourneDeveloper/Device.Net)
With Device.net's DeviceManager.Current.GetDevices(deviceDefinitions) using .NET 5 I can't find any device connected to my win10, which can be easily selected by ManagementObjectSearcher:
public List<ManagementBaseObject> GetLogicalDevices()
{
List<ManagementBaseObject> devices = new List<ManagementBaseObject>();
ManagementObjectCollection collection;
ManagementObjectSearcher seacher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM CIM_LogicalDevice");
collection = seacher.Get();
foreach (var device in collection)
{
devices.Add(device);
}
return devices;
}

How to create a simple local web page using C# windows forms

I am looking to create a simple webpage using C# Windows Forms Application, or a C# Console application.
Running the application will begin hosting a web page at:
http://localhost:3070/somepage
I have read a little bit on MSDN about using endpoints, however being self-taught, this isn't making a ton of sense to me...
In short, this program, when running will display some text on a webpage at localhost:3070.
Sorry for such a vague question, however my hour(s) of searching for a decent tutorial haven't yielded any understandable results...
Thanks for your time!
🛑 2020 Update:
Original answer at the bottom.
Kestrel and Katana are now a thing and I would strongly recommend you look into those things as well as OWIN
Original Answer:
You will want to look into creating an HttpListener, you can add prefixes to the listener such as Listener.Prefixes.Add("http://+:3070/") which will bind it to the port your wanting.
A simple console app: Counting the requests made
using System;
using System.Net;
using System.Text;
namespace TestServer
{
class ServerMain
{
// To enable this so that it can be run in a non-administrator account:
// Open an Administrator command prompt.
// netsh http add urlacl http://+:8008/ user=Everyone listen=true
const string Prefix = "http://+:3070/";
static HttpListener Listener = null;
static int RequestNumber = 0;
static readonly DateTime StartupDate = DateTime.UtcNow;
static void Main(string[] args)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("HttpListener is not supported on this platform.");
return;
}
using (Listener = new HttpListener())
{
Listener.Prefixes.Add(Prefix);
Listener.Start();
// Begin waiting for requests.
Listener.BeginGetContext(GetContextCallback, null);
Console.WriteLine("Listening. Press Enter to stop.");
Console.ReadLine();
Listener.Stop();
}
}
static void GetContextCallback(IAsyncResult ar)
{
int req = ++RequestNumber;
// Get the context
var context = Listener.EndGetContext(ar);
// listen for the next request
Listener.BeginGetContext(GetContextCallback, null);
// get the request
var NowTime = DateTime.UtcNow;
Console.WriteLine("{0}: {1}", NowTime.ToString("R"), context.Request.RawUrl);
var responseString = string.Format("<html><body>Your request, \"{0}\", was received at {1}.<br/>It is request #{2:N0} since {3}.",
context.Request.RawUrl, NowTime.ToString("R"), req, StartupDate.ToString("R"));
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
// and send it
var response = context.Response;
response.ContentType = "text/html";
response.ContentLength64 = buffer.Length;
response.StatusCode = 200;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
}
}
}
And for extra credit, try adding it to the services on your computer!
Microsoft Relased an Open Source Project called OWIN it is simlar to Node but bottom line it allows you to host web applications in a console application:
You can find more information here:
https://github.com/duovia/duovia-http
http://owin.org/
http://katanaproject.codeplex.com/
But if you insist in creating your personal listener you can find some help here:
http://msdn.microsoft.com/en-us/library/system.net.httplistener(VS.80).aspx
http://social.msdn.microsoft.com/Forums/vstudio/en-US/b7f476d1-3147-4b18-ba5e-0b3ce8f8a918/want-to-make-a-webserver-with-httplistener

Enumerate available WiFi Printers in my Windows Phone app

I know similar questions have been asked a lot, ie "can I print from my wp8?". Most folks seem content with the simple "no" response. I am looking to add this functionality into my app and I know it can be accomplished at some level - even if I can only support a very minimum printer set for now.
I've looked at the app PrintHand, and it appears to have the capabilities I need: print to wireless and Bluetooth printers.
I've been looking through the Bluetooth scenarios document a bit and I think this might help discover a Bluetooth printer. That's a start. Perhaps it will help in identifying wireless printers also.
I realize I need to start very small with this project and I want to first try to enumerate any wireless printers (I don't have a Bluetooth one yet) available on the current network. Would anybody happen to have a pointer in the right direction on how to get started or better yet, some related sample code?
Thanks so much!
Best I can give is my Github Repo which isn't related to Wifi printers, but Google cloud printing.
There isn't really a .net reference for using Google Cloud Print in their documentation, but the code works in Mono, and should be easy to move over to .NET and hence, Windows Phone.
MonoGCP
I was able to detect a nearby wireless printer using Simple Service Discovery Protocol.
Here is my sample function:
private const string SSDP_IP = "239.255.255.250";
private const string SSDP_PORT = "1900";
public async static void DiscoverAsync2()
{
var multicastIP = new HostName(SSDP_IP);
var found = false;
using (var socket = new DatagramSocket())
{
socket.MessageReceived += (sender, e) =>
{
var reader = e.GetDataReader();
var bytesRemaining = reader.UnconsumedBufferLength;
var receivedString = reader.ReadString(bytesRemaining);
// TODO: something useful with this new info
found = true;
};
await socket.BindEndpointAsync(null, string.Empty);
socket.JoinMulticastGroup(multicastIP);
while (true)
{
found = false;
using (var stream = await socket.GetOutputStreamAsync(multicastIP, SSDP_PORT))
{
var request = new StringBuilder();
request.AppendLine("M-SEARCH * HTTP/1.1");
request.AppendLine("HOST: " + SSDP_IP + ":" + SSDP_PORT);
request.AppendLine("MAN: \"ssdp:discover\"");
request.AppendLine("MX: 3");
request.AppendLine("ST: urn:schemas-upnp-org:device:Printer:1"); // use ssdp:all to get everything
request.AppendLine(); // without this extra blank line, query won't run properly
var buff = Encoding.UTF8.GetBytes(request.ToString());
await stream.WriteAsync(buff.AsBuffer());
await Task.Delay(5000);
if (!found)
break;
}
}
}

NAudio record never receiving a sample

I'm attempting the simplest possible NAudio example to record from an input device but for some reason I can't get the DataAvailable callback function to be called.
In the example below a break point on Do Something never gets hit.
WaveIn waveIn = new WaveIn();
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
waveIn.WaveFormat = new WaveFormat(44100, 1);
waveIn.StartRecording();
private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
Do Something
}
I've checked, re-checked and re-re-checked that the settings are exactly the same as those used by the NAudio VoiceRecorder test application which is able to record audio fine with the exact same settings.
The only difference is my test application is a console application rather than a WPF app. Would that make a difference?
Yes, it is because it is a console app, and the WaveIn class uses Windows messages as callbacks. If you are able to download and build the very latest source code from codeplex, you can use the brand new WaveInEvent class (added 6 Mar 2012), which does not rely on a Windows message loop.
Alternatively, if you are familiar with installing pre-release packages using NuGet, you can install the latest NAudio prerelease (currently 1.5.4-beta) which has this class in.
Just a thought, have you verified your device is valid?
Try something like this;
int waveInDevices = WaveIn.DeviceCount;
for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
{
WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
MessageBox.Show("Device " + waveInDevice + ": " + deviceInfo.ProductName +
", " + deviceInfo.Channels + " channels");
}
to verify you have a recording device.

C# Telnet Library

Is there a good, free telnet library available for C# (not ASP .NET)? I have found a few on google, but they all have one issue or another (don't support login/password, don't support a scripted mode).
I am assuming that MS still has not included a telnet library as part of .NET v3.5 as I couldn't find it if it was. I would loooooove to be wrong though.
Best C# Telnet Lib I've found is called Minimalistic Telnet. Very easy to understand, use and modify. It works great for the Cisco routers I need to configure.
http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx
Here is my code that is finally working
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;
class TelnetTest
{
static void Main(string[] args)
{
TelnetTest tt = new TelnetTest();
tt.tcpClient = new TcpClient("myserver", 23);
tt.ns = tt.tcpClient.GetStream();
tt.connectHost("admin", "admin");
tt.sendCommand();
tt.tcpClient.Close();
}
public void connectHost(string user, string passwd) {
bool i = true;
while (i)
{
Console.WriteLine("Connecting.....");
Byte[] output = new Byte[1024];
String responseoutput = String.Empty;
Byte[] cmd = System.Text.Encoding.ASCII.GetBytes("\n");
ns.Write(cmd, 0, cmd.Length);
Thread.Sleep(1000);
Int32 bytes = ns.Read(output, 0, output.Length);
responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.WriteLine("Responseoutput: " + responseoutput);
Regex objToMatch = new Regex("login:");
if (objToMatch.IsMatch(responseoutput)) {
cmd = System.Text.Encoding.ASCII.GetBytes(user + "\r");
ns.Write(cmd, 0, cmd.Length);
}
Thread.Sleep(1000);
bytes = ns.Read(output, 0, output.Length);
responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.Write(responseoutput);
objToMatch = new Regex("Password");
if (objToMatch.IsMatch(responseoutput))
{
cmd = System.Text.Encoding.ASCII.GetBytes(passwd + "\r");
ns.Write(cmd, 0, cmd.Length);
}
Thread.Sleep(1000);
bytes = ns.Read(output, 0, output.Length);
responseoutput = System.Text.Encoding.ASCII.GetString(output, 0, bytes);
Console.Write("Responseoutput: " + responseoutput);
objToMatch = new Regex("#");
if (objToMatch.IsMatch(responseoutput))
{
i = false;
}
}
Console.WriteLine("Just works");
}
}
Another one with a different concept: http://www.klausbasan.de/misc/telnet/index.html
I ended up finding MinimalistTelnet and adapted it to my uses. I ended up needing to be able to heavily modify the code due to the unique** device that I am attempting to attach to.
** Unique in this instance can be validly interpreted as brain-dead.
I am currently evaluating two .NET (v2.0) C# Telnet libraries that may be of interest:
http://www.thoughtproject.com/Libraries/Telnet/
http://dotnettelnet.sourceforge.net/
Hope this helps.
Regards,
Andy.
Another one, it is an older project but shares the complete source code: http://telnetcsharp.codeplex.com/
I doubt very much a telnet library will ever be part of the .Net BCL, although you do have almost full socket support so it wouldnt be too hard to emulate a telnet client, Telnet in its general implementation is a legacy and dying technology that where exists generally sits behind a nice new modern facade. In terms of Unix/Linux variants you'll find that out the box its SSH and enabling telnet is generally considered poor practice.
You could check out:
http://granados.sourceforge.net/ - SSH Library for .Net
http://www.tamirgal.com/home/dev.aspx?Item=SharpSsh
You'll still need to put in place your own wrapper to handle events for feeding in input in a scripted manner.

Categories