Sending Modbus Packet Using Nim Sockets

2024-06-27 Thread jbeuter
Hello, I am a new nim user (forgive me if this post lacks info). Currently I am 
trying to create a socket connection and send a custom modbus packet. I am able 
to do this in C with  and build my own 
packet as a char array and send it. Using the nim documentation, I wrote the 
following code:


let
ip = ""
port = Port(502)


var
socket: Socket = newSocket()

socket.connect(ip, port)

#Create packet to write coil
var packet: array[12, int] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 
0x05, 0x00, 0x00, 0xFF, 0x00];

var dataPtr: ptr array = packet.addr


socket.sendTo(ip, port, dataPtr, sizeof(packet), AF_INET, 0'i32)



Run

With this I get the error "TCP not supported". Anyone have any tips? Is there a 
modbus library for nim that anyone can provide documentation for?

Thank you for any info!


Sending Modbus Packet Using Nim Sockets

2024-06-27 Thread jbeuter
The send() for socket accepts a string data type. I cannot get the actual 
packet to be received as Modbus when converted to a string. 


Sending Modbus Packet Using Nim Sockets

2024-06-28 Thread jbeuter
Do you have any documentation for how to implement this/nim resources for how 
to use this? I am pretty new to nim!


Sending Modbus Packet Using Nim Sockets

2024-06-28 Thread jbeuter
I got this to work! Here is the working code to send a basic Modbus on/off 
packet:


var
socket: Socket = newSocket()

socket.connect(ip, port)

#Create packet to write coil
#OFF packet
var packet: array[12, uint8] = [0x00'u8, 0x00, 0x00, 0x00, 0x00, 0x06, 
0x01, 0x05, 0x00, 0x00, 0x00, 0x00];

#ON packet
#var packet: array[12, uint8] = [0x00'u8, 0x00, 0x00, 0x00, 0x00, 0x06, 
0x01, 0x05, 0x00, 0x00, 0xFF, 0x00];

var dataPtr: ptr array = packet.addr

var output: int

output = socket.send(dataPtr, sizeof(packet))

echo output



Run

Thank you for the help everyone!