Help needed for serial comms

oracacle

Senior Member
as some know I have been working on laser trip wires.
The hardware is here and working as expected (thanks erco)
however I am now to a point were I am developing the software and in my efforts to achieve the ability to be able to produce more sensors and connect them without issue, in any order I have come a cropper.
I am just trying to get the first 2 sensors to talk to one another before I start updating the main controller.
with the first sensor the contin is tied high to simulate being attached to the controller. the second sensor output (on the first sensor) is connected to the controller connector of the second sensor.
if I tell the first sensor to send the pin high the second sensor sees that just fine with 4.95v at both pins.
the sensors are connected to each other via a stereo lead.
while using serial command the voltage is -0.3v which seems a little odd.
the sensors do not pass the first serin with qualifier on line 55 just before the debug

the main controller pins (A.0 and A.1) where the sensors are connected are tied low with 10k resistors (testing was done for analogue sensors and this was about the best size, started at 220m) for other sensors and will hopefully be serial comms through those as well.
I am thinking the pins used for serial should be tied low, but am trying to keep the sensors standard (which can make things complex I know) which is the key to universality of the system.
 

Attachments

Goeytex

Senior Member
Serial comms at 9600_8 are not reliable especially when a qualifier is used. Either use 9600_16 or 9600_32. This has been covered here countless times.
Due to processing overhead there is simply not enough time between bytes for the firmware to process the qualifier and then receive the next byte. If for some odd reason you must use 9600_8 then there needs to be time added between sending the qualifier and the next data byte.

Example:

Serout pin,baud, (qualifier)
pause 4
Serout Pin,baud, (databyte)

At 9600_8, reception of more than a few back to back databytes (with no qualifier) can fail. This is one reason why extra time was added between sent data bytes with serout.
 

oracacle

Senior Member
I can change the clock no problem. I am going to use faster clock latter in the programme any how.
I did find that I could get it to pass qualifier if I tied the pin high with 330ohm resistor (it was design without any resistors on the signal lines. I was going to do a trial and error as I ma unsure about things while using the same pin for input, output and bi-directional serial comms.

I will update the clock speed, thanks for that. I must have missed it at some point.
 

oracacle

Senior Member
I have some progress, I have comms working fine in one direction, however sending back in being problematic

Code:
      [color=Blue]let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%010010
      [/color][color=Blue]serin  [PLAIN][[/PLAIN][/color][color=Navy]500[/color][color=Black],last_sens[/color][color=Blue][PLAIN]][/PLAIN][/color][color=Black], sercas, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])[/color]
[color=Black]last_sens_return:
      [/color][color=Green]'debug      
      [/color][color=Blue]if [/color][color=Black]flag [/color][color=DarkCyan]= [/color][color=Navy]2 [/color][color=Blue]then                          [/color][color=Green]'flag 2 indicates last sensor
            [/color][color=Blue]pause [/color][color=Navy]1000
            [/color][color=Blue]serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])
            pause [/color][color=Navy]4
            [/color][color=Blue]serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]#address[/color][color=Blue])     [/color][color=Green]'send address to previous sensor
      [/color][color=Blue]else
            serin [/color][color=Black]sercas, baud,[/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])[/color][color=Black], #number  [/color][color=Green]'receive 
            [/color][color=Blue]debug
      end if[/color]
this code snippet tells the last sensor to send its address to the previous sensor (where it got its address from) while the previous sensor wait for it to arrive.
if I remove the #number from the receiving side, the qualifier is received and the programme moves on, if I add it back on, it appears that the programme gets "stuck" on the serin command. if I remove the qualifier and ask for whatever to be put into the number variable its seem to get stuck again.
I have added a 330ohm resistor between c.2 (sercas) and +v.

this code going in the other direction (and before the above) works fine
Code:
      [color=Blue]debug[/color]
[color=Green]'###Sensor Startup Squence###
      [/color][color=Blue]pause [/color][color=Navy]200                                 [/color][color=Green]'allow controller start up
      [/color][color=Blue]let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%000110
      [/color][color=Blue]setfreq M16
      let [/color][color=Black]qual [/color][color=DarkCyan]= [/color][color=Red]"U"                            [/color][color=Green]'set serin qualifier
      [/color][color=Blue]let [/color][color=Black]address [/color][color=DarkCyan]= [/color][color=Navy]0                           [/color][color=Green]'set defualt address
      [/color][color=Blue]let [/color][color=Black]instruct [/color][color=DarkCyan]= [/color][color=Navy]$01                        [/color][color=Green]'set defualt instruction - and on all sensors
      [/color][color=Blue]if [/color][color=Black]contin [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then                        [/color][color=Green]'check for controller signal
            'wait for address information from other sensors
            'if controller signal is not recieved
            [/color][color=Blue]let [/color][color=Black]flag [/color][color=DarkCyan]= [/color][color=Navy]1                        [/color][color=Green]'set not first sensor flag
            [/color][color=Blue]serin [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])[/color][color=Black],#address
            [/color][color=Green]'debug
            [/color][color=Blue]inc [/color][color=Black]address                         [/color][color=Green]'increment address from previous sensor
                  
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])         [/color][color=Green]'send out next address
            [/color][color=Blue]pause [/color][color=Navy]4
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]#address[/color][color=Blue])     [/color][color=Green]'send out next address
                  
            [/color][color=Blue]let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%010010
            [/color][color=Blue]serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])         [/color][color=Green]'send qualifier to previous to show attatched
            'debug      
      [/color][color=Blue]else                                      [/color][color=Green]'other wise fist sensor, send address to next sensor
            [/color][color=Blue]let [/color][color=Black]flag [/color][color=DarkCyan]= [/color][color=Navy]0                        [/color][color=Green]'set first sensor flag
            [/color][color=Blue]pause [/color][color=Navy]1000
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])
            pause [/color][color=Navy]4
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]#address[/color][color=Blue])
      end if[/color]
symbols:
Code:
symbol laser	= c.1			'pin to switch laser on or off
symbol cascin	= pinc.2		'input from cascaded sensors
symbol sercas	= c.2
symbol cascout	= pinc.2		'output to cascded sensors
symbol sense	= pinc.3		'input from S6986 sensor
symbol contin	= pinc.4		'input from controller
symbol contout	= c.4			'output to controller
symbol sercon	= c.4

symbol baud		= n4800_16

symbol flag		= b0			'flag variable
symbol qual		= b1			'qualifier varaible
symbol address	= b2			'address variable
symbol instruct	= b3			'instruction variable
symbol number	= b4			'number of sensors (address + 1)
I am struggling to understand why it works in in direction but not the other.
 

hippy

Technical Support
Staff member
Perhaps it's that receiving a #var requires the sender to put a non-digit character after the #var value it sends.

It would be best to strip the sender and receiver down to its bare bones and absolute minimum, get that working first at default operating speeds. The code you have is too complicated to analyse without having to study it.
 

oracacle

