Hi,
We have developed a socket program as below, The program works fine, except that it sometimes gives me jumbled messages or same message gets repeated twice.

The whole code is produced below.

I will let you know what exactly are we doing.

This a socket program for tracking vehicle.
The vehicle sends the signal through gprs+gsm mode, to our port.

We retrieve those messages using the above socket program.

Most of the messages come with GPRMC .

such as
$GPRMC,040302.663,A,3939.7,N,10506.6,W,0.27,358.86,200804,,*1A

Kindly help and let me know where am I going wrong
Regards
cmrhema

namespace SocketServer 
{

class Program

{

 

public AsyncCallback pfnWorkerCallBack;private Socket m_mainSocket; 
 

public string IPPort = "7030";

public byte[] m_byBuff = new byte[5000]; 
public int flag2 = 0;

public string queuePath; 
private string strDateFormat;

private string strLocation; 
public byte[] inValue = new byte[] { 1, 0, 0, 0, 0, 0, 5, 0, 1, 0, 0, 0 }; //1 -Minutes // 0,5 -- 6th parameter --seconds, 7th parameter--->Minutes

public byte[] outvalue = new byte[10];public Program() 
{

this.queuePath = ".\\private$\\AMI"; 
 

this.strDateFormat = null;

this.strLocation = null; 
StartListen();

 

}

public void StartListen() 
{

try

{

string portStr = this.IPPort; 
int port = System.Convert.ToInt32(portStr);

// Create the listening socket...

m_mainSocket = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream,

ProtocolType.Tcp);IPAddress ip; 
 

string ipstr = "192.168.0.20";

ip=IPAddress.Parse(ipstr); 
IPEndPoint ipLocal = new IPEndPoint(ip, port);

//// Bind to local IP Address...

m_mainSocket.Bind(ipLocal);

//// Start listening...

m_mainSocket.Listen(100);

// Create the call back for any client connections...

while (true) 
{

m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);Thread.Sleep(2000); 
}

 

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

 

}

}

 

public void OnClientConnect(IAsyncResult asyn) 
{

try

{

Socket msocket; 
msocket = m_mainSocket.EndAccept(asyn);

msocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);msocket.IOControl(IOControlCode.KeepAliveValues, inValue, outvalue); 
 

WaitForData(msocket);

 

//BY COMMENTING THE BELOW LINE IT WILL NOT ACCEPT THE NEW CONNECTIONS

m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null); 
 

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

}

}




public void WaitForData(System.Net.Sockets.Socket soc) 
{

try

{

if (pfnWorkerCallBack == null) 
{

 

pfnWorkerCallBack = new AsyncCallback(OnDataReceived); 
}

 

soc.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, pfnWorkerCallBack, soc); 
//BY CLEARING BUFFER NO DATA EVER GETS RECEIVED, EXCEPTION WILLL BE TRHOWN IN "ONDATARECEIVED" METHOD

//m_byBuff = null;

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

}

}

public void OnDataReceived(IAsyncResult asyn) 
{

try

{

 

Socket sock = (Socket)asyn.AsyncState; 
 

int nBytesRec = sock.EndReceive(asyn);string sReceived = System.Text.Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec); 
WriteLogMessage(sReceived);

//By MAKING THE BUFFER NULL OVER HERE, THE FIRST DATA GETS CAPTURED AND WHEN IT MOVES AGAIN TO SOC.BEGINRECEIVE, THE BUFFER BECOMES NULL

 

//m_byBuff = null;

this.ToMSMQ(sReceived);

Console.Write(sReceived); 
WaitForData(sock);

}

catch (SocketException e1) 
{

Socket sock = (Socket)asyn.AsyncState; 
sock.Close();

WriteErrorLogMessage(e1.Message);

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

}

 

}

 

public void ToMSMQ(string msg) 
{

//some process 




}








 

static void Main(string[] args) 
{

Program p = new Program(); 
 

}

}

}

Hi,
This is vijay bhaskar. We have also faced the same problem, but the context what you are doing is bit different than what we are doing. I will explain my context. Even in my case also GPS is sending the continuously. The message format is same as what you have mentioned like GPRMC. But we are also processing GPGGA and GPGSA messages. Ok now i am coming to the current context. What you are doing is, the data is reading through Socket programming. But what we are doing is, GPS is sending the data through UART. So we have written UART driver for reading the data from the UART. Once you have got the data now the process what you are following is same as what we are following. We also faced the same problem i.e jumbled messages.

What we did for this is....

First we have taken char array where you want to store the read data.

char[] result = new char[How many bytes you want to read];

And declare one ASCII encoding object

ASCIIEncoding objEncoder = new ASCIIEncoding();

And then use the imported libarry function ReadFile from Coredll.dll

int handleSerialPort = -1;
byte[] mbytRxBuffer;
mbytRxBuffer = new byte[How many bytes you want to read];

ReadFile(handleSerialPort, mbytRxBuffer,
BytesToRead, NULL,NULL);

Convert byte array to char array

result = objEncoder.GetChars(mbytRxBuffer);

Convert char array to string...

string Result_string = new string(result);


Finally the Result_string contains Actual data you read.

Please feel free to contact me if you face problems.


Regards,
Vijay Bhaskar

Hi,
We have developed a socket program as below, The program works fine, except that it sometimes gives me jumbled messages or same message gets repeated twice.

The whole code is produced below.

I will let you know what exactly are we doing.

This a socket program for tracking vehicle.
The vehicle sends the signal through gprs+gsm mode, to our port.

We retrieve those messages using the above socket program.

