EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

listening to rs485 serial port on BL4S200

Started by "yan...@omniverter.com [rabbit-semi]" August 10, 2016
Hello,

I am using BL4S200 for my control project. BL4S200 is a slave device. Other equipment uses Modbus RS485 to communicate BL4S200. Actually, everything is working, but the only thing is I need use loop to check serial port weather I receive any request, is it possible to use some internal interrupt to detect information received on serial port? so I can remove the fast loop to save some resource.

Thanks
Your program is the only thing running, and the BL4S200 runs at a fixed speed. A typical program is going to have a big loop where it checks for things to do. The overhead of checking for available data on a serial port is negligible in the overall time it takes that loop to run, so it's probably fine as it is. And in most cases, you're better off processing serial data at a known point in program execution, instead of interrupting some other task to process it.

-Tom
On Aug 10, 2016, at 10:28 AM, y...@omniverter.com [rabbit-semi] wrote:
> I am using BL4S200 for my control project. BL4S200 is a slave device. Other equipment uses Modbus RS485 to communicate BL4S200. Actually, everything is working, but the only thing is I need use loop to check serial port weather I receive any request, is it possible to use some internal interrupt to detect information received on serial port? so I can remove the fast loop to save some resource.
Hello, Tom:

Thank you for your response. Actually, I am using serval "costate" with
different loop timing. My current serial port use one of them "costate" with
15 msec loop time. My RS485 port use 115kbps. Do you think how often
checking serial port is reasonable according to your experience?

Regards,

Yang

From: r... [mailto:r...]
Sent: Wednesday, August 10, 2016 2:01 PM
To: r...
Subject: Re: [rabbit-semi] listening to rs485 serial port on BL4S200

Your program is the only thing running, and the BL4S200 runs at a fixed
speed. A typical program is going to have a big loop where it checks for
things to do. The overhead of checking for available data on a serial port
is negligible in the overall time it takes that loop to run, so it's
probably fine as it is. And in most cases, you're better off processing
serial data at a known point in program execution, instead of interrupting
some other task to process it.

-Tom
On Aug 10, 2016, at 10:28 AM, y...@omniverter.com
[rabbit-semi] wrote:

I am using BL4S200 for my control project. BL4S200 is a slave device. Other
equipment uses Modbus RS485 to communicate BL4S200. Actually, everything is
working, but the only thing is I need use loop to check serial port weather
I receive any request, is it possible to use some internal interrupt to
detect information received on serial port? so I can remove the fast loop to
save some resource.
At 115kbps, you'll see a maximum of 11.5kbytes/second, or 11.5bytes/msec.

So in your 15 msec loop time, you can receive a maximum of 172.5 bytes.

Are your buffers large enough to hold that? If so, you're probably fine.

If your test for received bytes is short, you could sleep for just 5ms instead of 15ms and probably not see a difference in the performance of your other costates.

-Tom
On Aug 10, 2016, at 12:20 PM, 'Yang Hong' y...@omniverter.com [rabbit-semi] wrote:
> Thank you for your response. Actually, I am using serval costate with different loop timing. My current serial port use one of them costate with 15 msec loop time. My RS485 port use 115kbps. Do you think how often checking serial port is reasonable according to your experience?
Hi Yang,

Sorry I have to disagree with Tom but Modbus is quite sensitive to timing issues: You have the 1.5 characters maximum delay between characters and the 3.5 characters spacing at the end of each message. Even parsing messages becomes complicated if you don't rely on the 3.5 characters gap between messages.

Not too long ago I had to implement a Modbus master on a a Rabbit module and pretty much I had to start from scratch. There is a Digi library for Modbus but exactly this low level part is not very well implemented.

At 115K baudrate (assuming 8-N-1 format which makes for 10 bits per char) the delays you are dealing with are:
- 1.5 chars = 0.13ms You have to send your answer without any gaps larger than that. Make sure you prepare your whole reply before starting to send and don't even dream about not using interrupts to pump out the whole message.

