Setting parameters using serial input

Hi all,

I’m experimenting with the serial port of my botboard using the ATOM 28 pro.
I want to be able to update some variables in the ATOM using my PC. I have my botboard connected to the PC using a standard serial cable. Sending data from the ATOM to the PC works fine and also receiving one byte from the PC to the ATOM isn’t a problem. But what I really want to do is sending something like “X100 Y25” to the ATOM.

I wrote something like this:

'Inputs
butA var bit
butB var bit
butC var bit

prev_butA var bit
prev_butB var bit
prev_butC var bit

BUTTON_DOWN con 0
BUTTON_UP con 1

'Variables
ReceiveBuf var byte
Parameter var byte

X var byte
Y var byte

X=0
Y=0
'---------------------------------------------------
main:

'Check Parameters X and Y
'Button A
IF (butA = BUTTON_DOWN) AND (prev_butA = BUTTON_UP) then
	serout S_OUT, i19200, "X=",dec X," Y=",dec Y,13]
ENDIF

'Receive data
serin S_IN, i19200, 500, timeout, [ReceiveBuf] 
sound p9, [50\2250]	'Data received

'Check parameter
IF(ReceiveBuf="x" | ReceiveBuf="X") THEN
	Parameter="x"
ENDIF

IF(ReceiveBuf="y" | ReceiveBuf="Y") THEN
	Parameter="y"
ENDIF

'Change value of the selected parameter
IF(ReceiveBuf>="0" & ReceiveBuf<="9") THEN
	IF(Parameter="x") THEN
		X=1
	ENDIF
	
	IF(Parameter="y") THEN
		Y=2
	ENDIF
ENDIF

timeout 'No input

gosub ReadButtons

goto main
'---------------------------------------------------
ReadButtons:
'Sub for reading the status of the buttons
input P4
input P5
input P6

prev_butA = butA
prev_butB = butB
prev_butC = butC

butA = IN4
butB = IN5
butC = IN6

return
'---------------------------------------------------

And of course…
this doesn’t work :confused:

I found some examples but they all wait for input. What I need is a sub routine that checks for input every loop and continues running even without new input. Is this possible or do I need some kind of hardware buffer for this?

Can somebody tell me what I’m doing wrong??
Or where I can find some example.

Thanks

Xan

You want to write a subroutine that checks if there is a character, and then puts it in a buffer. When a CR is received (end of line), then parse the buffer for the ‘X’ or ‘Y’ and ‘nn’ parameters. (hint: convert the string to upper case first, and remove spaces).

Get the X or Y, and then handle the value of the variable appropriately.

HTH

Alan KM6VV

Hi all,

Finally I found some time to test the serial input some more. At this moment I’m able to send data to the Atom and when the values will change when I hit enter. See my code:

'Inputs
butA var bit
butB var bit
butC var bit

prev_butA var bit
prev_butB var bit
prev_butC var bit

BUTTON_DOWN con 0
BUTTON_UP con 1

ReceiveBuf var word
Buffer var word(128)
BufferIndex var byte
i var byte
StartIndex var byte

Parameter var byte

X var byte
Y var byte

BufferIndex = 0

X=12
Y=56

serout S_OUT, i19200, "X = ", DEC X, " Y = ",DEC Y,13]

'---------------------------------------------------
main:

'Button A
IF (butA = BUTTON_DOWN) AND (prev_butA = BUTTON_UP) then
	serout S_OUT, i19200, "X = ", DEC X, " Y = ",DEC Y,13]
ENDIF

	
gosub ReadSerial
gosub ReadButtons

goto main
'---------------------------------------------------
ReadSerial:

ReceiveBuf = 0
serin S_IN, i19200, [ReceiveBuf] 

IF (ReceiveBuf = 27)THEN 'ESC
	BufferIndex = 0
ENDIF
	
IF (ReceiveBuf <> 13) THEN 'CR
	IF (ReceiveBuf <> 32) THEN 'space
		Buffer(BufferIndex) = ReceiveBuf
		BufferIndex = BufferIndex + 1
	ENDIF
ELSE
	'process buffers to params
	FOR i = 0 TO (BufferIndex-1)
		IF ((Buffer(i) = "x") OR (Buffer(i) = "X")) THEN
			Parameter = "X"
			X = 0
		ENDIF
		
		IF ((Buffer(i) = "y") OR (Buffer(i) = "Y")) THEN
			Parameter = "Y"
			Y = 0
		ENDIF
		
		IF ((Buffer(i) >= "0") AND (Buffer(i) <= "9")) THEN 'decimal -> "0" and "9"
			IF (Parameter = "X") THEN
				X = X * 10
				X = X + (Buffer(i)-48 )
			ENDIF
			IF (Parameter = "Y") THEN
				Y = Y * 10
				Y = Y + (Buffer(i)-48 )
			ENDIF							
		ENDIF					
	NEXT
	
	'Echo settings
	serout S_OUT, i19200, "X = ", DEC X, " Y = ",DEC Y,13]
	BufferIndex = 0
ENDIF	

return
'---------------------------------------------------
ReadButtons:
'Subroutine voor het uitlezen van de knoppen
input P4
input P5
input P6

prev_butA = butA
prev_butB = butB
prev_butC = butC

butA = IN4
butB = IN5
butC = IN6

return
'---------------------------------------------------

But still my problem is that the total program is waiting for input! I’ll tried to change
serin S_IN, i19200, [ReceiveBuf]
to
serin S_IN, i19200,500,Timeout, [ReceiveBuf]
and added a label timeout at the end of the ReadSerial sub.

If I use the timeout the main program does not wait for input which is just what I need. But when I check the received byte by sending it back most of the time I get a different byte!

serin S_IN, i19200,500,Timeout, [ReceiveBuf] 
serout S_OUT, i19200,[ReceiveBuf]
Timeout

So when I send a char like ‘a’ 10 times. It will return just 2 times as a ‘a’. Most of the times it will return Unicode chars like ‘ö’ or ‘ú’.

I’ve read that there where some issues with the timeout function of serin with version 8.0.1.3. I’m currently using version 8.0.1.6. which should be a good working version :wink:

Does somebody knows what the problem is? A’m i doing something wrong or is there still a problem in the compiler? :unamused:

Thanks!

Xan

Sorry, I don’t have much time to look at this right now, but the first thing I would try is to change the variable ReceiveBuf to a byte and see if that helps.

From a quick glance at the rest of your thread it looks like you wish to receive multiple bytes of information. like X and Y coordinates. You can do this like Alan suggested. Alternatively you could try to define some fixed format, like: the first byte is a command, the next two bytes (in binary) is the X coordinate and the next two bytes is the Y coordinate. Then you can have your program waiting for a fixed size message.

Also some times you run into issues that when you output stuff on your PC that it is stuck in the output buffer either waiting for a CR or you need to do some type of flush operation.

Good Luck