Most of the messages come with GPRMC .

such as
$GPRMC,040302.663,A,3939.7,N,10506.6,W,0.27,358.86,200804,,*1A

Kindly help and let me know where am I going wrong
Regards
cmrhema

namespace SocketServer 
{

class Program

{

 

public AsyncCallback pfnWorkerCallBack;private Socket m_mainSocket; 
 

public string IPPort = "7030";

public byte[] m_byBuff = new byte[5000]; 
public int flag2 = 0;

public string queuePath; 
private string strDateFormat;

private string strLocation; 
public byte[] inValue = new byte[] { 1, 0, 0, 0, 0, 0, 5, 0, 1, 0, 0, 0 }; //1 -Minutes // 0,5 -- 6th parameter --seconds, 7th parameter--->Minutes

public byte[] outvalue = new byte[10];public Program() 
{

this.queuePath = ".\\private$\\AMI"; 
 

this.strDateFormat = null;

this.strLocation = null; 
StartListen();

 

}

public void StartListen() 
{

try

{

string portStr = this.IPPort; 
int port = System.Convert.ToInt32(portStr);

// Create the listening socket...

m_mainSocket = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream,

ProtocolType.Tcp);IPAddress ip; 
 

string ipstr = "192.168.0.20";

ip=IPAddress.Parse(ipstr); 
IPEndPoint ipLocal = new IPEndPoint(ip, port);

//// Bind to local IP Address...

m_mainSocket.Bind(ipLocal);

//// Start listening...

m_mainSocket.Listen(100);

// Create the call back for any client connections...

while (true) 
{

m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);Thread.Sleep(2000); 
}

 

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

 

}

}

 

public void OnClientConnect(IAsyncResult asyn) 
{

try

{

Socket msocket; 
msocket = m_mainSocket.EndAccept(asyn);

msocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);msocket.IOControl(IOControlCode.KeepAliveValues, inValue, outvalue); 
 

WaitForData(msocket);

 

//BY COMMENTING THE BELOW LINE IT WILL NOT ACCEPT THE NEW CONNECTIONS

m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null); 
 

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

}

}




public void WaitForData(System.Net.Sockets.Socket soc) 
{

try

{

if (pfnWorkerCallBack == null) 
{

 

pfnWorkerCallBack = new AsyncCallback(OnDataReceived); 
}

 

soc.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, pfnWorkerCallBack, soc); 
//BY CLEARING BUFFER NO DATA EVER GETS RECEIVED, EXCEPTION WILLL BE TRHOWN IN "ONDATARECEIVED" METHOD

//m_byBuff = null;

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

}

}

public void OnDataReceived(IAsyncResult asyn) 
{

try

{

 

Socket sock = (Socket)asyn.AsyncState; 
 

int nBytesRec = sock.EndReceive(asyn);string sReceived = System.Text.Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec); 
WriteLogMessage(sReceived);

//By MAKING THE BUFFER NULL OVER HERE, THE FIRST DATA GETS CAPTURED AND WHEN IT MOVES AGAIN TO SOC.BEGINRECEIVE, THE BUFFER BECOMES NULL

 

//m_byBuff = null;

this.ToMSMQ(sReceived);

Console.Write(sReceived); 
WaitForData(sock);

}

catch (SocketException e1) 
{

Socket sock = (Socket)asyn.AsyncState; 
sock.Close();

WriteErrorLogMessage(e1.Message);

}

catch (Exception e1) 
{

WriteErrorLogMessage(e1.Message);

}

 

}

 

public void ToMSMQ(string msg) 
{

//some process 




}








 

static void Main(string[] args) 
{

Program p = new Program(); 
 

}

}

}

Thanks Vijay Bhaskar for the reply

I will checkout and revert back to you soon

Many Thanks

cmrhema

Kindly clarify whether you received fixed amt of bytes. Because ours willl vary, if for some time the vehicle due to unavoidable reason cannot send the data, it will send the whole bulk later as history which will be really very huge.

Secondly do I need to download coredll.dll, because while searching, i do not get the dll in my system

1st Question answer: - As i already mentioned you, we are using UART driver to retrieve the data from the GPS. Hence for any driver there is baudrate. Baudrate means that how many number of bits GPS is sending in one sec. Hence it's depend up on developers requirement how much baudrate he is using. In my application i am using 9600b/s (or) 19200b/s (or) 38400b/s. Hence it will send fixed number of bytes.

Now coming to your application, i have an issue.

1) You are reading the data through socket. I just wanted to know where you are storing the GPS read data. That means whether you are storing in a file or any buffer...etc.


2nd Question Ans: -

Before answer to this question i wanted to know which OS you are using. If you are using WINCE OS, the coredll.dll is already exist in your PC. Hence you can directly import that dll into your application like i mentioned below.

[DllImport("coredll.dll")]
public static extern int ReadFile(
int hFile, Byte[] Buffer, int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped);

Use this function in your .CS file (I hope you are using C# programming language)

If you are using some other operating system, May be what ever i told till now wont be helpful to you. But the concept is same. Just do as per your OS. Why because coredll.dll is specific to WINCE OS.

If you have any queries feel free to contact me.

Regards,
Vijay Bhaskar

Kindly clarify whether you received fixed amt of bytes. Because ours willl vary, if for some time the vehicle due to unavoidable reason cannot send the data, it will send the whole bulk later as history which will be really very huge.

Secondly do I need to download coredll.dll, because while searching, i do not get the dll in my system

This article has been dead for over six months. Start a new discussion instead.