Posts: 2
Joined: Sun Jan 21, 2018 9:50 pm
Location: UK
Contact: Website

Bluetooth Serial Port with C# on Raspbian /w Mono

Sun Jan 21, 2018 10:37 pm

Hello everyone,
as a fellow noob, like me, I have very specific question that I was searching the solution to for a very, very long time.

So I'm trying to achieve a Bluetooth serial communication between Arduino (coupled with HC-05) and RPi 3, running the latest version of Raspbian.

I want to make an appealing GUI, so I decided to use C# (Visual Studio for IDE, Windows Forms App) and run my ".exe" file with Mono for Raspbian. However I am not able to open the serial port. I have successfully paired the Bluetooth module with the Pi and even transferred some strings with:

Code: Select all

sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1
cat /dev/rfcomm0 > recievedText.txt
So in order for the C# to recognize the Bluetooth port connection as serial I run this:

Code: Select all

sudo ln -s  /dev/rfcomm0 /dev/ttyS12
and my C# code:

Code: Select all

public static SerialPort BSerialPort;
private void Form1_Load(object sender, EventArgs e) {
            String[] AvailablePorts = SerialPort.GetPortNames();
            foreach (String port in AvailablePorts) {
private void ConnectBtn_Click(object sender, EventArgs e) {
     	    BSerialPort.PortName = DevicesComboBox.SelectedText;
            BSerialPort.ReadBufferSize = 4096;
            BSerialPort.DataBits = 8;
            BSerialPort.BaudRate = 115200;
            BSerialPort.Encoding = Encoding.UTF8;
            BSerialPort.Parity = Parity.Space;
            BSerialPort.StopBits = StopBits.One;
            BSerialPort.Handshake = Handshake.None;
And as expected I get all serial devices in the combo box, named "DevicesComboBox". And when I try to open the port I get the lovely exception "System.NullReferenceException: Object reference not set to an instance of an object.", which means that it finds the port name but not the port itself?

I thought that it might be because of the Bluetooth device and decided to connect the Arduino and the RPi using the good old USB B to A cable and, as expected, it showed up as a new serial port in the combo box, but again when it gets to the part of opening the port I face the same error, stating that the port is null...

I also tried using a dirty trick - used the "cat" command, I mentioned earlier, to save serial data to some file and read this file with C#, turned out that C# can only read the file once and then it stops...

So is there a way for this code to work with Mono? Am I doing something wrong?
Thank you for your time to read this long post, hope I'm not posting this topic in the wrong thread. If so, please excuse me... as it says on the SD Card NOOBS, still very clever made (New Out Of Box Software) :D

Raspbian: 9 (stretch), Kernel: Linux 4.9.59-v7+ #1047
Mono: JIT compiler version
Visual Studio: Community 2017 v.15.5.4

Posts: 1
Joined: Mon Jan 29, 2018 3:03 am

Re: Bluetooth Serial Port with C# on Raspbian /w Mono

Mon Jan 29, 2018 3:11 am

System.IO.SerialPort only works on Windows platform.

I think you can try It works on Linux and Windows. The readme.MD has every detail you need.

Posts: 2
Joined: Sun Jan 21, 2018 9:50 pm
Location: UK
Contact: Website

Re: Bluetooth Serial Port with C# on Raspbian /w Mono

Wed Jan 31, 2018 11:21 am

Hey thanks for the reply! I'm gonna try it out!
Actually I found that I have a small and stupid mistake in the declaration of the serial port itself. It worked when I defined it the following way:

Code: Select all

 public static SerialPort BSerialPort = new SerialPort();
I had missed the "SerialPort" definition before the name "BSerialPort"...
Thank you for this library reference! :)

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 6 guests