Basic Stamp 2, SERIN, Non-Numeric Characters and C++
SB316
Posts: 33
Hi all,
I've been experimenting with serial communication between a Basic Stamp 2 and my PC (running Ubuntu) using C++. I have been using a library (I suppose that's what you would call it) that works with Unix based systems called Termios I've been able to set up a simple test program in which the BS2 waits for a number to be fed to it (via the SERIN command) then blinks an LED that number of times. For example, you send the number 3, it blinks the LED 3 times. My C++ program simply sets some things up using Termios (the baud rate and stuff like that), gets the number to send (simple cin command), then creates an 'ofstream' variable and writes (using the << operator) to the device file '/dev/ttyS0' through the stream. It works fine if the number in question is less than 10. Once you get into double digits the BS2 only works with the first digit, i.e. the number 10 makes the light blink once. I tried using the DEC operator in my SERIN command so that the BS2 would get the actual numeric value of the number sent (before then I was converting from the ASCII value by subtracting 48 from the number I got). I read in the help manual in the Basic Stamp editor software that when you use the DEC operator the SERIN command waits for a non-numeric character to come through, telling it that the sender has finished sending. So, here's the problem: I've tried sending a whole bunch of non-numeric characters from my C++ program. 'a', '\n', '\0', etc. My BS2 just will not blink the light anymore! If I revert back to the original program, the one with the single digit numbers, it works fine. But not when I try to get it to work with double digits.
So, I hope I haven't bored you with my extremely long post. Can anybody help?
P.S. I will be posting another thread about another question I have regarding serial I/O between my BS2 and my computer. I'll post a link to this thread when it's up. Keep your eyes peeled.
I've been experimenting with serial communication between a Basic Stamp 2 and my PC (running Ubuntu) using C++. I have been using a library (I suppose that's what you would call it) that works with Unix based systems called Termios I've been able to set up a simple test program in which the BS2 waits for a number to be fed to it (via the SERIN command) then blinks an LED that number of times. For example, you send the number 3, it blinks the LED 3 times. My C++ program simply sets some things up using Termios (the baud rate and stuff like that), gets the number to send (simple cin command), then creates an 'ofstream' variable and writes (using the << operator) to the device file '/dev/ttyS0' through the stream. It works fine if the number in question is less than 10. Once you get into double digits the BS2 only works with the first digit, i.e. the number 10 makes the light blink once. I tried using the DEC operator in my SERIN command so that the BS2 would get the actual numeric value of the number sent (before then I was converting from the ASCII value by subtracting 48 from the number I got). I read in the help manual in the Basic Stamp editor software that when you use the DEC operator the SERIN command waits for a non-numeric character to come through, telling it that the sender has finished sending. So, here's the problem: I've tried sending a whole bunch of non-numeric characters from my C++ program. 'a', '\n', '\0', etc. My BS2 just will not blink the light anymore! If I revert back to the original program, the one with the single digit numbers, it works fine. But not when I try to get it to work with double digits.
So, I hope I haven't bored you with my extremely long post. Can anybody help?
P.S. I will be posting another thread about another question I have regarding serial I/O between my BS2 and my computer. I'll post a link to this thread when it's up. Keep your eyes peeled.
Comments
SERIN's InputData can be structured as a sophisticated list of actions to perform on the incoming data. This allows you to process incoming data in powerful ways. For example, suppose you have a serial stream that contains "pos: xxxx yyyy" (where xxxx and yyyy are 4-digit numbers) and you want to capture just the decimal y value. The following code would do the trick:
yCoord VAR Word ' y coordinate
Main:
SERIN 1, 16468, [WAIT ("pos: "), SKIP 4, DEC yCoord]
DEBUG ? yCoord
END
The items of the InputData list work together to locate the label "pos: ", skip over the four-byte x data, then convert and capture the decimal y data. This sequence assumes that the x data is always four digits long; if its length varies, the following code would be more appropriate:
yCoord VAR Word
Main:
SERIN 1, 16468, [WAIT ("pos: "), DEC yCoord, DEC yCoord]
DEBUG ? yOffset
END
The unwanted x data is stored in yCoord then replaced by the desired y data. This is a sneaky way to filter out a number of any size without using an extra variable. With a little creativity, you can combine the InputData modifiers to filter and extract almost any data.
Checkout ASCII encoding.
Using the WAIT instruction as suggested is the way to go when you finally get your protocol figured out, it will make your transmissions much more reliable.
Jeff T.
Post your source code.
Jeff T.