Announcement

Collapse
No announcement yet.

Modbus RTU

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SEvans
    replied
    Sorry for the late reply Richard..thanks for the insight.Help is always appreciated from the experienced.

    Leave a comment:


  • richard
    replied
    i wrote this modbus sniffer some years ago , from recollection it worked ok for debugging mach3 modbus issues
    modbus connects using rs485 to monitor rx/tx @9600b on rx1/rx2 with output on porta1 @38400b

    Code:
    '****************************************************************
    '*  Name    :modbus_sniffer.BAS                                  *
    '*  Author  : richard                                            *
    '*  Notice  : Copyright (c) 2011                                 *
    '*          : All Rights Reserved                                *
    '*  Date    : 16/03/2012                                         *     
    '*  Version : 1.0                                                *
    '*  Notes   :                                                    *
    '*          :  18f25k22                                          *
    '****************************************************************
    
    
    
              #CONFIG
               config            PBADEN = OFF 
               config            MCLRE = EXTMCLR
               config            LVP = OFF  
               config            FOSC = INTIO67 
               config            PRICLKEN = ON
               config            PLLCFG = OFF
    
            CONFIG  WDTEN = OFF
            CONFIG  PWRTEN = ON
            CONFIG  BOREN = OFF
    
            CONFIG  DEBUG = OFF
            CONFIG  XINST = OFF
    
    
               #ENDCONFIG
    
    
         include "dt_ints-18.bas"
        include "REENTERPBP-18.bas"
        ASM
    INT_LIST macro
            INT_Handler RX1_INT , _RXA,asm,YES
            INT_Handler RX2_INT , _RXB,asm,YES
            INT_Handler TMR2_INT , _TA,PBP,YES
            INT_Handler TMR4_INT , _TB,PBP,YES  
           endm
            INT_CREATE
        ENDASM          
    @    INT_ENABLE   RX1_INT     
    @    INT_ENABLE   RX2_INT            
    @    INT_ENABLE   TMR4_INT            
    @    INT_ENABLE   TMR2_INT            
    
             DEFINE OSC 64 
    
              RCSTA1=$91       ;n-8-2 for mach3   should be $91
             BAUDCON1.3=1
          SPBRG1=   130       ;9600 modbus
           SPBRGH1= 6
           TXSTA1=4
              RCSTA2=$91
               BAUDCON2.3=1
          SPBRG2=   130     ;9600 modbus
           SPBRGH2= 6
           TXSTA2=4 
    
    
             OSCTUNE.6=1
             ANSELB=0
             ANSELC=0
             T4CON=$79
             T2CON=$79
             OSCCON=$70
            SSP1CON1.5=0
            CCP2CON=0
            CCP1CON =0   
            TRISA = %11111101 
            TRISB = %11111111
            TRISC = %11111111
    
    
            DOUT VAR PORTA.1    ;output and baudrate
            BR CON 6        ;6 = 38400,32 19200 etc
    
    
           A_C VAR BYTE
           B_C VAR BYTE
    
           BUFFERA    VAR BYTE[16]       ;16 byte buffer
           BUFFERB    VAR BYTE[16]
            FLG VAR BYTE
           j var byte
    
    
    
    
             A_C=0
             b_C =0
             flg=0
             dout=1
    
    pause 2000  
    serout2 DOUT,BR, [ "ready  38400"]  
    
    
    
    GOTO OVERIT 
    
    
    
        TA:
     ;for testing of end of host transmission     ' 2ms timeout
    
        T2CON=$79       ; NO TIMER
        PIE5.1=0
        FLG.0=1
    @ INT_RETURN
    
    
    
        TB:
     ;for testing of end of slave transmission    ' 2ms timeout
    
        T4CON=$79       ; NO TIMER
        PIE3.1=0
        FLG.1=1
    @ INT_RETURN
    
    
    
        RXA:
     ;recievr a chr  from host
    
         TMR2=128          ' 2ms timeout
        T2CON=$3E
    
    
    
        BUFFERA[A_C]=RCREG1
       A_C=A_C+1
        IF ( A_C>=15) THEN PIE1.5=0
    
    
    
    
    
    @ INT_RETURN
    
    
    
        RXB:
     ;recievr a chr   from slave
    
    
    
    
        TMR4=128        ' 2ms timeout
        T4CON=$3E
    
    
    
        BUFFERB[B_C]=RCREG2
        B_C=B_C+1
        IF ( B_C>=15) THEN PIE3.5=0
    
    @ INT_RETURN
    
    
    OVERIT:
    
    
    
    
    
    
     start:     
    
    
        IF FLG.0 =1 THEN 
    
    
            flg.0=0
           serout2 DOUT,BR, [ "h "]
              for j= 0 to a_c-1
      serout2 DOUT,BR, [hex BUFFERa[J],","] 
                       BUFFERa[J]=0
                   NEXT
    
    serout2 DOUT,BR, [ 13,10] 
    
    
    
             PIR1.5=0
    
            A_C=0
            RCSTA1.4=0  ;clear any rcsta error
            RCSTA1.4=1
             PIE1.5=1
    
    
        endif
    
    
    
        IF FLG.1 =1 THEN       
            flg.1=0
                  serout2 DOUT,BR, [ "s "]
              for j= 0 to b_c-1
      serout2 DOUT,BR,[hex BUFFERb[J],","] 
                       BUFFERb[J]=0
                   NEXT
    
    serout2 DOUT,BR, [ 13,10] 
    
    
            PIR3.5=0
    
            B_C=0
            RCSTA2.4=0  ;clear any rcsta error
            RCSTA2.4=1
            PIE3.5=1
    
        endif
    
    
    GOTO START

    Leave a comment:


  • richard
    replied
    My need simply put is that i need master code for pic 16 or 18 to connect through simple rx tx converter to the power meter

    there is nothing simple about modbus https://en.wikipedia.org/wiki/Modbus.
    you will need to supply a lot more info to get a meaningful response

    rx tx converter ? baudrate? parity? stop bits?. rs232/rs485 . simplex/duplex

    power meter ? a link or a datasheet ?. modbus commands accepted/implemented (variation to standard) / data format for registers? crc binomial?

    at least put a logic analyser on it and capture a command-reply sequence or two
    for a starting point

    Leave a comment:


  • SEvans
    replied
    Need a serial output string to the meter.RTU....please assist thank you

    Leave a comment:


  • SEvans
    replied
    Can someone contact me concerning this.The resolution of this issue comes with renumeration. EU200.. My need simply put is that i need master code for pic 16 or 18 to connect through simple rx tx converter to the power meter modbus... My pc already connects to the meter and is working fine..meter slave address is 1...need to read 1 analogue input register at a time at a time.

    Leave a comment:


  • SEvans
    started a topic Modbus RTU

    Modbus RTU

    Hi all ,has anyone attempted a modbus connection where the pic is the master connecting to slave?
Working...
X