Frustrated Modbus


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Hi Shawn,
    I looked at your TX-routine and the thing that caught my eye was:
    Code:
    Rts = 1
    For B0 = 1 To Length      ' Send the response to Master
      B1 = buffer[B0]
      Gosub charout
    Next B0
    Pause 5
    RTS = 0
    Pause 5000
    Now, you say that it all works and I can possibly see why in this particular example but in your real code do you always put the first byte of the frame at location 1 in the array? Arrays are zero-indexed so the first location of Buffer is Buffer[0] and the 6th location is Buffer[5].

    The reason it works in the example you posted is because PBP doesn't have any boundry checks on arrays so when you do Buffer[6]=6 it's actually writing to the memory location AFTER the last "slot" in the array. In this case there might not BE anything of importance there but in you're real program there's a risk you're actually overwriting another variable.

    Also, in this example you have the Buffer array declared as an array of words, is that what you want? It won't work properly when you try to stuff a 16 bit word into the 8bit TXReg - you'll loose the high byte which isn't a problem in this case but still....

    Finally, you could replace the Pause 5 with something like
    Code:
    While TXSTA.1=0 : WEND
    That should make it wait just long enough for the last bit to go out before turning off your transmitter.

    Well, that's my thoughs...

    /Henrik.

  2. #2
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Thanks Henrick,
    I knew better with the array numbering I just dropped the ball. As for the Word sized array I also realize that the Tx register is only 8 bits. It will be taken care of in the function routines.
    While TXSTA.1 = 0 : WEND that rocks thank you.
    Next question, I am switching this project over to 40Mhz and so I need to reconfigure timers. In the Modbus.txt file above, at the end it has a If Then statement that looks for a 10mS delay between Rx interrupts. This delay means that the modbus frame of characters is over. The comment says 10mS but for the life of me I cannot figure out where this comes from. It uses a 20Mhz OSC. It uses tmr2 with a 1:16 prescaler and then a variable that increments 1 time every time PIR1.1 is true. All I should have to do when switching from 20 to 40Mhz is take the 120 x 2 and use that value. But I would like to understand how it is getting 10mS.
    20000000/4=5000000 1/5000000=200nS 200nS*16= 3.2uS and 3.2uS*120=384uS
    Attached Files Attached Files

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Hi Shawn,
    TMR2 interrupts when its value matches what's in PR2. I don't see anything being written to PR2 in the code you posted so I'm assuming that its 0 and therefor TMR2 interrupts when it rolls over from 255 to 0.

    You have calculated that the timer "ticks" at 3.2us which is correct but it needs 256 ticks before it interrupts so there's 3.2*256=819.2us between each interrupt. The ISR then counts 120 interrupts so 0.819ms*120=~100ms..... Hmm I must be doing something wrong...

    Anyway, you should know that according to the MODBUS specification the correct interframe delay or timout depends on the baudrate at which the network is run. For baudrates of 19200 and below the interframe delay should equal 3.5 characters and for baudrates over 19200 the delay should be fixed at 1750us.

    So to meet the specification the delay at 9600baud should be 1/9600*11bits*3.5 = 4ms.

    /Henrik.

  4. #4
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Hey Henrik,
    I guess it doesn't really matter what the modbus code said because I am revamping it. I didn't actually write that code, or at least the communication part. I copied it from someone else's work. It only had a couple of modbus functions and it used an I2C chip to read and write to registers. So I modified it. As far as I can tell, the codes TMR interrupts every 98mS which is way to long and that might have been part of my problem.
    @20Mhz and a 1:16 pre-scaler TMR1 Variable counts to 120, this is my math for it.
    1/5000000=200nS 200nS*16=3.2uS 3.2uS*256=819.2uS
    819.2uS*120=98.304mS
    In post #7 I forgot to figure in the 8bit PR2 register.

    So, check my math if you would, what I need to do is.....
    @40Mhz-1:16 pre-Scaler-8bit TMR
    I need my TMR Counter Variable to count to 10.
    40Mhz/4=10Mhz 1/10Mhz=100nS 100nS*16=1.6uS 1.6uS*256=409.6uS
    409.6uS*10=4.096mS

    Thats way closer to 4mS then it was unless my math is wrong. I am going to try and scope it to see what I get

    Shawn

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Hi Shawn,
    Math seems correct to me and ~4ms interframe timeout is what you want for 9600 baud. I can't really see why the 98ms timeout would cause you any trouble as long as the masters timeout setting is set long enough. Obviously it would take unnecessary time but again, as long the masters timeout setting allows it it should have worked.

    /Henrik.

  6. #6
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Hello,
    How is everyone? I got a new question, it is related to this project but not to modbus. If no one minds I am just going to keep asking questions on this post. I am working with a MCP4209 DA Converter. I want to output 0 to 10V. So I want to run the output of the 4209 into a non inverting 741 op amp. My problem is, the lowest value out of the op amp I can get is 3.97V. Is that all the closer a 741 can get to the lower rail. I have 12V going to the positive rail and ground hooked to lower rail. I get my 10V out when my 4209 is putting out 5V.
    I have a 10K resistor hooked from ground to pin2 of the 741. And a 10K resistor from pin2 of the 741 to pin6 of the 741. Input from the 4209 is running to pin3 of the 741.
    If I run the 741 at + and - 12V all works good but I do not want to add a negative voltage supply to my project. Is there anyway around this?
    Thanks
    Shawn
    Last edited by shawn; - 1st May 2011 at 20:46. Reason: lacking info

  7. #7
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Quote Originally Posted by shawn View Post
    Hello,
    How is everyone? I got a new question, it is related to this project but not to modbus. If no one minds I am just going to keep asking questions on this post. I am working with a MCP4209 DA Converter. I want to output 0 to 10V. So I want to run the output of the 4209 into a non inverting 741 op amp. My problem is, the lowest value out of the op amp I can get is 3.97V. Is that all the closer a 741 can get to the lower rail. I have 12V going to the positive rail and ground hooked to lower rail. I get my 10V out when my 4209 is putting out 5V.
    I have a 10K resistor hooked from ground to pin2 of the 741. And a 10K resistor from pin2 of the 741 to pin6 of the 741. Input from the 4209 is running to pin3 of the 741.
    If I run the 741 at + and - 12V all works good but I do not want to add a negative voltage supply to my project. Is there anyway around this?
    Thanks
    Shawn
    Hi try to use a LM358 Insted of the 741. Power the LM at +12V and ground and post the results.
    Thanks and Regards;
    Gadelhas

  8. #8
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    ok,
    I wrote a short program to test the 4mS TMR2 routine. My math seems to be right I scoped it and all is good. Next part of the project will be he Rx code.

    Shawn

  9. #9
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    Ok
    New puzzle for ya all. This may be too much code for you to go over. But I am going to attach it anyway. This program is only to interrupt receive a modbus frame and detect the end of the modbus frame, then echo it back out the tx line. It seems to be working kinda, however I am only receiving 4 bytes of information back no matter what size of modbus frame I send. I put this line of code in to display my length variable which is the variable that determines how many bytes are transmitted out of the pic.
    Serout2 LCD,16468,[12,dec length]
    Length is displaying as zero. I think somewhere I am having a problem incrementing my length variable. I am still sifting through the code but I thought I would post it for anyone that is bored.
    Oh ya, I realize that the Serout2 command is a time eater but I am manually sending the ModBus frames from my Pc using MBPoll. When I first wrote the program I did not have the Serout2 command in there and still had the same problem.
    Thanks
    Shawn
    Attached Files Attached Files

  10. #10
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Re: Frustrated Modbus

    I'm Back
    Ok, I am making progress, maybe. I know what the problem is but I haven't narrowed down the cause yet. I broke up the receiver error routine into to parts to check RCSTA.1 and RCSTA.2 individually. If either one errors I turn on a corresponding LED. Now every time my ModBus Master sends a request I get an error on RCSTA.2. I get an error but my slave(pic) still sends out the proper message to the master. Now the goofy thing is, is that I do not have this problem when using rs232. Only have the problem using my 485 to 232 converter.
    When using the rs232 chip I can have the cpu scan at 5ms with no errors. Using the 485 to 232 if I scan ever 5 senconds my message gets through but my pic still has RCSTA.2 errors. It errors every time.

    At this point I am no longer sure if its a software problem or a hardware problem. I'm not real sure what a framing error is but I am going to investigate.
    Heres my code if anyone would like to through in their 2 cents.
    The readRegResponse: label is the subroutine where the RTS line is pulled and data sent out.

    Thanks
    Shawn
    Attached Files Attached Files

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts