web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Using tcpClient in X++ To Communicate With Equipment

(0) ShareShare
ReportReport
Posted on by 240
We are attempting to retrieve data from a piece of equipment connected to our network.  This equipment happens to be a wireless scale with an IP address assigned to it.  We have no problem connecting to the scale via Hypertrm yet cannot receive anything using X++ code.  When connected via Hypertrm, the scale prompts for a user ID and password.  Once 'logged in', you must enter some commands to get the scale to output the weight which is just a real number.  We are calling methods connect() then requestLine() and cannot even get the initial 'login:' prompt must less log in and retrieve the weight.  I've tried sending IOStreamW.Write() with different values (Cr, CrLf, spaces) right before the IOStreamR.ReadLine() to try to coax it into sending something with no success.  Please see the code below and thanks in advance for any help:

class declaration:
    System.Net.Sockets.TcpClient        tcpClient;
    System.Net.Sockets.NetworkStream    tcpStream;
    System.IO.StreamWriter              IOStreamW;
    System.IO.StreamReader              IOStreamR;
    real                                weight;
    str                                 line;
    str                                 ipAddress;

void connect()
{
    ;
    tcpClient.Connect(ipAddress, 23);
    tcpStream       = tcpClient.GetStream();
    IOStreamW       = new System.IO.StreamWriter(tcpStream);
    IOStreamR       = new System.IO.StreamReader(tcpStream);
}

str requestLine()
{
    ;
    if (tcpStream.get_CanWrite() && tcpStream.get_CanRead())
    {
        line = IOStreamR.ReadLine();
    }
    return line;
}

*This post is locked for comments

I have the same question (0)
  • Denis Patrakov Profile Picture
    on at
    Is your class bound to run on client or on server? If it runs on a server then is there an IP route to your scale? There are lots of means to troubleshoot networking issues and Network Monitor is among my favorite ones.
  • Glen Hassell Profile Picture
    225 on at

    Hi Mike,

    System.IO.Ports is the way I approached this previously.
     
    It's an interesting approach to connect directly using TCP/IP and can see the advantages!
     
     

    comport.BaudRate = int.Parse(cmbBaudRate.Text);
    comport.DataBits = int.Parse(cmbDataBits.Text);
    comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.Text);
    comport.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
    comport.PortName = cmbPortName.Text;

    // Open the port
    comport.Open();

    So to use this as a setting = private SerialPort port = new SerialPort("COM1",9600, Parity.None, 8, StopBits.One);

     

    From this code snippet you can see, the most difficult aspect is going to be establishing the connection. If you already have an IP address for the device, this is reduced dramatically and you need a response from the device to ensure that you are talking to the right device and initiate a login / handshake.

    Each device has it's own configuration for specifically handling handshaking routines, you will need the technical manual, which may take some time in tracking down but will be worth it's weight in gold!

    You need a class to handle DataReceived, which is a like your requestLine() string method, with the additional of being an event handler, which again is different when working with - SerialDataReceivedEventArgs - as found in System.IO.Ports.

    Another idea, using the - tcpStream - reference to System.Net.Sockets - how about calling .connected() method opposed to seeing if you can read or write, as if it's connected, you will retrieve some data leading you further than the empty return. Try to not be so specific in your naming at the early stages as you can tunnel deeper in once you have established the Connection and can ensure that you have data returning from the device, then build out from there.

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans