Use of C code for modem in C#

Frequent questions asked online, offline, in forums are answered here
Post Reply
bsahoo1995
Posts: 2
Joined: Fri Jan 06, 2017 7:30 am

Use of C code for modem in C#

Post 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

User avatar
admin
Site Admin
Posts: 305
Joined: Tue Jan 26, 2016 7:06 pm
Contact:

Re: Use of C code for modem in C#

Post 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

apoorvcn47
Posts: 2
Joined: Tue Jan 10, 2017 5:21 pm

Re: Use of C code for modem in C#

Post 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.ToString());
Console.Write(" ");

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

User avatar
admin
Site Admin
Posts: 305
Joined: Tue Jan 26, 2016 7:06 pm
Contact:

Re: Use of C code for modem in C#

Post 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.ToString());
Console.Write(" ");

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



Thank you!

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

bsahoo1995
Posts: 2
Joined: Fri Jan 06, 2017 7:30 am

Re: Use of C code for modem in C#

Post 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
Attachments
2016_12_03_C_example.zip
(168.44 KiB) Downloaded 299 times

apoorvcn47
Posts: 2
Joined: Tue Jan 10, 2017 5:21 pm

Re: Use of C code for modem in C#

Post 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

Post Reply