Communication between Raspberry Pi 3B+ and MDEK1001 listener using c#

I’ve plugged the listener onto my Rpi 3 headers. With screen /dev/ttyS0 115200 I’m able to receive the coordinates from the tags.(I don’t use the USB anymore)

At least I know my connection between the listener and Rpi is correct and my setup of the serial port on the Rpi is correct.

I’ve tried the following code to get the dwm> cursor:
mySerialPort.Open();
if (mySerialPort.IsOpen)
{
Console.WriteLine(mySerialPort.PortName + " is Open");
Console.WriteLine(“Trying to open dwm> cursor…”);

                mySerialPort.Write("/r");
                Thread.Sleep(50);
                mySerialPort.Write("/r");
                Thread.Sleep(3000);
                Console.WriteLine("Sending 'lep' command...");

                mySerialPort.Write("lep\r");
                mySerialPort.DataReceived += MySerialPort_DataReceived;
            }

In the console it shows that the /dev/ttyS0 port opens, but didn’t show the dwm> cursor or any data

It work 100% when I plug the listener into my PC with usb cable.

What C# code must I use to receive the coordinates from the listener? How do I use screen in my C# application?

Hi Izak,

The operation flow seems ok.
I’m not very familiar with C#, but in C and python, you need to input \r rather than /r for a new line. Maybe give it a try?

Weibo

Thank you Weibo for replying. I saw the mistake. I’ve change it, but still not getting data from listener. It don’t want to open the dwm cursor (e.g. dwm> POS. etc).

Please send me your python code if you wish.

Hi Izak,

My code in python is pretty simple.

     import sys, serial
     from time import sleep

     ser = serial.Serial('COM3', 115200)

     print("(trying to input double Enter...)")
     ser.write(b'\r')     # write a byte
     sleep(0.2)
     ser.write(b'\r')     # write a byte
     sleep(1)
     [idx, data] = find_in_resp(b'\r\ndwm>')

Weibo

Hi Weibo

I’ve tried different ways to write the string to the serial port, but without success. I even experiment with different settings of the serial port.

I’m sure I’m missing something in the way to write and read to and from the serial port to get the info from the listener. I know for certain my setup is correct because I get data with “screen /dev/ttyS0 115200”.

I don’t know Python or C, but I can C# help me to read it.

Here is the code:

    //Reading data from serial port
    private static void ReadingData()
    {
        try
        {
            mySerialPort.Open();
            if (mySerialPort.IsOpen)
            {
                mySerialPort.DiscardOutBuffer();
                mySerialPort.DiscardInBuffer();
                Console.WriteLine(mySerialPort.PortName + " is Open");
                Console.WriteLine("Trying to open dwm> cursor...");
                mySerialPort.Write("\r");
                Thread.Sleep(50);
                mySerialPort.Write("\r");
                Thread.Sleep(1000);
                Console.WriteLine("Sending 'lep' command...");
                mySerialPort.Write("lep\r");
                Console.WriteLine("lep command send");
                mySerialPort.DataReceived += MySerialPort_DataReceived;
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine("Error in ReadingData: " + ex.ToString());
        }
    }

Screenshot of the Raspberry Pi using SSH while receiving data from listener using screen.
RPi%20dwm|690x388

I’m not sure what I must try next. Please HELP!!!

Hi Izak,

In C language, “\r” is not one byte. It actually indicates a string of two bytes, i.e. a ‘\r’ byte followed by a ‘\0’ byte. Maybe try single byte ‘\r’ ?

Weibo

Hi Weibo

In C# it is “\r”. This indicate it is a string.

Maybe there is a read/write permission on the /dev/ttyS0 port. I’ve google it and change accordingly. This is what I get when I enter “ls -l /dev/ttyS0” at the prompt.

pi@raspberrypi:~ $ ls -l /dev/ttyS0
crw-rw-rw- 1 root dialout 4, 64 Jan 28 15:43 /dev/ttyS0
pi@raspberrypi:~ $

Any suggestions?

Hi,
try the more simplest thing - enter into the shell mode manually and run les comamnd. And then start your C# program and try to read something.

Also try not to write a string but try raw value via write fucntion https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.write?view=netframework-4.7.2#System_IO_Ports_SerialPort_Write_System_Char___System_Int32_System_Int32_

Another note is that shell probably dont use DMA so you cant write “les” in one shot but you must split it in to “l” wait 10mses “e” wait 10mses “s” wait 10mses …

Last note - dont use /dev/ttyS0 but use /dev/serial0 as on some RPi the /dev/ttyS0 dont point into the accessible user UART.

Hello Izak

I had the same problem.

The callback does not work, do not use:

mySerialPort.DataReceived += MySerialPort_DataReceived;

You will have to poll the existence of bytes in the UART port.This way it works fine.

Regards

Epenciso

Hello Epenciso

Finally, after months, you gave me the reason why I didn’t receive data from my Raspberry Pi serial port. Serial Port events doesn’t fire in Mono!!

Thank you for your help.

Hello Epenciso

I’m able to read the coordinates from the listener. I want to miniplate the coordinates, but ran into another problem. I receive the coordinate with 'n dot (e.g. 1.23) and the RPi needs a comma (e.g. 1,23). I’ve used the following to get the value from the line of data:

tagX = float.Parse(dataReceived[r].ToString(), CultureInfo.InvariantCulture.NumberFormat);

With this I can get an average of the coordinates:

avgtagX += tagX;

avgtagX = (float)(Math.Truncate((double)avgtagX / avgNum * 100.0) / 100.0);

where avgNum is the quantity of reads.
(tagX and avgtagX is floats)

Now I want to save it in a MySQL Server Database with the following sqlText:

sqlText = String.Format(@“Insert into myDB.tag_pos_data (TagID,DateTimeIn,TagX,TagY,TagZ,Zone) values (’{0}’,’{1}’,{2},{3},{4},’{5}’)”, tagID, timeIn, _avgtagX, _avgtagY, _avgtagZ, zone);

The problem with this is that the avtagX, avgtagY and avgtagZ is with a comma (e.g. 1,23) which give me and error, because the string “see” the data as 1, 23 and not 1,23.

I’ve try to setup the regional settings on the RPi but couldn’t get the correct setting to change it to work with a dot instead of a comma.

I don’t want to change the datatype to string, because it will use more memory.
Do you know how I can achieve this on my RPi?

Hi Izak
I don not understand what you mean by “the string “see” the data as 1, 23 and not 1,23”…

I can not help on this.

However, these are only 3 strings (X,Y,Z) with few bytes per string (about 5-6bytes length), so I do not think it is much more memory for a Raspberry! And a float is actually 4 bytes.

Regards

Sorry for the late reply. The problem has been solved thank you.