Skip to main content

Hi, 

I have to do one more integration between my application and a TCP/IP controller.
The supplier just sent me the code with the programming of the features in C#, code that I already partially converted to Cobol. Now my question is how and what do I call these features (for example the OPEN and CLOSE functions).
I type below a part of the code in C# that was sent to me.

using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace NuxIOAPI2
{
public class ANuxIOII : NuxIOBase
{
protected int nRandomtemp;
protected Socket m_clientSocket; //UDP
protected AsyncCallback m_pfnCallBack;
protected IAsyncResult m_result;
protected ArrayList _messages = new ArrayList();

protected IPEndPoint _ipEndpoint;

private class SocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1024];
}

public static bool WriteErrorLog(string strText)
{
System.Diagnostics.Trace.WriteLine(strText);
string strPathName = @"C:\\NuxIOAPI.log";
bool bReturn = false;
string strException = string.Empty;
try
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPathName, true);
sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " : " + strText);
sw.Flush();
sw.Close();
bReturn = true;
}
catch (Exception)
{
bReturn = false;
}
return bReturn;
}

public ANuxIOII()
{
m_clientSocket = null;
_strModelo = "NuxIOII";
}


public override int Open(string localAddress)
{


try
{
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

IPEndPoint endPoint;

if (localAddress != "")
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());



int nIPIndex = -1;
for (int a = 0; a < hostEntry.AddressList.Length; a++)
if (hostEntry.AddressList[a].AddressFamily == AddressFamily.InterNetwork)
{
nIPIndex = a;
break;
}

endPoint = new IPEndPoint((localAddress == "") ? hostEntry.AddressList[nIPIndex] : IPAddress.Parse(localAddress), PortNo);
m_clientSocket.Bind(endPoint);
this._ipEndpoint = endPoint;
}
else
{
endPoint = new IPEndPoint(IPAddress.Any, PortNo);
m_clientSocket.Bind(endPoint);
}

WriteErrorLog("Socket Created at :" + endPoint.Address);

return WaitForDataUdp();
}
catch (Exception se)
{
m_clientSocket = null;
System.Diagnostics.Trace.WriteLine(se.Message);
LastError = se.Message;
return 0;
}
}

public override void Close()
{
try
{
if (m_clientSocket != null)
m_clientSocket.Close();
m_clientSocket = null;
}
catch (Exception se)
{
System.Diagnostics.Trace.WriteLine(se.Message);
LastError = se.Message;
}
}
}
}

There are some parts that I can't convert with Cobol Converter (eg this complete piece of code).
I have to convert in pieces.

I am once again waiting for your help.
If necessary, I can send all the information that the supplier has given me.

Best regards
Alberto Ferraz


Hi, 

I have to do one more integration between my application and a TCP/IP controller.
The supplier just sent me the code with the programming of the features in C#, code that I already partially converted to Cobol. Now my question is how and what do I call these features (for example the OPEN and CLOSE functions).
I type below a part of the code in C# that was sent to me.

using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace NuxIOAPI2
{
public class ANuxIOII : NuxIOBase
{
protected int nRandomtemp;
protected Socket m_clientSocket; //UDP
protected AsyncCallback m_pfnCallBack;
protected IAsyncResult m_result;
protected ArrayList _messages = new ArrayList();

protected IPEndPoint _ipEndpoint;

private class SocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1024];
}

public static bool WriteErrorLog(string strText)
{
System.Diagnostics.Trace.WriteLine(strText);
string strPathName = @"C:\\NuxIOAPI.log";
bool bReturn = false;
string strException = string.Empty;
try
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPathName, true);
sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " : " + strText);
sw.Flush();
sw.Close();
bReturn = true;
}
catch (Exception)
{
bReturn = false;
}
return bReturn;
}

public ANuxIOII()
{
m_clientSocket = null;
_strModelo = "NuxIOII";
}


public override int Open(string localAddress)
{


try
{
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

IPEndPoint endPoint;

if (localAddress != "")
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());



int nIPIndex = -1;
for (int a = 0; a < hostEntry.AddressList.Length; a++)
if (hostEntry.AddressList[a].AddressFamily == AddressFamily.InterNetwork)
{
nIPIndex = a;
break;
}

endPoint = new IPEndPoint((localAddress == "") ? hostEntry.AddressList[nIPIndex] : IPAddress.Parse(localAddress), PortNo);
m_clientSocket.Bind(endPoint);
this._ipEndpoint = endPoint;
}
else
{
endPoint = new IPEndPoint(IPAddress.Any, PortNo);
m_clientSocket.Bind(endPoint);
}

WriteErrorLog("Socket Created at :" + endPoint.Address);

return WaitForDataUdp();
}
catch (Exception se)
{
m_clientSocket = null;
System.Diagnostics.Trace.WriteLine(se.Message);
LastError = se.Message;
return 0;
}
}

public override void Close()
{
try
{
if (m_clientSocket != null)
m_clientSocket.Close();
m_clientSocket = null;
}
catch (Exception se)
{
System.Diagnostics.Trace.WriteLine(se.Message);
LastError = se.Message;
}
}
}
}

There are some parts that I can't convert with Cobol Converter (eg this complete piece of code).
I have to convert in pieces.

I am once again waiting for your help.
If necessary, I can send all the information that the supplier has given me.

Best regards
Alberto Ferraz


Hi Alberto.

It isn't clear to me what it is that you are asking for.
Do you wish for this piece of code here to be converted or do you have questions on how this code would be called from a COBOL program or something else altogether?


Hi Alberto.

It isn't clear to me what it is that you are asking for.
Do you wish for this piece of code here to be converted or do you have questions on how this code would be called from a COBOL program or something else altogether?

Hi Chris,

I have several problems that I will go on to explain better:
The first would be like calling the routines from a Cobol program. I think I managed to overcome this one because I was able to create the code to call and pass the parameters.

The second, and more complicated and that I was trying to solve, is to convert two pieces of code and still an instruction that gives error after the conversion.
I send below the two pieces of code that I can't convert.

A)
public static bool WriteErrorLog(string strText)
{
System.Diagnostics.Trace.WriteLine(strText);
string strPathName = @"C:\\NuxIOAPI.log";
bool bReturn = false;
string strException = string.Empty;
try
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPathName, true);
sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " : " + strText);
sw.Flush();
sw.Close();
bReturn = true;
}
catch (Exception)
{
bReturn = false;
}
return bReturn;
}

B)
public override void OnDataReceivedUdp(IAsyncResult asyn)
{

EndPoint epSender = (_ipEndpoint);
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
try
{
int iRx = theSockId.thisSocket.EndReceiveFrom(asyn, ref epSender);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);

//Generate class event
ValueEnteredEventArgs ea = new ValueEnteredEventArgs();
ea.IPAddress = ((IPEndPoint)epSender).Address.ToString();

ea.strData = szData;
szData = szData.Trim('\\0');
ea.Value = szData; //Para retirar


WriteErrorLog("Received data :" + szData);

if (szData == "*OK*")
ea.Type = 14; //OK

if (szData == "*KO*")
ea.Type = 15; //KO

if (szData == "I0")
ea.Type = 17; //Botão pressionado

if (szData.StartsWith("I0") && szData.Length == 4)
ea.Type = 20; //IO status

if (szData == "I2")
ea.Type = 16; //Cartão introduzido

if (szData == "I1")
ea.Type = 17; //Cartão devolvido

if (szData == "PRINT OK")
ea.Type = 18; //Impressão OK

if (szData.Length > 2 && szData.Substring(0, 2) == "#S") //PSR - Printer status Report
{
ea.Value = szData.Substring(2);
ea.Type = 19;
}

if (szData.Contains("*S"))
{
if (szData[2] == '1' || szData[2] == '2')
{
ea.Value = szData.Substring(3);
switch (szData[2])
{
case '1':
ea.Type = 8; //Receive data from Serial Port 1
break;

case '2':
ea.Type = 9; //Receive data from Serial Port 2
break;

default:
throw new Exception("Unexpected Case");
}
}
}

string[] strNuxIO = szData.Split((char)28);

if(strNuxIO.Length == 14)
{
object o = _hEstados;
if (!Monitor.TryEnter(o, TimeSpan.FromSeconds(1)))
{
}
try
{
if (_hEstados.ContainsKey(((IPEndPoint)epSender).Address.ToString()))
_hEstados[((IPEndPoint)epSender).Address.ToString()] = DateTime.Now;
else
_hEstados.Add(((IPEndPoint)epSender).Address.ToString(), DateTime.Now);
}
catch (Exception ex)
{
WriteErrorLog("Erro :" + ex);
}
finally
{
Monitor.Exit(o);
}
} else
{
WriteErrorLog("UDP :" + ea.IPAddress + " : " + szData);
}

if (ea.Type == 0 && szData[0] == 'B')
{
string strListenPort = strNuxIO[0].Substring(1);
string strIPAddress = strNuxIO[1];
string strGateway = strNuxIO[2];
string strServerIP = strNuxIO[3];
string strMask = strNuxIO[4];

string[] strMac = strNuxIO[5].Split('.');
string strMacAddress = "";
for (int j = 0; j < strMac.Length; j++)
{
strMacAddress += Convert.ToInt32(strMac[j]).ToString("X2");
if (j < strMac.Length - 1)
strMacAddress += ".";
}

ea.Type = 7; //Broadcast read
ea.NuxIONetwork = new NuxIONetwork(strListenPort, strIPAddress, strGateway, strServerIP, strMask, strMacAddress);
}

if (ea.Type > 0)
SetEvent(ea);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\\nOnDataReceived: Socket has been closed\\n");
}
catch (Exception se)
{
System.Diagnostics.Trace.WriteLine(se.Message);
}

