Basic Stamp serial connection Raspberry Pi
Hi,
I'm trying to connect a BS2 on a Board of Education Develompemnt Board - USB with a Raspberry Pi running Raspbian through serial connection via USB.
First I program the BS2 with an Windows PC, when I got it programmed unplug from PC and plug in the Raspberry Pi, then run the Python program.
The code I'm working in BS is attached:
and the python code:
I use dmesg to see in which ttyUSB is connected the BS since it changes everytime I plug and unplug the BS to the Raspberry.
When I exeute the Python code the led blinks, so I suposse the BS restarts and so the connection is made, but it's only a supposition, I'm just a begginer both BS and Raspberry Pi.
Although the led blinks it do not respond to the '1' and '2' inputs in the Raspberry Pi and I really don't know why. What am I doing wrong?
Best regards,
Mois
I'm trying to connect a BS2 on a Board of Education Develompemnt Board - USB with a Raspberry Pi running Raspbian through serial connection via USB.
First I program the BS2 with an Windows PC, when I got it programmed unplug from PC and plug in the Raspberry Pi, then run the Python program.
The code I'm working in BS is attached:
' {$STAMP BS2}
' {$PBASIC 2.5}
Baud CON 84
SerialPin CON 16
state VAR Byte
HIGH 9
PAUSE 200
LOW 9
Main:
SERIN SerialPin, BAUD, [state]
IF state = 1 THEN
HIGH 9
ELSEIF state = 2 THEN
LOW 9
ENDIF
PAUSE 1000
GOTO Main
and the python code:
import serial
ser=serial.Serial(port='/dev/ttyUSB2', baudrate=9600, bytesize=8, parity=''N', stopbits=1, timeout=None)
print("Starting!")
while True:
val=raw_input('Value: ')
ser.write(val)
if val == '1':
print('LED ON')
elif val =='2':
print('LED OFF')
elif val=='exit':
ser.close()
exit()
ser.close()
I use dmesg to see in which ttyUSB is connected the BS since it changes everytime I plug and unplug the BS to the Raspberry.
When I exeute the Python code the led blinks, so I suposse the BS restarts and so the connection is made, but it's only a supposition, I'm just a begginer both BS and Raspberry Pi.
Although the led blinks it do not respond to the '1' and '2' inputs in the Raspberry Pi and I really don't know why. What am I doing wrong?
Best regards,
Mois
Comments
Hello!
What was the problem and how did you solve it?
The problem as you've described it isn't solved since you didn't change the settings from "unsolved" to "solved". So what was the solution?
import serial ser=serial.Serial(port='/dev/ttyUSB2', baudrate=9600, bytesize=8, parity=''N', stopbits=1, timeout=None) print("Starting!") while True: val=raw_input('Value: ') if val == '1': print('LED ON') ser.write('1/n') elif val =='2': print('LED OFF') ser.write('2/n') elif val=='exit': ser.close() exit() ser.close()
Best regards,
Mois
ser=serial.Serial(port='/dev/ttyUSB2', baudrate=9600, bytesize=8, parity=''N', stopbits=1, timeout=None)
and it should be:
ser=serial.Serial(port='/dev/ttyUSB2', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None)
You have to know which port is the BS attached to the RPi, the first time you plug it usually would be '/dev/ttyUSB0' but if you unplug it and plug it again it may have changed so you can type dmesg in the command line to see in which ttyUSB is connected the BS and edit that parameter.
Although isn't the main problem it's true that in the write lines should be "1\n" and "2\n".
So the final Python code is:
import serial ser=serial.Serial(port='/dev/ttyUSB0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None) print("Starting!") while True: val=raw_input('Value: ') if val == '1': print('LED ON') ser.write('1\n') elif val =='2': print('LED OFF') ser.write('2\n') elif val=='exit': ser.close() exit() ser.close()
Finally I've seen you have changed the SerialPin from 16 to 15, I've used 16 because if Rpin is set to 16, the BASIC Stamp uses the dedicated serial-input pin (SIN, physical pin 2), which is normally used by the Stamp Editor during the download process.
I let you the code you have on the other post with little modifications, I think this should work:
'{$STAMP BS2} '{$PBASIC 2.5} ' I/O Declaration (Added) LedPin PIN 15 ' Added SerialPin PIN 16 ' Changed from CON to PIN and changed from 15 to 16 ' Constant Declaration (Added) Baud CON 84 ' 9600 Baud, 8-Bit, No-Parity, True (non-inverted) LoopDelay CON 1000 ' Added ' Variable Declaration (Added) state VAR Byte HIGH LedPin ' Changed from 9 to LedPin PAUSE 200 LOW LedPin ' Changed from 9 to LedPin DO ' Changed from Main: to DO SERIN SerialPin, Baud, [DEC1 state] ' Added formatter DEC1 IF state = 1 THEN HIGH LedPin ' Changed from 9 to LedPin ELSEIF state = 2 THEN LOW LedPin ' Changed from 9 to LedPin ENDIF PAUSE LoopDelay LOOP ' Changed from Goto Main to LOOP
Unfortunately I no longer have the BS so I can't try it by myself.
Here you have a little video I made when I was starting this project: http://youtu.be/4zM54iNZajs
Best regards,
Mois
Congratulation on the project! Looks great.