View previous topic :: View next topic |
Author |
Message |
jacksan
Joined: 28 Jan 2005 Posts: 95
|
Posted: Fri Apr 18, 2014 4:13 pm Post subject: Serial input is corrupting serial out buffer. |
|
|
This is part of a MODBUS ASCCII routine.
The following code works if a clean MODBUS request is sent.
If the request is immediately followed with some extra "garbage" characters (extra "x"s in this example) then the output buffer(?) is corrupted.
Note: If I wait a "long time" and do the printing elsewhere in the code it works!
Example #1
Sent:
.:010300010001FA(CRLF)
Reply:
.:010302000000F4
.:010302000000F4
.:010302000000F4 '(printed 3 times for DEBUG purposes) All 3 prints are good.
Example #2: one extra "x" sent:
Sent:
.:010300010001FA(CRLF)x
Reply:
. ............2. ' (corrupted output)
.:010302000000F4
.:010302000000F4 '(the second and third printing works fine)
NOTE: The more extra characters that are sent the worse the problem is.
Example #3: many extra "x" sent:
Sent
.:010300010001FA(CRLF)xxxxxxxxxxxxxxxxx
Reply
. ............2.
.u...........SHH... '(the first and second printing fail)
.:010302000000F4 '(the third print still works)
CODE SNIPPET:
Config Serialin = Buffered , Size = 128 '(I also tried using Bytematch = 58 and the exact same problem occurs)
do
If Ischarwaiting() = 1 Then
Modchar = Inkey()
If Modchar = ":" Then
'process the incoming Modbus request...
moddone = 1
end if
if moddone =1 then gosub sendreply
end if
loop
end
sendreply:
'data manipulation ... create Modstring string
Print Modstring
Print Modstring
Print Modstring 'printing 3 times for debug purpose
return
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sat Apr 19, 2014 1:41 pm Post subject: |
|
|
it is not very clear to me what you do.
i assume you make some modbus slave?
do you use buffered output? or just buffered input?
do you use rs485 to switch direction or you have full duplex?
the serial input buffer can not corrupt the output. but you must understand interrupts : when you print data, the process is interrupted when you receive data with an interrupt.
so maybe you need to disable interrupts during printing. _________________ Mark |
|
Back to top |
|
|
jacksan
Joined: 28 Jan 2005 Posts: 95
|
Posted: Mon Apr 21, 2014 8:58 pm Post subject: |
|
|
Hi Mark
Modbus ASCII Slave
Half Duplex RS-485 (hardware based automatic-send-data-control)
Buffered output:
Config Serialout = Buffered , Size = 64
Config Print0 = Portd.2 , Mode = Set 'automatically sets XMIT pin
Config Pind.2 = Output
I agree, it is an interrupt/timing problem.
If I WAIT before doing PRINT it works better, by allowing the incoming character to be processed before I do the PRINT.
I have to WAIT 200us for 1 extra character to be processed.
At 19200 Baud, 1 character is 52us. 200us seems a long time to wait for 1 character to process.(?)
I am not sure what you mean by "receive data with an interrupt"?
I'm using buffers, does that mean I'm using interrupts?
I cannot disable all interrupts since I have critical interrupt driven sensor inputs that cannot be disabled.
Is there a way to only disable Serial input?
Brgds |
|
Back to top |
|
|
techknight
Joined: 21 Apr 2008 Posts: 231
|
Posted: Sat Apr 26, 2014 8:05 pm Post subject: |
|
|
what processor? do you have enough RAM?
Also, what about your power feed to the CPU? is it clean? decoupled? lots of little things need checked before you automatically assume code issue. |
|
Back to top |
|
|
jacksan
Joined: 28 Jan 2005 Posts: 95
|
Posted: Mon Apr 28, 2014 9:05 pm Post subject: |
|
|
Check, check...
It's a code issue ... mine
I wasn't waiting long enough for the incoming data to process before transmitting (half duplex RS-485).
Have to wait much longer than I had thought. |
|
Back to top |
|
|
|