try
{
WaitForDataUdp();
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\\nOnDataReceived: Socket has been closed\\n");
}
catch (Exception se)
{
System.Diagnostics.Trace.WriteLine(se.Message);
}
}

And I still have one more line that gives error:
endPoint = new IPEndPoint((localAddress == "") ? hostEntry.AddressList[nIPIndex] : IPAddress.Parse(localAddress), PortNo);

The conversion gives this:
set endPoint to new IPEndPoint((localAddress = "") ? hostEntry::AddressList[nIPIndex] : type IPAddress::Parse(localAddress), PortNo) *> ERROR: ternary expressions not supported
It gives an error in the character "?"

Thanks
Alberto Ferraz




Hi Chris,

I have several problems that I will go on to explain better:
The first would be like calling the routines from a Cobol program. I think I managed to overcome this one because I was able to create the code to call and pass the parameters.

The second, and more complicated and that I was trying to solve, is to convert two pieces of code and still an instruction that gives error after the conversion.
I send below the two pieces of code that I can't convert.

A)
public static bool WriteErrorLog(string strText)
{
System.Diagnostics.Trace.WriteLine(strText);
string strPathName = @"C:\\NuxIOAPI.log";
bool bReturn = false;
string strException = string.Empty;
try
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPathName, true);
sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " : " + strText);
sw.Flush();
sw.Close();
bReturn = true;
}
catch (Exception)
{
bReturn = false;
}
return bReturn;
}

