Page 1 of 1

Use of C code for modem in C#

Posted: Fri Jan 06, 2017 7:44 am
by bsahoo1995
Hello Maxim,

I am facing a problem while using the C code that you have given for extracting the positional data from the modem, in a C# code that we are writing for our software.

Problem:
The code that you have given is in C and for using that code C# requires converting it into dll. And I thought of making the dll in C++ and use that dll in our c# code, by using the same C code, but as C++ is not totally backward-compatible, its showing lots of build errors while compiling the C code.

My Question:
Is there any way that I could use that C source file and C header file, in my C# code to extract the positional values ? If yes, then how?

Thank you
Biswajit

Re: Use of C code for modem in C#

Posted: Sat Jan 07, 2017 6:10 pm
by admin
bsahoo1995 wrote:Hello Maxim,

I am facing a problem while using the C code that you have given for extracting the positional data from the modem, in a C# code that we are writing for our software.

Problem:
The code that you have given is in C and for using that code C# requires converting it into dll. And I thought of making the dll in C++ and use that dll in our c# code, by using the same C code, but as C++ is not totally backward-compatible, its showing lots of build errors while compiling the C code.

My Question:
Is there any way that I could use that C source file and C header file, in my C# code to extract the positional values ? If yes, then how?

Thank you
Biswajit



Hello,

I discussed with my colleagues. We don't have much experience with C#, so, we can't offer you a quick help.
Maybe, somebody from the forum can.

BR,
Maxim

Re: Use of C code for modem in C#

Posted: Tue Jan 10, 2017 5:25 pm
by apoorvcn47
You dont have to use their C code. Write a serial reader in c#. following should help

using System;
using System.IO.Ports;

class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM4");

mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;

mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

mySerialPort.Open();

Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}

private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
//string indata = sp.ReadExisting();
sp.DtrEnable = true;
sp.RtsEnable = true;

int bytes = sp.BytesToRead;
byte[] buffer = new byte[bytes];
sp.Read(buffer, 0, bytes);
Console.WriteLine("Data Received:");
//Console.WriteLine(indata);
if(buffer.Length>=3)
{
if (buffer[2] == 2)
{
Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}

}

for (int i=0; i<buffer.Length; i++)
{
//Console.WriteLine("buffer size : " + buffer.Length);
//Console.WriteLine("byte size : " + bytes.ToString());
Console.Write(buffer[i].ToString());
Console.Write(" ");

}
Console.Write("\n");
}
}

Re: Use of C code for modem in C#

Posted: Tue Jan 10, 2017 6:35 pm
by admin
apoorvcn47 wrote:You dont have to use their C code. Write a serial reader in c#. following should help

using System;
using System.IO.Ports;

class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM4");

mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;

mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

mySerialPort.Open();

Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}

private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
//string indata = sp.ReadExisting();
sp.DtrEnable = true;
sp.RtsEnable = true;

int bytes = sp.BytesToRead;
byte[] buffer = new byte[bytes];
sp.Read(buffer, 0, bytes);
Console.WriteLine("Data Received:");
//Console.WriteLine(indata);
if(buffer.Length>=3)
{
if (buffer[2] == 2)
{
Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}

}

for (int i=0; i<buffer.Length; i++)
{
//Console.WriteLine("buffer size : " + buffer.Length);
//Console.WriteLine("byte size : " + bytes.ToString());
Console.Write(buffer[i].ToString());
Console.Write(" ");

}
Console.Write("\n");
}
}



Thank you!

Dear users, please, confirm, that the code works for you too.

Re: Use of C code for modem in C#

Posted: Tue Jan 24, 2017 11:00 am
by bsahoo1995
Dear apoorvcn47,

Thank you for your reply, and for the code. But the code that you have given is to handle the serial ports and that code works well too, but I need to extract the positional data from the modem (not from the mobile beacon) calling the same method that their C library has already defined in my C# code. I will attach the C library, header and example file too, So that you can have a good idea, what I want to do, and their is a pdf that explains how to use this C code.

(And sorry for the late reply as I was busy in other works from few weeks)

Hope to listen from you at earliest.

Thanks and Regards
Biswajit

Re: Use of C code for modem in C#

Posted: Mon Mar 06, 2017 10:40 pm
by apoorvcn47
Hi Biswajit,

I am using the same code to get data from modem. you probably have to change the COM number. What result are you getting by using this code for modem? Send me more information/screen shots at apoorvcn47@gmail.com