(with-serial) only works in a loop?


#1

Hi
I notice this behaviour :
My serial port is set to the default usb port on itsibitsy M4
I want to output data on the second serial port using this :

(defun main ()
  (with-serial (str 1 96) (princ "hallo" str)))

this just gives a glitch on the tx line using an oscilloscoop.
changing it to:

(defun main ()
  (with-serial (str 1 96)
    (loop
      (princ "hallo" str))))

prints out correct

I also tried on on an mkrzero , with the same result. Am I doing something wrong or is this normal ?

Thanks Ronny


#2

What you’re doing looks correct.

What are you outputting to? In other words, what’s connected to the Serial 1 line?


#3

Hi David ,
I think I found the clue.
I looks like having to do with time needed to output the characters
This code doesn’t do anything :

(with-serial (str 1 96)
    (princ "Temp " str))
    (delay 10))

-> nothing happens only a glitch on the scope.

This code sends ‘Temp’ to the lcd display connected to serial 1 (also a scoop to see the signal

(with-serial (str 1 96)
    (princ "Temp " str)
    (delay 10)))

Lisp now wait 10msec before closing the stream which is enough to print the 5 characters at 9600Bd
5 characters at 9600Bd takes about 5.2ms
If delay is changed to 5 it still is ok
Changing it to say 3 prints only Te followed by a faulty char.
So it looks like the stream may not be closed before all characters are printed.
That’s why I only see a glitch on the Tx line when no delay is executed.
At least that my honest opinion.
Hope this rings a bell to you.

BTW : so far this has not happened with i2c (with-i2c)

Kind regards Ronny


#4

I assume there shouldn’t be a (delay 10) in the first example?

Yes, I think you’re onto something there. When you exit from the (with-serial …) block uLisp immediately calls Serial1.end(), which is probably a bit premature. Would you like to try adding a delay(100) at the start of the definition of serialend() like this:

void serialend (int address) {
  delay(100);
  ...
}

in the uLisp source. That should fix it. Note that this problem doesn’t occur with Serial 0 because it stays open.


#5

Hi David ,
The delay 10 in the first example was to show that if outside the with-serial loop , nothing happened.
The delay(100) would solve a great deal I think
My question : can’t we just leave the ports open at all time ?

And many thanks for the new itsybitsy M4 file !
Going to install asap !

Kind regards
Ronny Suy


#6

Hi Ronny,

The delay 10 in the first example was to show that if outside the with-serial loop , nothing happened.

Oh yes, I see, good point.

My question : can’t we just leave the ports open at all time ?

Yes, that would be a simpler solution, but it would prevent someone using the TX and RX pins for something else, such as simple I/O, after using them for serial. However, I can’t think of an application where you’d want to do that, so perhaps the best solution is to remove the serialend() function completely.


#7

Hi David

I totally agree with you for removing the serialend function

Regards
Ronny


#8

Hi Ronny,

After reading about Arduino Serial I think the correct solution is to add Serial1.flush() at the start of the definition of serialend() like this:

void serialend (int address) {
  Serial1.flush()
  ...
}

Please try this if you get a chance.

Thanks, David


Serial1 not working on Grand Central M4
#9

Hi David
I was busy with lisp :-) so I tried it directly and it seems to work.
I had the serial.end removed , so I inserted serial flush and the
rest of the code again , and my program keeps running :-) ,
so I think this is a good solution !
I will test it a bit further but it looks ok .
Thanks !!!
Ronny