B)
public override void OnDataReceivedUdp(IAsyncResult asyn)
{

EndPoint epSender = (_ipEndpoint);
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
try
{
int iRx = theSockId.thisSocket.EndReceiveFrom(asyn, ref epSender);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);

//Generate class event
ValueEnteredEventArgs ea = new ValueEnteredEventArgs();
ea.IPAddress = ((IPEndPoint)epSender).Address.ToString();

ea.strData = szData;
szData = szData.Trim('\\0');
ea.Value = szData; //Para retirar


WriteErrorLog("Received data :" + szData);

if (szData == "*OK*")
ea.Type = 14; //OK

if (szData == "*KO*")
ea.Type = 15; //KO

if (szData == "I0")
ea.Type = 17; //Botão pressionado

if (szData.StartsWith("I0") && szData.Length == 4)
ea.Type = 20; //IO status

if (szData == "I2")
ea.Type = 16; //Cartão introduzido

if (szData == "I1")
ea.Type = 17; //Cartão devolvido

if (szData == "PRINT OK")
ea.Type = 18; //Impressão OK

if (szData.Length > 2 && szData.Substring(0, 2) == "#S") //PSR - Printer status Report
{
ea.Value = szData.Substring(2);
ea.Type = 19;
}

if (szData.Contains("*S"))
{
if (szData[2] == '1' || szData[2] == '2')
{
ea.Value = szData.Substring(3);
switch (szData[2])
{
case '1':
ea.Type = 8; //Receive data from Serial Port 1
break;

case '2':
ea.Type = 9; //Receive data from Serial Port 2
break;

default:
throw new Exception("Unexpected Case");
}
}
}

string[] strNuxIO = szData.Split((char)28);

if(strNuxIO.Length == 14)
{
object o = _hEstados;
if (!Monitor.TryEnter(o, TimeSpan.FromSeconds(1)))
{
}
try
{
if (_hEstados.ContainsKey(((IPEndPoint)epSender).Address.ToString()))
_hEstados[((IPEndPoint)epSender).Address.ToString()] = DateTime.Now;
else
_hEstados.Add(((IPEndPoint)epSender).Address.ToString(), DateTime.Now);
}
catch (Exception ex)
{
WriteErrorLog("Erro :" + ex);
}
finally
{
Monitor.Exit(o);
}
} else
{
WriteErrorLog("UDP :" + ea.IPAddress + " : " + szData);
}

if (ea.Type == 0 && szData[0] == 'B')
{
string strListenPort = strNuxIO[0].Substring(1);
string strIPAddress = strNuxIO[1];
string strGateway = strNuxIO[2];
string strServerIP = strNuxIO[3];
string strMask = strNuxIO[4];

string[] strMac = strNuxIO[5].Split('.');
string strMacAddress = "";
for (int j = 0; j < strMac.Length; j++)
{
strMacAddress += Convert.ToInt32(strMac[j]).ToString("X2");
if (j < strMac.Length - 1)
strMacAddress += ".";
}

ea.Type = 7; //Broadcast read
ea.NuxIONetwork = new NuxIONetwork(strListenPort, strIPAddress, strGateway, strServerIP, strMask, strMacAddress);
}

if (ea.Type > 0)
SetEvent(ea);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\\nOnDataReceived: Socket has been closed\\n");
}
catch (Exception se)
{
System.Diagnostics.Trace.WriteLine(se.Message);
}

try
{
WaitForDataUdp();
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\\nOnDataReceived: Socket has been closed\\n");
}
catch (Exception se)
{
System.Diagnostics.Trace.WriteLine(se.Message);
}
}

And I still have one more line that gives error:
endPoint = new IPEndPoint((localAddress == "") ? hostEntry.AddressList[nIPIndex] : IPAddress.Parse(localAddress), PortNo);

The conversion gives this:
set endPoint to new IPEndPoint((localAddress = "") ? hostEntry::AddressList[nIPIndex] : type IPAddress::Parse(localAddress), PortNo) *> ERROR: ternary expressions not supported
It gives an error in the character "?"

Thanks
Alberto Ferraz



Can you privately send to me the assembly containing the NuxIOBase class? chris.glazier@microfocus.com.


Can you privately send to me the assembly containing the NuxIOBase class? chris.glazier@microfocus.com.

Hi,

I have already emailed you what you asked for.

In the meantime I've been doing some research and I think I've already found the solution to the error given in the last instruction that has to do with the character "?". 

I think the alternative is this:
if localAddress = ""
set endPoint to new IPEndPoint(hostEntry::AddressList[nIPIndex], PortNo)
else
set endPoint to new IPEndPoint((type IPAddress::Parse(localAddress)), PortNo)
end-if

I await more news from you.

Best regards.
Alberto Ferraz
 

Hi,

I have already emailed you what you asked for.

In the meantime I've been doing some research and I think I've already found the solution to the error given in the last instruction that has to do with the character "?". 

I think the alternative is this:
if localAddress = ""
set endPoint to new IPEndPoint(hostEntry::AddressList[nIPIndex], PortNo)
else
set endPoint to new IPEndPoint((type IPAddress::Parse(localAddress)), PortNo)
end-if

I await more news from you.

Best regards.
Alberto Ferraz
 

I received your email and am taking a look at the material. You are correct in your conversion of the conditional operator as you show above...


I received your email and am taking a look at the material. You are correct in your conversion of the conditional operator as you show above...

Hi Chris,

I've been trying to convert the application and I've already made some progress but I still can't compile without errors.
I'll wait for your analysis to see if you can solve my problems.

Thanks again for your availability.

Best regards
Alberto Ferraz

Hi Chris,

I've been trying to convert the application and I've already made some progress but I still can't compile without errors.
I'll wait for your analysis to see if you can solve my problems.

Thanks again for your availability.

Best regards
Alberto Ferraz

Situation resolved.

Thanks Chris.

Best regards

Alberto Ferraz