It is currently Sat May 25, 2013 9:40 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: TCP/modbus
PostPosted: Tue Sep 23, 2008 6:49 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 5808
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 :-)

_________________
( PB5.11 Win7 x64 Dell XPS710 Raid 0 VelociRaptor Intel Q6600 nForce 5 NVidia GTS450 )
( You have two options: psychotherapy, or the PureBasic Survival Guide... )


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 4:18 pm 
Offline
User
User

Joined: Thu Jan 29, 2004 12:54 am
Posts: 42
here snippet to check WatLow temperature controller
Use "Esc" to exit and "." to check status of registers in time

Code:
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.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 12:21 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 5808
Cool! Thanks, I'm going to try this next week.

_________________
( PB5.11 Win7 x64 Dell XPS710 Raid 0 VelociRaptor Intel Q6600 nForce 5 NVidia GTS450 )
( You have two options: psychotherapy, or the PureBasic Survival Guide... )


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Exabot [Bot], User_Russian and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye