Had some odd experiences trying to do hand-coded serial comms in KRL over the past week. Thought I'd share.
Had to connect a Keyence camera to my KRC2 via RS232. Got the channel working easily enough, but eventually found that I had to append a Carraige Return (ASCII code 13) to my output string before I gave that string to my CWRITE command, as per these threads that I dug up:
http://74.125.95.132/search?q=cache…lient=firefox-a
and
http://www.roboterforum.de/roboter-forum/…t-t2797.15.html
But there was a snag. The code described as working in the second link... didn't, for me. Any attempt to SWRITE the value of a Carriage Return into the string using a %c conversion code failed with a #FMT_ERR. This is odd, b/c it's been described as working for other people, and judging from the manual, the %c conversion code should do exactly that.
Trying UNIX-Style "\n|" or "\r" inside the format string didn't work, either.
Eventually, someone suggested doing this: STRING[first_clear_position] = 13
It sounded crazy to me, since string variables (char arrays, really) in KRL are always fussy and hard to work with, but what the heck, I gave it a shot...
And it worked. It would appear that assigning a single element of a CHAR[] array the value of an INT variable will put the ASCII equivalent of that INT value into the CHAR[] array.
One other thing I found interesting: I had the String variable logged in the .dat file, so I was able to look at it later. It's appearance in the .dat file was STRING] = "T1'H0D'"
Note the single quotes inside the double quotes. 'Hxx' is the standard KRL notation for Hexadecimal values, and 'H0D' is the hex equivalent of 13, so my guess is that KRL uses this technique to store non-alphanumeric ASCII characters (like Carriage Return, Line Feed, etc) inside string variables.
While my code now works, I'm interested in seeing if anyone wants to chime in with their own experiences and/or tricks for handling string variables, ASCII values, and serial comms. It's probably a rich topic for discussion, and I suspect I'll be doing more of this stuff before long.