I can’t solve this problem.
I am trying to implement a serial port read/write loop.
/*********************************************/
main
in var byte
num con 1000
dtime var word
resist var word
'check resistance value: if larger than 55000, output 1 via serout
high p14
pause 10
rctime p14,0,dtime
resist=(10000dtime)/(1830.1)
if resist>55000 then
serout s_out,i9600,[dec 1]
serout s_out,i9600," "]
endif
'check whether there is any data sent to serial port
in=0
serin s_in, i9600,5000, main, [dec in]
if in=1 then
high p2
pause num
low p2
serout s_out, i9600, “{Got Touched!}”]
endif
goto main
end
/*******************************************/
The problem is
serin is blocking. Though i set 5000ms at timeout, this code still doesn’t work correctly.
In all cases like this, it would help to have some additional information, like are you doing this with an Atom or with an Atom Pro.
However in both cases, you have to realize that the IO done by the serin and serout command is done without any hardware support (bit bang). That is unless the program is sitting in the Serin command when the actual serial data is sent to it, it will miss it. So timing can be everything here. So you may need to define some protocol for the two sides to properly synchronize.
It can be as simple as knowing that the host will only send out information right after it receives something from you. You may need to play a bit with pauses or sleeps on the other side to make sure that the Atom/Pro has a chance to enter the serin before the result is sent. The delay can not be too long or too short…
It can be also more complex. On my TV Brat Plus, I wanted the ability for the host to tell that brat that it wants the brat to try something at a random time. I did not want my main loop of the Brat to slow down for somthing that happens very rarely. The way that I solved this was knowing on the Atom Pro, both S_IN and S_OUT map to one pin on the underlying processor and this pin is also has the ability to be an interrupt (IRQ0). My Brat code sets up an interrupt handler on IRQ0 and if the host sends a one byte packet of 0xff (hex). This translates in RS232 to one simple pulse, which in my brat interrupt code, sets a flag to say it knows that data is coming. Then the host pauses for enough time to allow the brat to get into a known state where it enters a serin command and is ready for the host to send it… I don’t know if you could do something similiar to this on a normal Atom.
Yep, your program will miss everything if you are not in the serin.
Yes Hserin helps here. However the hardware serial port is not on the same IO pin(s) as S_IN and S_OUT. It is on IO pins P14 and P15. IE it is not connected to the 9 pin RS232 connector but instead to standard IO pins with TTL level signals. If you need RS232 level signals to connect to your host you would need to add in some form of level shifters such as a MAX232 or the like. Somthing like this: sparkfun.com/commerce/product_info.php?products_id=449
Kurt