Senior Member
If I read that correctly I would send something like
Code:
            pause 1000
            serout sercon, baud, (qual)
            pause 4
            serout sercon, baud, (#address, "a")     'send address to previous sensor
or would just sending a high/low be good enough.
as a side note, both are senders and receivers. the first one sends info out, the second (end more in the future) receives just fine. the second one than uses the same line to send information back, some of the code complexity is to ensure that not more than one chip is talking at the same time.
its seems a little odd that everything works fine in one direction but not that other, despite the code be comparably identical in its protocols.
 

oracacle

Senior Member
well i seem to have solved the problem. if the information is sent and recieved in either direction just var and not #var all seems to be fine. well at least unntil i start interfacing with the controller.

thanks for the help so far, i maybe back.
 

hippy

Technical Support
Staff member
Untested but this should cycle an ever increasing number around between master and slave ...

Code:
Master:
  Do
    Pause 1000
    SerOut TX, BAUD, ( QUALIFIER ) : Pause 4 : SerOut TX, BAUD, ( #w0, "X" )
    SerIn RX, BAUD, ( QUALIFIER ), #w0
    SerTxd( #w0, CR, LF )
  Loop
Code:
Slave:
  Do
    SerIn RX, BAUD, ( QUALIFIER ), #w0
    SerTxd( #w0, CR, LF )
    w0 = w0 + 1
    SerOut TX, BAUD, ( QUALIFIER ) : Pause 4 : SerOut TX, BAUD, ( #w0, "X" )
  Loop
 

oracacle

Senior Member
I will give its a go later. I will try each piece on each sensor as both are TX and RX.

I have the sensors communicating correctly with each other, but there are power issues when I moved to developing the updates for the controller. the batteries are a little low (sensors where developed with a wall wart type PSU), but a single sensor and I can get a qualifier pass (but not reading variable for some reason), but with 2 sensors no joy. the voltage drop post regulator (about 0.1v drop), but not pre regulator. Its looking like I am going have to step up the development programme

I think it maybe helpful if I try and give a description of what the sensor code does.
The controller must send the contin line high so that the sensor know that a controller is connected - the sensor defaults if it unconnected.

if the contin line is low, it assumes it the second or greater sensor in the chain, and sets the flag to show that. Then listens for an address from the previous sensor, once received it sends an address on, and a qualifier to show present to the previous sensor
if the contin line is high, it is the first sensor in the chain, and sets the flag to 0 and sends an address to the next sensor
these 2 action are under a single if statement at the start of the programme

the programme then listens for the qualifier to come back from the next sensor, if nothing is heard it times out and assume its the last in the chain and sets the flag to 2

if the flag is 2 (the last sensor in the chain) its address is sent to the previous sensor,
the previous sensor is waiting for the signal, when its received it send it on until the first sensor receives it, where it will be held until the controller request the information.
this is done in the second if statement

as a result each sensor is identical until the addresses are established which means there is no order the attach them, they self address and could in theory make a chain of 256 sensors. this makes the code look more complex than it is as it contains the code for both "master" and "slave". In this system that is no true master and slave, just simply a chain that information is passed along and modified were needed. Due to the fact that there is three line. 2 for power and ground, and one for data means that information has to be passed in both directions meaning one has to ensure that each sensor sets its pins correctly at the any given time.

Code:
[color=Black]init:
      [/color][color=Green]'debug
'###Sensor Startup Squence###
      [/color][color=Blue]pause [/color][color=Navy]200                                 [/color][color=Green]'allow controller start up
      [/color][color=Blue]let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%000110
      [/color][color=Blue]setfreq M16
      let [/color][color=Black]qual [/color][color=DarkCyan]= [/color][color=Red]"U"                            [/color][color=Green]'set serin qualifier
      [/color][color=Blue]let [/color][color=Black]address [/color][color=DarkCyan]= [/color][color=Navy]0                           [/color][color=Green]'set defualt address
      [/color][color=Blue]let [/color][color=Black]instruct [/color][color=DarkCyan]= [/color][color=Navy]$01                        [/color][color=Green]'set defualt instruction - and on all sensors
      [/color][color=Blue]if [/color][color=Black]contin [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then                        [/color][color=Green]'check for controller signal
            'wait for address information from other sensors
            'if controller signal is not recieved
            [/color][color=Blue]let [/color][color=Black]flag [/color][color=DarkCyan]= [/color][color=Navy]1                        [/color][color=Green]'set not first sensor flag
            [/color][color=Blue]serin [PLAIN][[/PLAIN][/color][color=Navy]2000[/color][color=Black],start_laser[/color][color=Blue][PLAIN]][/PLAIN][/color][color=Black], sercon, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])[/color][color=Black],address
                                                [/color][color=Green]'if no controller detected, presume standalone sensor and skip ahead
            [/color][color=Blue]inc [/color][color=Black]address                         [/color][color=Green]'increment address from previous sensor
                  
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])         [/color][color=Green]'send out next address
            [/color][color=Blue]pause [/color][color=Navy]4
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]address[/color][color=Blue])      [/color][color=Green]'send out next address
                  
            [/color][color=Blue]let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%010010
            [/color][color=Blue]serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])         [/color][color=Green]'send qualifier to previous to show attatched
            'debug      
      [/color][color=Blue]else                                      [/color][color=Green]'other wise fist sensor, send address to next sensor
            [/color][color=Blue]let [/color][color=Black]flag [/color][color=DarkCyan]= [/color][color=Navy]0                        [/color][color=Green]'set first sensor flag
            [/color][color=Blue]pause [/color][color=Navy]1000
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])
            pause [/color][color=Navy]4
            [/color][color=Blue]serout [/color][color=Black]sercas, baud, [/color][color=Blue]([/color][color=Black]address[/color][color=Blue])
      end if

      let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%010010
      [/color][color=Blue]serin  [PLAIN][[/PLAIN][/color][color=Navy]500[/color][color=Black],last_sens[/color][color=Blue][PLAIN]][/PLAIN][/color][color=Black], sercas, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])[/color]
[color=Black]last_sens_return:
      [/color][color=Green]'debug      
      [/color][color=Blue]if [/color][color=Black]flag [/color][color=DarkCyan]= [/color][color=Navy]2 [/color][color=Blue]then                          [/color][color=Green]'flag 2 indicates last sensor
            [/color][color=Blue]pause [/color][color=Navy]1000
            [/color][color=Blue]serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])
            pause [/color][color=Navy]4
            [/color][color=Blue]serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]address[/color][color=Blue])      [/color][color=Green]'send address to previous sensor
            [/color][color=Blue]low [/color][color=Black]cascout
      [/color][color=Blue]else
            serin [/color][color=Black]sercas, baud,[/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])[/color][color=Black], number   [/color][color=Green]'receive
            [/color][color=Blue]if [/color][color=Black]flag [/color][color=DarkCyan]> [/color][color=Navy]0 [/color][color=Blue]then
                  serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]qual[/color][color=Blue])
                  pause [/color][color=Navy]4
                  [/color][color=Blue]serout [/color][color=Black]sercon, baud, [/color][color=Blue]([/color][color=Black]number[/color][color=Blue])
            end if
            [/color][color=Green]'debug
      [/color][color=Blue]end if[/color]
this works for comms between 2 sensor, I haven't tried more as yet.
 
Top