System.serial.write command data type confusion

I am trying to communicate with a device using the system.serial.write and the system.serial.writeBytes commands. I want to send it Modbus RTU commands so that I can retrieve certain registers for testing. I need to send it hexadecimal numbers in a sequence in order for it to respond. I can successfully send a command through the writeBytes function but I’m unable to send a number over 128 because that number is no longer represented by 1 byte. I try to use the system.serial.write command but I can’t get a response from the device. I am sending the same data for the system.serial.write command but I’m representing the hexadecimal command as a string instead of a byte array. I’m just not sure how the system.serial.write command sends data to the device and why I’m not getting a response back.

A few things:

  • Bytes in java are signed two’s complement. Subtract 256 from byte values over 127 to get the valid byte for a java byte array.

  • Is there any particular reason you aren’t using Ignition’s Modbus driver’s RTU support?

  • If the reason is a function code not supported by Ignition’s driver, consider using my Advanced Modbus driver. In addition to more built-in supported function codes, it also exposes a gateway scripting function for exchanging messages with arbitrary functions and payloads. [/shameless plug]

The reason I’m not using the Modbus driver is because I am only connecting to the device locally. I don’t want the device added to the gateway but am instead communicating locally through the vision client.

Hmm, this sounds like a project I have down the road.

I will need to connect Ignition to a CNC machine using a serial connection. My interface will be by writing and reading text strings and GCode comands to and from a vision client. I was wondering if this was even possible. Hope you don’t mind but I’m curious to see how this turns out.

Oy. Not fun. I’d use an ethernet-to-serial device there to avoid that pain.

Just an update. So I used the Ignition serial module and started to use the serial.write command. I needed to send the bytes in python unicode in order for my device to properly read the data. I learned that the serial.write command would send each character as a separate byte but I was using hex so I needed to use unicode. It was pretty complicated but it worked.