Search All Forums
Dart Home | PowerTCP Sockets for .NET | Custom Development Reply | PowerTCP Sockets for .NET (Secure and Standard) Topics | Forums   
AuthorForum: PowerTCP Sockets for .NET (Secure and Standard)
Topic: TcpClient Sample project, read Modbus Byte Array

From: USA
Posts: 25
Member Since: 02/25/15
posted October 1, 2020 7:36 AM

I'm testing with your TcpClient.40 sample project.
I'm trying to Read ModBus Holding Registers.
The Read request is working fine (sending the correct read request), the Slave is receiving the read request, and responding back correctly.

However, the Read code for the data arriving back into the client, is just throwing up some blank spaces & a few unprintable character's. I can't seem to figure out how to get this to correctly display the ModBus Byte Array in the text box.

Code is below:
  //Read response
  byte[] buffer = new byte[1024];
  Data data = _Tcp.Read(buffer);
  //Continue to read and marshal data
  while (data != null)
    _Tcp.Marshal(data, "", null);
    //Read response
    buffer = new byte[1024];
    data = _Tcp.Read(buffer);

Below is the traffic logging by the Slave:
The TX is what I should be receiving in my Client.

Rx:01 03 00 00 00 02 C4 0B
Tx:01 03 04 00 16 00 2C 1A 2A

Thanks in advance for any assistance with this,
Tim Snelling

From: USA
Posts: 25
Member Since: 02/25/15
posted October 1, 2020 12:44 PM

I think i got this figured out.

I added code inside the
"while (data != null)"

// convert byte array, to Hexidecimal "X" format.
// "X2" forces it to fill with a leading Zero,
// as needed.
s1 = buffer[i].ToString("X2");
if (s1.Length == 2)
  // put the data into a hex string,
  // for the display.
  sCombined += s1;

then I set up a Marshal for the UserState,
to pass back the sCombined string.
  _Tcp.Marshal(sCombined, null);

Please Let me know if there is an easier method of doing this.

Tim Snelling
Jamie Powell (Admin)

From: Rome, NY USA
Posts: 436
Member Since: 03/13/07

Extra Support Options
Custom Application Development

posted October 2, 2020 9:18 AM

Per your expected TX, most of that would be non-printable data, except for a comma and an asterisk (3rd to last, and last character, respectively) - the TCP Client sample is designed for sending textual data and receiving textual data for display. You can refer to this for what your TX hex values represent:
Unless you want to be displaying the hex values, instead of converting the data to ascii? In which case you can work against the byte array directly.
Reply | PowerTCP Sockets for .NET (Secure and Standard) Topics | Forums   
This site is powered by PowerTCP WebServer Tool PowerTCP WebServer for ActiveX