- 3.5 chars = 0.3ms Any receiving gap larger than this must be interpreted as an end of message and you have to parse that message. If the message is not addressed to you, the other slave will probably reply and will see its reply in your receive buffer (that's just the nature of RS485 bus). The only guidance you have to make sense of all that traffic are the 0.3ms gaps that you will see in the data stream.

When I'm talking about the 3.5 characters gap it doesn't mean you have to answer immediately when that delay expires. Most masters will accept a reasonable delay (frequently 1 sec) for a slave to answer. It means only that you must be prepared to correctly interpret the Modbus traffic, even if it is generated by communication with a very fast client.

If you need more explanations let me know. Also if you need my code (sorry, it is for a Modbus master).

Cheers,
Mircea

---In r..., wrote :

Hello, Tom:

Thank you for your response. Actually, I am using serval “costate” with different loop timing. My current serial port use one of them “costate” with 15 msec loop time. My RS485 port use 115kbps. Do you think how often checking serial port is reasonable according to your experience?

Regards,

Yang

From: r... [mailto:r...]
Sent: Wednesday, August 10, 2016 2:01 PM
To: r...
Subject: Re: [rabbit-semi] listening to rs485 serial port on BL4S200
Your program is the only thing running, and the BL4S200 runs at a fixed speed. A typical program is going to have a big loop where it checks for things to do. The overhead of checking for available data on a serial port is negligible in the overall time it takes that loop to run, so it's probably fine as it is. And in most cases, you're better off processing serial data at a known point in program execution, instead of interrupting some other task to process it.

-Tom

On Aug 10, 2016, at 10:28 AM, yang@... mailto:yang@... [rabbit-semi] wrote:

I am using BL4S200 for my control project. BL4S200 is a slave device. Other equipment uses Modbus RS485 to communicate BL4S200. Actually, everything is working, but the only thing is I need use loop to check serial port weather I receive any request, is it possible to use some internal interrupt to detect information received on serial port? so I can remove the fast loop to save some resource.
Mircea,

Thanks for the detailed response! I don't have any experience with MODBUS, so it sounds like for a slave you'd want to modify the interrupt handler for received bytes to automatically frame the messages based on the 3.5 character delay between messages.

Queuing an entire message in the outbound serial buffer should be fine, since the Rabbit uses an ISR to keep the UART loaded with bytes.

If you were marking the start/end of each message inside the ISR, servicing the message queue every 15ms would suffice based on your statement that replies can take up to 1 second to send.

-Tom
On Aug 10, 2016, at 8:03 PM, m...@neacsu.net [rabbit-semi] wrote:

> Hi Yang,
>
> Sorry I have to disagree with Tom but Modbus is quite sensitive to timing issues: You have the 1.5 characters maximum delay between characters and the 3.5 characters spacing at the end of each message. Even parsing messages becomes complicated if you don't rely on the 3.5 characters gap between messages.
>
> Not too long ago I had to implement a Modbus master on a a Rabbit module and pretty much I had to start from scratch. There is a Digi library for Modbus but exactly this low level part is not very well implemented.
>
> At 115K baudrate (assuming 8-N-1 format which makes for 10 bits per char) the delays you are dealing with are:
> - 1.5 chars = 0.13ms You have to send your answer without any gaps larger than that. Make sure you prepare your whole reply before starting to send and don't even dream about not using interrupts to pump out the whole message.
>
> - 3.5 chars = 0.3ms Any receiving gap larger than this must be interpreted as an end of message and you have to parse that message. If the message is not addressed to you, the other slave will probably reply and will see its reply in your receive buffer (that's just the nature of RS485 bus). The only guidance you have to make sense of all that traffic are the 0.3ms gaps that you will see in the data stream.
>
> When I'm talking about the 3.5 characters gap it doesn't mean you have to answer immediately when that delay expires. Most masters will accept a reasonable delay (frequently 1 sec) for a slave to answer. It means only that you must be prepared to correctly interpret the Modbus traffic, even if it is generated by communication with a very fast client.
>
> If you need more explanations let me know. Also if you need my code (sorry, it is for a Modbus master).
>
> Cheers,
> Mircea
>
> ---In r..., wrote :
>
> Hello, Tom:
>
>
> Thank you for your response. Actually, I am using serval costate with different loop timing. My current serial port use one of them costate with 15 msec loop time. My RS485 port use 115kbps. Do you think how often checking serial port is reasonable according to your experience?
>
>
> Regards,
>
>
> Yang
>
>
> From: r... [mailto:r...]
> Sent: Wednesday, August 10, 2016 2:01 PM
> To: r...
> Subject: Re: [rabbit-semi] listening to rs485 serial port on BL4S200
>
>
>
> Your program is the only thing running, and the BL4S200 runs at a fixed speed. A typical program is going to have a big loop where it checks for things to do. The overhead of checking for available data on a serial port is negligible in the overall time it takes that loop to run, so it's probably fine as it is. And in most cases, you're better off processing serial data at a known point in program execution, instead of interrupting some other task to process it.
>
>
> -Tom
>
> On Aug 10, 2016, at 10:28 AM, yang@... [rabbit-semi] wrote:
>
> I am using BL4S200 for my control project. BL4S200 is a slave device. Other equipment uses Modbus RS485 to communicate BL4S200. Actually, everything is working, but the only thing is I need use loop to check serial port weather I receive any request, is it possible to use some internal interrupt to detect information received on serial port? so I can remove the fast loop to save some resource.
>
>
>
>
Hi Tom,

You are right: he will have to build a small FSM in the the interrupt handler and that can notify the non-interrupt code when a message is waiting. Presumably it will also discard all traffic that is not destined to that slave (each slave has an address) and issue notifications only for properly addressed messages.

It has to be however a fairly smart piece of code as it must time intervals shorter than the standard Rabbit "tick" (1/1024 sec). A possibility is to use one of the Rabbit timers.

Cheers,
Mircea
Hi Mircea,

Can you share your code for modbus master.
Thanks.
Stev

On Thu, Aug 11, 2016 at 10:34 AM, m...@neacsu.net [rabbit-semi] <
r...> wrote:

> Hi Tom,
>
> You are right: he will have to build a small FSM in the the interrupt
> handler and that can notify the non-interrupt code when a message is
> waiting. Presumably it will also discard all traffic that is not destined
> to that slave (each slave has an address) and issue notifications only for
> properly addressed messages.
>
> It has to be however a fairly smart piece of code as it must time
> intervals shorter than the standard Rabbit "tick" (1/1024 sec). A
> possibility is to use one of the Rabbit timers.
>
> Cheers,
> Mircea
Hi Stev,
I will do that tomorrow morning.

---
Mircea Neacsu
T: 514-694-7158
M: 514-591-0329
E: m...@neacsu.net

Sent in Morse code from my rotary phone
> On Aug 10, 2016, at 23:38, S Sutikno s...@gmail.com [rabbit-semi] wrote:
>
> Hi Mircea,
>
> Can you share your code for modbus master.
> Thanks.
> Stev
>
>> On Thu, Aug 11, 2016 at 10:34 AM, m...@neacsu.net [rabbit-semi] wrote:
>>
>> Hi Tom,
>>
>> You are right: he will have to build a small FSM in the the interrupt handler and that can notify the non-interrupt code when a message is waiting. Presumably it will also discard all traffic that is not destined to that slave (each slave has an address) and issue notifications only for properly addressed messages.
>>
>> It has to be however a fairly smart piece of code as it must time intervals shorter than the standard Rabbit "tick" (1/1024 sec). A possibility is to use one of the Rabbit timers.
>>
>> Cheers,
>> Mircea
>>
Hello, Tom and Mircea:

Thank you for your response. They are valuable for me. Do you have any code
to describe ISR to keep the UART loaded with bytes? In fact, I use BL4S200
RS485 functions to develop my Modbus slave function. As I understand,
BL4S200 RS485 functions are top of lower serial port function.

Regards,

Yang

From: r... [mailto:r...]
Sent: Wednesday, August 10, 2016 11:17 PM
To: r...
Subject: Re: [rabbit-semi] listening to rs485 serial port on BL4S200

Mircea,

Thanks for the detailed response! I don't have any experience with MODBUS,
so it sounds like for a slave you'd want to modify the interrupt handler for
received bytes to automatically frame the messages based on the 3.5
character delay between messages.

Queuing an entire message in the outbound serial buffer should be fine,
since the Rabbit uses an ISR to keep the UART loaded with bytes.

If you were marking the start/end of each message inside the ISR, servicing
the message queue every 15ms would suffice based on your statement that
replies can take up to 1 second to send.

-Tom
On Aug 10, 2016, at 8:03 PM, m...@neacsu.net
[rabbit-semi] wrote:
Hi Yang,

Sorry I have to disagree with Tom but Modbus is quite sensitive to timing
issues: You have the 1.5 characters maximum delay between characters and the
3.5 characters spacing at the end of each message. Even parsing messages
becomes complicated if you don't rely on the 3.5 characters gap between
messages.

Not too long ago I had to implement a Modbus master on a a Rabbit module and
pretty much I had to start from scratch. There is a Digi library for Modbus
but exactly this low level part is not very well implemented.

At 115K baudrate (assuming 8-N-1 format which makes for 10 bits per char)
the delays you are dealing with are:
- 1.5 chars = 0.13ms You have to send your answer without any gaps larger
than that. Make sure you prepare your whole reply before starting to send
and don't even dream about not using interrupts to pump out the whole
message.

- 3.5 chars = 0.3ms Any receiving gap larger than this must be interpreted
as an end of message and you have to parse that message. If the message is
not addressed to you, the other slave will probably reply and will see its
reply in your receive buffer (that's just the nature of RS485 bus). The only
guidance you have to make sense of all that traffic are the 0.3ms gaps that
you will see in the data stream.

When I'm talking about the 3.5 characters gap it doesn't mean you have to
answer immediately when that delay expires. Most masters will accept a
reasonable delay (frequently 1 sec) for a slave to answer. It means only
that you must be prepared to correctly interpret the Modbus traffic, even if
it is generated by communication with a very fast client.

If you need more explanations let me know. Also if you need my code (sorry,
it is for a Modbus master).

Cheers,
Mircea

---In r... ,
> wrote :

Hello, Tom:

Thank you for your response. Actually, I am using serval "costate" with
different loop timing. My current serial port use one of them "costate" with
15 msec loop time. My RS485 port use 115kbps. Do you think how often
checking serial port is reasonable according to your experience?

Regards,

Yang

From: r...
[mailto:r...]
Sent: Wednesday, August 10, 2016 2:01 PM
To: r...
Subject: Re: [rabbit-semi] listening to rs485 serial port on BL4S200

Your program is the only thing running, and the BL4S200 runs at a fixed
speed. A typical program is going to have a big loop where it checks for
things to do. The overhead of checking for available data on a serial port
is negligible in the overall time it takes that loop to run, so it's
probably fine as it is. And in most cases, you're better off processing
serial data at a known point in program execution, instead of interrupting
some other task to process it.

-Tom

On Aug 10, 2016, at 10:28 AM, yang@... [rabbit-semi]
wrote:

I am using BL4S200 for my control project. BL4S200 is a slave device. Other
equipment uses Modbus RS485 to communicate BL4S200. Actually, everything is
working, but the only thing is I need use loop to check serial port weather
I receive any request, is it possible to use some internal interrupt to
detect information received on serial port? so I can remove the fast loop to
save some resource.

The 2024 Embedded Online Conference