TCP/modbus

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

TCP/modbus

Post by blueznl »

Anyone got any experience accessing this via PureBasic? I know how to deal with WinSock, but I've never tried to communicate with modbus devices, so if you have done so, feel free to tel me :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Molchyn
User
User
Posts: 42
Joined: Thu Jan 29, 2004 12:54 am

Post by Molchyn »

here snippet to check WatLow temperature controller
Use "Esc" to exit and "." to check status of registers in time

Code: Select all

ModBUScontroller$="PDXXXXXX" ;by name 
ModBUScontroller$="10.10.10.47" ;by IP

;- Endian procs by wilbert - Thank you 
Procedure.w xchEndianW(e.w) 
  ProcedureReturn (e & $FF) << 8 + (e >> 8) & $FF 
EndProcedure 

Procedure xchEndianL(e.l) 
  ProcedureReturn (e & $FF) << 24 + (e & $FF00) << 8 + (e >> 8) & $FF00 + (e >> 24) & $FF 
EndProcedure 

InitNetwork() 
OpenConsole() 

h = OpenNetworkConnection(ModBUScontroller$,502,#PB_Network_TCP) 
*b = AllocateMemory(512) 
If h And *b 
  ZeroMemory_(*b,512) 
  PrintN("Connected") 
  Start$="0"

     PokeB(*b,$00) 
     PokeB(*b+1,$00) 
     PokeB(*b+2,$00) 
     PokeB(*b+3,$00) 
     PokeB(*b+4,$00)
     PokeB(*b+5,$06);
     PokeB(*b+6,$01)
     PokeB(*b+7,3) 
     PokeB(*b+8,$00) ; start high
     PokeB(*b+9,$00) ; start low
     PokeB(*b+10,$00) ;  
     PokeB(*b+11,$00) ;  
     PokeB(*b+12,$00) ;  
     PokeB(*b+13,$00) ;  
  
  
  If SendNetworkData(h,*b,12) = -1 
    PrintN("Send failed try to reconnect") 
    CloseNetworkConnection(h) 
    h = OpenNetworkConnection(ModBUScontroller$,502,#PB_Network_TCP) 
    If h
      PrintN("Reconnected")
    Else
      PrintN("Reconection failed")  
    EndIf
    
  Else
    PrintN("Send >") 
    For j=0 To 12 
      Print(" "+Right("0" + Hex(PeekB(*b+j)),2)) 
    Next 
    ; CallDebugger
  EndIf 
  ; ZeroMemory_(*b,512) 
  Repeat 
    e = NetworkClientEvent(h) 
    If e 
      PrintN("")  
      
      i = ReceiveNetworkData(h,*b,512) 
      PrintN("Sended: " + Str(i)+" bytes") 
      For j=1 To i  
          Print(Right("0" + Hex(PeekB(*b+j)),2)) 
      Next 
      PrintN("") 
      PrintN("")
 
      zzz.l=PeekL(*b+71)
      Temperature.f=xchEndianL(zzz)/1000
      
      PrintN("Fahrenheit: " +StrF(Temperature,3))
      PrintN("Celsius: " +StrF((Temperature-32)*5/9,3))
    EndIf 
    
    Text$ =Inkey()
    If Text$=Chr(46)
      i+1
      PokeB(*b,$00) ;Transaction/invocation Identifier (2 Bytes): This identification
      PokeB(*b+1,$00);field is used For transaction pairing when multiple messages are
                     ; sent along the same TCP connection by a client without waiting For
                     ;  a prior response.
      
      PokeB(*b+2,$00)  ;Protocol Identifier (2 bytes): This field is always 0 for Modbus
      PokeB(*b+3,$00)  ;services and other values are reserved for future extensions. 
      
      PokeB(*b+4,$00) ;Length (2 bytes): This field is a byte count of the remaining fields
      PokeB(*b+5,$06) ;and includes the unit identifier byte, function code byte, and the data fields.
      
      PokeB(*b+6,$01);Unit Identifier (1 byte): This field is used to identify a remote
      ;server located on a non TCP/IP network (for serial bridging). In a
      ;typical Modbus TCP/IP server application, the unit ID is set to 00 or
      ;FF, ignored by the server, and simply echoed back in the response.
        
        PokeB(*b+7,3) 
        PokeB(*b+8,00) ; start high
        PokeB(*b+9,00) ; start low
        PokeB(*b+10,$00) ;  
        PokeB(*b+11,$FF) ;  
        PokeB(*b+12,$00) ;  
        PokeB(*b+13,$00) ;  
        If SendNetworkData(h,*b,12) = -1 
          PrintN("") 
          PrintN("Send failed try to reconnect") 
          CloseNetworkConnection(h) 
          h = OpenNetworkConnection(ModBUScontroller$,502,#PB_Network_TCP) 
          If h
            PrintN("Reconnected")
          Else
            PrintN("Reconection failed")  
          EndIf 
        Else
          PrintN("")
          PrintN("Send >") 
          For j=0 To 12 
            Print(" "+Right("0" + Hex(PeekB(*b+j)),2)) 
          Next 
          ; CallDebugger
        EndIf 
        Delay(10)
      
      EndIf

  Until Text$ = Chr(27) 
  CloseNetworkConnection(h) 
  If *b 
    FreeMemory(*b) 
  EndIf 
EndIf 
PrintN("End") 
CloseConsole()  
Last edited by Molchyn on Tue Feb 10, 2009 6:47 pm, edited 3 times in total.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Cool! Thanks, I'm going to try this next week.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply