Author Topic: ModbusTCP : Unable to write to a register  (Read 1549 times)

Slowpaddle

  • Newbie
  • *
  • Posts: 4
    • View Profile
ModbusTCP : Unable to write to a register
« on: September 15, 2021, 07:38:49 PM »
This is my first post so forgive me if I mess this up.

I am using ModbusTCP to communicate with an Arduino via Ethernet using mudbus.h. (The correct spelling) I can read and write bit values and read register values without any issues. But for some reason I cannot write data to a register.

I would like to read a text file and send the values to two registers when the program starts. These values represent offset values for two TOF sensors connected to the Arduino.

I am hoping to be able to use them to line up my two Y Axis stepper motor positions on my CNC gantry and use the offsets to allow for any mounting errors.

Code: [Select]
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        If My.Computer.FileSystem.FileExists("C:\axis_offsets.txt") Then

            Dim reader As New System.IO.StreamReader("C:\axis_offsets.txt")

            Dim MyValue1, MyValue2 As String

            MyValue1 = reader.ReadLine()
            Dim CurrentValueInt1 As Integer = CInt(MyValue1)

            MyValue2 = reader.ReadLine()
            Dim CurrentValueInt2 As Integer = CInt(MyValue2)

            reader.Close()

            ModbusTCPCom1.Write("40003", CurrentValueInt1)
            ModbusTCPCom1.Write("40004", CurrentValueInt2)

        Else
            Dim writer As New System.IO.StreamWriter("C:\axis_offsets.txt", False)
            writer.WriteLine(0)
            writer.WriteLine(0)
            writer.Close()
        End If
    End Sub

This button click code will not work either.

Code: [Select]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ModbusTCPCom1.Write("40003", 2)
        ModbusTCPCom1.Write("40004", 2)
    End Sub

If I could figure out a way to use an ESP8266 with AHMI, I'd give it a go. Save me the problem of storing offsets on my computer and saving them on the ESP8266.

I'm a recently retired Industrial Electrician (Canada) so maybe I'm just getting old and missed something simple?

Thank you for any help one of you can provide. It would be deeply appreciated.

Slowpaddle

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: ModbusTCP : Unable to write to a register
« Reply #1 on: September 15, 2021, 08:35:36 PM »
Just to add, I am using V3.99v if that helps.

Tried this as well to see if an error was thrown. Alas, nothing.

Code: [Select]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            ModbusTCPCom1.Write("40003", 2)
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show("Couldn't write to PLC. Error: " & ex.Message)
        End Try
    End Sub
« Last Edit: September 15, 2021, 08:47:36 PM by Slowpaddle »