Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp serial connection Raspberry Pi — Parallax Forums

Basic Stamp serial connection Raspberry Pi

MoiMoi Posts: 5
edited 2015-01-19 01:12 in BASIC Stamp
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:
' {$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

  • MoiMoi Posts: 5
    edited 2014-02-25 17:03
    Problem solved
  • Buck RogersBuck Rogers Posts: 2,178
    edited 2014-02-25 17:24
    Moi wrote: »
    Problem solved

    Hello!
    What was the problem and how did you solve it?
  • Buck RogersBuck Rogers Posts: 2,178
    edited 2014-03-06 21:10
    Moi wrote: »
    Problem solved

    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?
  • MoiMoi Posts: 5
    edited 2014-03-07 07:45
    The first problem was that the command SERIN monitors the incoming serial data until is find a non-decimal numeric character. The second problem was that I didn't know what was the Python program sending. Both problems are solved changing the Python code by that other:
    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
  • satriaoosatriaoo Posts: 20
    edited 2015-01-18 22:38
    Moi wrote: »
    The first problem was that the command SERIN monitors the incoming serial data until is find a non-decimal numeric character. The second problem was that I didn't know what was the Python program sending. Both problems are solved changing the Python code by that other:
    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
  • MoiMoi Posts: 5
    edited 2015-01-19 01:12
    I've been reading your post and they are right, there is an extra quote on parity
    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
  • And the video when I finished the project:
  • PublisonPublison Posts: 12,366
    edited 2016-09-02 22:36
    It's been a while! :)
    Congratulation on the project! Looks great.

Sign In or Register to comment.