Discussion:
Sockets in AX2009
(too old to reply)
Jonathan
2010-08-09 13:47:03 UTC
Permalink
Hi everyone!
I am trying to send raw printing instructions to a network barcode printer
from AX2009.
See the code below:
tatic void Job10(Args _args)
{
System.String netString;
System.String tmpString;

System.String ipAddressString = "172.x.x.x";
System.String netIpAddress = "172.x.x.x";
BarcodeSetupId barcodeFamily = "CODE128";
ItemBarcode barcode = "barcode";
Description ItemDesignation = "";
InventSiteId siteId = "SITE";
str numCopies = "0001";

System.Type t = System.Type::GetType("System.Byte[]", true);
System.Byte[] byteArray;
CLRObject myBytes = System.Array::CreateInstance(t, 1024);

System.Net.Sockets.TcpClient tcpClient;
System.IO.Stream networkStream;

System.Net.IPEndPoint remoteIP;
System.Net.IPAddress ipAddress =
System.Net.IPAddress::Parse(netIpAddress);
System.Int32 port = 9100;
System.Net.Sockets.Socket Socks;
System.Net.Sockets.NetworkStream ns;


System.Exception clrException;
;

tcpClient = new System.Net.Sockets.TcpClient(netIpAddress, 9100);
networkStream = tcpClient.GetStream();

if(networkStream.get_CanWrite())
{
try
{
// RAW INSTRUCTIONS - Begin
netString = "<[Cd\"+02:0\"]><[Cl\"0330:0630:0300\"]>";
tmpString = "<[Cc]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Fa\"0000:0000\"\"0600:0300\"\"A\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Cp\"-000\"\"-000\"\"-00\"]>";
netString = System.String::Concat(netString, tmpString);
if (barcodeFamily == "EAN13")
{
tmpString =
"<[Fb\"0090:0090\"\"01\"\"0\"\"5:2:1\"\"0100\"\"03\"\"+0000000000\"\"030\"]>"; //REM POUR CODE BARRE EAN13
}
else if (barcodeFamily == "CODE128")
{
tmpString =
"<[Fb\"0090:0090\"\"01\"\"0\"\"9:2:1\"\"0100\"\"01\"\"+0000000000\"\"030\"]>"; //REM POUR CODE BARRE 128
}
netString = System.String::Concat(netString, tmpString);
tmpString =
"<[Ff\"0015:0060\"\"000\"\"00\"\"H:B\"\"05:10\"\"+00:00:00\"\"\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString =
"<[Ff\"0001:0285\"\"002\"\"00\"\"G:B\"\"10:10\"\"+00:00:00\"\"\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString =
"<[Ff\"0460:0285\"\"003\"\"00\"\"G:B\"\"10:10\"\"+00:00:00\"\"\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Pc\"-0120\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Df\"000\"\"" + ItemDesignation + "\"]>"; //
DESIGNATION ARTICLE 32 CARACTERES
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Df\"002\"\"" + barcode + "\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Df\"003\"\"" + siteId + "\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Db\"01\"\"" + barcode + "\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Pi\"" + numCopies +
"\"\"000\"\"2:C:6:2:0\"\"0\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Pc\"+0120\"]>";
netString = System.String::Concat(netString, tmpString);
// RAW INSTRUCTIONS - End

remoteIp = new System.Net.IPEndPoint(ipAddress, port);
Socks = new
System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily::InterNetwork,
System.Net.Sockets.SocketType::Stream, System.Net.Sockets.ProtocolType::Tcp);

new InteropPermission(InteropKind::ClrInterop).assert();

Socks.Connect(remoteIp);
ns = new System.Net.Sockets.NetworkStream(Socks, true);
if (ns.get_CanWrite())
{
info("ns is available");
myBytes =
System.Text.Encoding::get_ASCII().GetBytes(netString);
ns.Write(myBytes, 0, myBytes.get_Length());
}
else
{
info("ns not available");
}
ns.Close();
Socks.Close();
CodeAccessPermission::revertAssert();

}
catch (Exception::CLRError)
{
clrException = CLRInterop::getLastException();
throw error(clrException.get_Message());
}
}
tcpClient.Close();
}

No errors are shown until I add the instruction :
ns.Write(myBytes, 0, myBytes.get_Length());
ERROR MESSAGE : "The System.Net.Sockets.NetworkSream class does not have a
Write method" (translated from french).
Dispite this error the Write method still appear in the class's methods list
... Would anyone have a clue of why this is happening ?
Jonathan
2010-08-09 14:30:03 UTC
Permalink
Hi again !
Post by Jonathan
ns = new System.Net.Sockets.NetworkStream(Socks, true);
if (ns.get_CanWrite())
{
info("ns is available");
myBytes =
System.Text.Encoding::get_ASCII().GetBytes(netString);
ns.Write(myBytes, 0, myBytes.get_Length());
}
else
{
info("ns not available");
}
ns.Close();
Socks.Close();
CodeAccessPermission::revertAssert();
By this:
new System.Net.Sockets.NetworkStream(Socks, true).Write(myBytes,
0, myBytes.get_Length());
Socks.Close();
CodeAccessPermission::revertAssert();

It is not really clean but it works ... Even if the system displays the
following message: 'CLRObject' object could not be created
Post by Jonathan
Hi everyone!
I am trying to send raw printing instructions to a network barcode printer
from AX2009.
tatic void Job10(Args _args)
{
System.String netString;
System.String tmpString;
System.String ipAddressString = "172.x.x.x";
System.String netIpAddress = "172.x.x.x";
BarcodeSetupId barcodeFamily = "CODE128";
ItemBarcode barcode = "barcode";
Description ItemDesignation = "";
InventSiteId siteId = "SITE";
str numCopies = "0001";
System.Type t = System.Type::GetType("System.Byte[]", true);
System.Byte[] byteArray;
CLRObject myBytes = System.Array::CreateInstance(t, 1024);
System.Net.Sockets.TcpClient tcpClient;
System.IO.Stream networkStream;
System.Net.IPEndPoint remoteIP;
System.Net.IPAddress ipAddress =
System.Net.IPAddress::Parse(netIpAddress);
System.Int32 port = 9100;
System.Net.Sockets.Socket Socks;
System.Net.Sockets.NetworkStream ns;
System.Exception clrException;
;
tcpClient = new System.Net.Sockets.TcpClient(netIpAddress, 9100);
networkStream = tcpClient.GetStream();
if(networkStream.get_CanWrite())
{
try
{
// RAW INSTRUCTIONS - Begin
netString = "<[Cd\"+02:0\"]><[Cl\"0330:0630:0300\"]>";
tmpString = "<[Cc]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Fa\"0000:0000\"\"0600:0300\"\"A\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Cp\"-000\"\"-000\"\"-00\"]>";
netString = System.String::Concat(netString, tmpString);
if (barcodeFamily == "EAN13")
{
tmpString =
"<[Fb\"0090:0090\"\"01\"\"0\"\"5:2:1\"\"0100\"\"03\"\"+0000000000\"\"030\"]>"; //REM POUR CODE BARRE EAN13
}
else if (barcodeFamily == "CODE128")
{
tmpString =
"<[Fb\"0090:0090\"\"01\"\"0\"\"9:2:1\"\"0100\"\"01\"\"+0000000000\"\"030\"]>"; //REM POUR CODE BARRE 128
}
netString = System.String::Concat(netString, tmpString);
tmpString =
"<[Ff\"0015:0060\"\"000\"\"00\"\"H:B\"\"05:10\"\"+00:00:00\"\"\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString =
"<[Ff\"0001:0285\"\"002\"\"00\"\"G:B\"\"10:10\"\"+00:00:00\"\"\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString =
"<[Ff\"0460:0285\"\"003\"\"00\"\"G:B\"\"10:10\"\"+00:00:00\"\"\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Pc\"-0120\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Df\"000\"\"" + ItemDesignation + "\"]>"; //
DESIGNATION ARTICLE 32 CARACTERES
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Df\"002\"\"" + barcode + "\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Df\"003\"\"" + siteId + "\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Db\"01\"\"" + barcode + "\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Pi\"" + numCopies +
"\"\"000\"\"2:C:6:2:0\"\"0\"]>";
netString = System.String::Concat(netString, tmpString);
tmpString = "<[Pc\"+0120\"]>";
netString = System.String::Concat(netString, tmpString);
// RAW INSTRUCTIONS - End
remoteIp = new System.Net.IPEndPoint(ipAddress, port);
Socks = new
System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily::InterNetwork,
System.Net.Sockets.SocketType::Stream, System.Net.Sockets.ProtocolType::Tcp);
new InteropPermission(InteropKind::ClrInterop).assert();
Socks.Connect(remoteIp);
ns = new System.Net.Sockets.NetworkStream(Socks, true);
if (ns.get_CanWrite())
{
info("ns is available");
myBytes =
System.Text.Encoding::get_ASCII().GetBytes(netString);
ns.Write(myBytes, 0, myBytes.get_Length());
}
else
{
info("ns not available");
}
ns.Close();
Socks.Close();
CodeAccessPermission::revertAssert();
}
catch (Exception::CLRError)
{
clrException = CLRInterop::getLastException();
throw error(clrException.get_Message());
}
}
tcpClient.Close();
}
ns.Write(myBytes, 0, myBytes.get_Length());
ERROR MESSAGE : "The System.Net.Sockets.NetworkSream class does not have a
Write method" (translated from french).
Dispite this error the Write method still appear in the class's methods list
... Would anyone have a clue of why this is happening ?
Loading...