1

I've been trying to create a C# Windows Forms Bluetooth serial application and then run it via Mono on my Raspberry Pi 3. I tried the following:

String SelectedDevice = AvailableDevicesComboBox.SelectedItem.ToString();
BSerialPort = new SerialPort(SelectedDevice, 115200, Parity.None, 8, StopBits.One);
BSerialPort.Open();

Using the standart System.IO.Ports library.

However I am not able to open the port, because I get the lovely exception "Object reference not set to an instance of an object", which means that there is no such port at all. I have paired the Bluetooth device to the RPi using one of the RFCOMM slots and then use it as serial (and have successfully transferred some strings via "cat" in the terminal), but the result in my app is the same, it actually doesn't show in the ComboBox. So I decided to use "32Feet-InTheHand" library for Bluez and Mono together. Bun now when I run my .exe file in Mono I get this in the Terminal:

Unhandled Exception: System.TypeLoadException: 
Could not load type of field 'NameOfApp.Form1:BClient' (1) due to:
Could not load file or assembly 'InTheHand.Net.Personal,
Version=3.5.605.0, Culture=neutral, PublicKeyToken=ea38caa273134499'
or one of its dependencies. assembly:InTheHand.Net.Personal, Version=3.5.605.0, 
Culture=neutral, PublicKeyToken=ea38caa273134499 type:<unknown type> member:<none>
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: 
Could not load type of field 'NameOfApp.Form1:BClient' (1) due to: Could not load file 
or assembly 'InTheHand.Net.Personal, Version=3.5.605.0, Culture=neutral, 
PublicKeyToken=ea38caa273134499' or one of its dependencies. assembly:InTheHand.Net.Personal, 
Version=3.5.605.0, Culture=neutral, PublicKeyToken=ea38caa273134499
 type:<unknown type> member:<none>

I have added the reference and marked it as required in the Application Files tab. But I'm not good at this at all...

Linux version: Raspbian 9 - Linux raspberrypi 4.9.59-v7+ #1047

Visual Studio Community 2017 version 15.5.3

Mono: Mono JIT compiler version 5.4.1.6

32Feet.NET: 3.5.605

Legends
  • 21,202
  • 16
  • 97
  • 123
Martin
  • 33
  • 1
  • 4
  • Have you copied the DLL's to the raspberry? The error says it can't load. Also, I doubt you will be able to use any of the Bluetooth libraries out there for Windows on Raspbian, all relay in native calls which will crash your app. Also, if you already have paired the device you should have a "/dev/rfcommx", you can open it as a file and read/write to it. – Gusman Jan 14 '18 at 18:12
  • Thought also to use symbolic link with "ln -s" and so I did, but same results. I have copied the .dll files to the same directory as the .exe one. Thank you for your suggestion. :) – Martin Jan 14 '18 at 19:11

1 Answers1

0

Ok so I managed to solve the problem. Turned out to be a very, very stupid mistake of mine. I did not declare the "BSerialPort" correctly. The following statement is wrong:

BSerialPort = new SerialPort(SelectedDevice, 115200, Parity.None, 8, StopBits.One);

the correct way to declare such port is as follows:

SerialPort BSerialPort = new SerialPort(SelectedDevice, 115200, Parity.None, 8, StopBits.One);

This little "SerialPort" definition at the start of the declaration makes a big difference.

Thank you for your help and have happy coding! :)

Martin
  • 33
  • 1
  • 4