Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp: turn led from application in C # — Parallax Forums

Basic Stamp: turn led from application in C #

spro80spro80 Posts: 7
edited 2014-05-08 16:58 in BASIC Stamp
Hello.

I am developing an application in c # and basic stamp.
I need to send from C # is a number from 1 to 2, basic stamp must retrieve this number and see

CODE C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace MicroLedPrueba1
{
public partial class Form1 : Form
{
SerialPort puertoSerie = new SerialPort();

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

puertoSerie.PortName = "COM5";
puertoSerie.BaudRate = 9600;
puertoSerie.DataBits = 8;
puertoSerie.ReadTimeout = 5000;
puertoSerie.Open();
label1.Text = "Abierto y configurado";
}

private void button2_Click(object sender, EventArgs e)
{
puertoSerie.Close();
label1.Text = "Cerrado";
}

private void button3_Click(object sender, EventArgs e)
{
puertoSerie.Write("1");
}


}
}


The code en C# is OK, because when I press the button 1 , the connection is OK
- After I press the button 3 write in C# application (puertoSerie.Write("1");) I never can see some in basic stamp.



As I can prove that I answer some basic stamp to see if the data sent was received in basic stamp to continue

My code in Basic stamp is the follow:

' {$STAMP BS2p}
' {$PBASIC 2.5}

INPUT 2

'Para recibir por el PIN 2, del BS2 a 9600 bps, 8N, invertido:
Dato_Recivido VAR Byte

SERIN 2, 16468,[Dato_Recivido]

LOW 1

Comments

  • GenetixGenetix Posts: 1,752
    edited 2014-04-24 13:48
    It's difficult to determine your problem without a schematic.
    One thing I notice in both programs is there is no looping main routine so each program will only run once.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-04-25 09:31
    It also matters how (what format) you're sending the data. Are these binary values or ASCII strings?
  • spro80spro80 Posts: 7
    edited 2014-04-25 10:11
    In c # I have a button to connect to the COM port 5.
    Once connected I press another button to send that does the following:

    puertoSerie.Write ("1");

    -> This data still not recuperare on basic stamp, I first want to know that you can call a routine implemented in basic stamp


    All we need to know is to call a routine in Basic Stamp, to see that when you press the send button, you can call with any microcontroller basic routine.

    example:

    low 1
    high 1
    low 2
    high 2


    I would like to verify whether basic stamp is listening when I press the submit button
  • spro80spro80 Posts: 7
    edited 2014-04-25 12:48
    Chris.

    The connection is the follow:


    PC (Application C#)
    BASIC STAMP (This micro have three LED 1, LED 2, LED 3)
    DB9

    The protocolo of Basic Stamp is Serial SPI

    The application in C# open the port COM 5 with te follow instructions:

    puertoSerie.PortName = "COM5";
    puertoSerie.BaudRate = 9600;
    puertoSerie.DataBits = 8;
    puertoSerie.ReadTimeout = 5000;
    puertoSerie.Open();

    This connect is OK, because I can see the message (COM 5 is open)

    After I press the button submit whit de follow code: puertoSerie.Write ("1"); This value is ASCII.

    The connection between PC y Basic Stamp is with cable DB9.


    I Don't know How I can execute any instruction in BASIC STAMP, when I press the button submit in C#.
  • FranklinFranklin Posts: 4,747
    edited 2014-04-25 15:54
    Try working with the serial terminal in the BasicStamp editor and send a 1 to the stamp to see what happens. Don't worry about your c# until you can communicate with the editor.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-04-25 16:25
    In your original code you're just receiving the data and making a line low. You didn't do any evaluation on the received value, such as:

    IF Dato_Recivido = "1" THEN LOW 1

    If you need more commands for each you can structure the command with an ENDIF.
  • spro80spro80 Posts: 7
    edited 2014-04-28 08:14
    Thank you Chris.

    I understand that you said me , in this moment I don't want to recover the value.

    I want turn on led in basic stamp, I just want to see that basic stamp turn on led when I press the button submit from an application extern (c#).


    C# application
    Boton Submit ( Do call to routine in Basic Stamp)


    Routine in Basic Stamp.

    Low 1 (If I can see that the led 1 in basic stamp turn on, when I press the button submit. I could continue with the next step. )
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-04-28 09:01
    In your current code, seeing the LED turn on only means the SERIN command got a byte from the port. You should DEBUG that value to see what is being received.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2014-04-29 06:49
    Hi spro80,

    Do you have a BS2p Stamp module, the BS2p value for 9600 baud is NOT 16468. I say this because you specify the BS2p in your code.
    '{STAMP BS2p}
    

    There are two values for 8 bit no parity, the value you choose depends on how the serial connection is wired. The following assumes a BS2p

    If you are using the programming cable to communicate the serial signal will be inverted and the SERIN port is 16. The value for the inverted signal is 16624.

    Example code for BS2p inverted serial communication
    Dato_Recivido VAR Byte
    
    SERIN 16, 16624,[Dato_Recivido]
    
    LOW 1
    

    If you are connected from PC to Stamp without modifying the serial signal's polarity then the signal is true (non inverted) and the value for 9600 baud is 240

    Example code for BS2p true serial communication
    Dato_Recivido VAR Byte
    
    SERIN 2, 240,[Dato_Recivido]
    
    LOW 1
    
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-04-29 10:37
    Hi spro80, Do you have a BS2p Stamp module, the BS2p value for 9600 baud is NOT 16468. I say this because you specify the BS2p in your code.

    Good eye, I missed that. :thumb:
  • spro80spro80 Posts: 7
    edited 2014-04-29 13:56
    Hello.

    In this moment I am seeing the led turn on in the basic stamp.

    When I execute the application in C#, I press the button submit whit the follow instruction: puertoSerie.Write("1");

    I HAVE TWO QUESTION:

    1.- In C# APPLICATION
    - If I want to send the value 1, I have that send 1 or 49? I don't know if I have that to send the value in decimal or ASCII from my application.


    2.- IN BASIC STAMP

    How I can recuperate the value 1 or (45 en ASCII) in basic stamp and turn on the LED 1.

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}

    Dato_Recibido VAR Byte (2)

    SERIN 16, 9600, [Dato_Recibido]



    'I understand the logic, I have the follow code but not is working, if I Remove -> IF(Dato_Recivido = 1) THEN, the LED 7 Turn on

    IF(Dato_Recivido = 1) THEN ' Never enter in IF.
    LOW 7
    ENDIF





    T
  • Mike GreenMike Green Posts: 23,101
    edited 2014-04-30 07:50
    If you send 1 from C#, that's what you'll get in the Stamp variable. If you send "1" from C# (or 49), you'll get 49 in the Stamp variable. If you want the Stamp to interpret the value as ASCII, use

    SERIN 16, 240, [DEC1 Data_Recibido]

    Remember that the Baud constant (the 240) is not the Baud itself. It's a control value for the number of data bits, parity, and Baud. It depends on the model of the Stamp as well. Look in the Reference Manual chapter on the SERIN or SEROUT statements for tables giving the proper value.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2014-04-30 07:55
    Hi,
    puertoSerie.Write("1");
    

    The above will transmit a binary value of 49
    puertoSerie.WriteLine("1");
    

    The above will transmit a binary value of 49 and then append a newline character ( 10 decimal is the default )

    the second option might be better for what you are trying to do.

    At the Stamp end try using the "DEC" formatter and pay special attention to the baud rate value.
    '{$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    Dato_Recibido VAR Byte 
    
    SERIN 16, 16624, [DEC1 Dato_Recibido]
    
    
    IF(Dato_Recivido = 1) THEN 
    LOW 7
    ENDIF
    

    Alternatively you can enclose the value to compare in quotes
    '{$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    Dato_Recibido VAR Byte 
    
    SERIN 16, 16624, [Dato_Recibido]
    
    
    IF(Dato_Recivido = "1") THEN 
    LOW 7
    ENDIF
    

    Lastly this should also work
    '{$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    Dato_Recibido VAR Byte 
    
    SERIN 16, 16624, [Dato_Recibido]
    
    
    IF(Dato_Recivido = 49) THEN 
    LOW 7
    ENDIF
    


    Something you may not be aware of is that when you send data from PC to Stamp then the Stamp will echo the characters back to the PC.

    These characters will sit in the UARTs buffer and get mixed in with your next transmission unless you get rid of them.

    For now a quick way to overcome this is to read the whole of the buffer before each transmission, reading the buffer has the effect of emptying it.
    puertoSerie.ReadExisting;
    puertoSerie.Write("1");
    

    EDIT this is basically what Mike Green is saying, with regard to baud the value is going to be 16624 or 240 depending on your hardwired setup
  • spro80spro80 Posts: 7
    edited 2014-05-07 10:25
    Thanks.

    In this moment the apllication in C# is working. When I send an 1 or 2 or 3 or........9 the led in basic stamp turn on.

    But I have problems when I send 12, the led in basic stamp is nor working, I believe that is by the type of date in byte, what other type of date I can use when I send a number with two digits from c#

    ///////////////////////////
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    '
    [ I/O Definiciones ]
    Dato_Recibido VAR Byte
    MAIN:
    SERIN 16, 16624, [DEC1 Dato_Recibido]


    IF (Dato_Recibido = 1) THEN 'OK the LED turn on, when I send 1 from C#
    GOTO LED1
    ELSEIF(Dato_Recibido = 12) THEN 'This part of code not working, when i send 12 from C#
    GOTO LED12


    '
    [ Sub-rutinas ]
    LED1:
    LOW 1
    GOTO MAIN

    LED12:
    LOW 12
    GOTO MAIN
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-05-07 10:51
    I would recommend writing a short program that just receives a byte and displays the value of it on the DEBUG screen. Then you can send the values over from your PC application and see visually what is being received. If the "12" you're sending from the PC application is being sent as two characters, then it should be detected as a "1" rather than a "12".
  • GenetixGenetix Posts: 1,752
    edited 2014-05-07 16:51
    You declare to the compiler that you want to use PBASIC 2.5 but you code is written as PBASIC 2.0.
    I think your issue was the DEC1 in your SERIN because DEC1 specifies only 1 Decimal Number and you need 2.
    I also changed the code to PBASIC 2.5 and to match The Elements of PBASIC Style.
    Your LEDs are Active-Low (Turn on when connected to Ground or 0V)?
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
     
    ' -----[ I/O PIN Definiciones ]------------------------------------------------
    LED1       PIN 1   ' LED on P1 (Added)
    LED12     PIN 12 ' LED on P12 (Added)
    ' -----[ Constant Declaration ] ----------------------------------------
    BaudRate   CON   16624 ' 9600 Baud, 8-Bit, No-Parity, Inverted
    ' -----[ Variable Declaration ] -----------------------------------------
    datoRecibido VAR Byte ' (Changed from Dato_Recibido to datoRecibido)
     
    DO ' (Changed from MAIN: to DO)
       SERIN 16, BaudRate, [DEC2 datoRecibido]
       ' (Changed from 16624 to BaudRate, DEC1 to DEC2, and from Dato_Recibido to datoRecibido)
    
       IF (datoRecibido = 1) THEN      'OK the LED turn on, when I send 1 from C# 
        ' (Changed from Dato_Recibido to datoRecibido)
        GOSUB LED_1 ' (Changed from GOTO to GOSUB, and LED1 to LED_1)
       ELSEIF(datoRecibido = 12) THEN  'This part of code not working, when i send 12 from C#    
      ' (Changed from Dato_Recibido to datoRecibido)
         GOSUB LED_12 ' (Changed from GOTO to GOSUB, and LED12 to LED_12)
      ENDIF ' (Added - End of IF...THEN...ELSEIF Statement)
    LOOP ' (Added - End of Infinite Loop)
    
    
     ' -----[ Sub-rutinas ]-----------------------------------------------------
     LED_1: ' (Changed from LED1 to LED_1)
        LOW 1
       RETURN ' (Changed from GOTO MAIN to RETURN)
    
     LED_12: ' (Changed from LED12 to LED_12)
        LOW 12
          RETURN ' (Changed from GOTO MAIN to RETURN)
    
  • spro80spro80 Posts: 7
    edited 2014-05-08 07:47
    Genetix wrote: »
    You declare to the compiler that you want to use PBASIC 2.5 but you code is written as PBASIC 2.0.
    I think your issue was the DEC1 in your SERIN because DEC1 specifies only 1 Decimal Number and you need 2.
    I also changed the code to PBASIC 2.5 and to match The Elements of PBASIC Style.
    Your LEDs are Active-Low (Turn on when connected to Ground or 0V)?
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
     
    ' -----[ I/O PIN Definiciones ]------------------------------------------------
    LED1       PIN 1   ' LED on P1 (Added)
    LED12     PIN 12 ' LED on P12 (Added)
    ' -----[ Constant Declaration ] ----------------------------------------
    BaudRate   CON   16624 ' 9600 Baud, 8-Bit, No-Parity, Inverted
    ' -----[ Variable Declaration ] -----------------------------------------
    datoRecibido VAR Byte ' (Changed from Dato_Recibido to datoRecibido)
     
    DO ' (Changed from MAIN: to DO)
       SERIN 16, BaudRate, [DEC2 datoRecibido]
       ' (Changed from 16624 to BaudRate, DEC1 to DEC2, and from Dato_Recibido to datoRecibido)
    
       IF (datoRecibido = 1) THEN      'OK the LED turn on, when I send 1 from C# 
        ' (Changed from Dato_Recibido to datoRecibido)
        GOSUB LED_1 ' (Changed from GOTO to GOSUB, and LED1 to LED_1)
       ELSEIF(datoRecibido = 12) THEN  'This part of code not working, when i send 12 from C#    
      ' (Changed from Dato_Recibido to datoRecibido)
         GOSUB LED_12 ' (Changed from GOTO to GOSUB, and LED12 to LED_12)
      ENDIF ' (Added - End of IF...THEN...ELSEIF Statement)
    LOOP ' (Added - End of Infinite Loop)
    
    
     ' -----[ Sub-rutinas ]-----------------------------------------------------
     LED_1: ' (Changed from LED1 to LED_1)
        LOW 1
       RETURN ' (Changed from GOTO MAIN to RETURN)
    
     LED_12: ' (Changed from LED12 to LED_12)
        LOW 12
          RETURN ' (Changed from GOTO MAIN to RETURN)
    



    Thanks Genetix.

    I change the code and put DEC 2: In this moment when I send 11, 12..... -> (Turn On Led 11, 12,....., this is working correctly when I change DEC 2 --> SERIN 16, 16624, [DEC2 datoRecibido] )


    Now my problem is with a decimal digit, when I sent a 1 ... to 9 ----> This i not working, the LED turn off. This problem begin when I change DEC 2 in serin


    My code in Basic Stamp is the follow:

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}

    '
    [ I/O Definiciones ]
    datoRecibido VAR Byte

    DO ' (Changed from MAIN: to DO)

    SERIN 16, 16624, [DEC2 datoRecibido]

    IF (datoRecibido = 1) THEN
    GOSUB LED_1
    ELSEIF(datoRecibido = 2) THEN
    GOSUB LED_2
    ELSEIF (datoRecibido = 12) THEN
    GOSUB LED_12
    ENDIF

    LOOP ' (Added - End of Infinite Loop)



    '
    [ Sub-rutinas ]
    LED_1:
    LOW 1
    RETURN

    LED_2:
    LOW 2
    RETURN


    LED_12:
    LOW 12
    RETURN
  • GenetixGenetix Posts: 1,752
    edited 2014-05-08 16:58
    Try placing a DEBUG statement after the SERIN statement so you can see what the BS2 is getting.
    DEBUG CR, "You Entered: ", receivedData, CR
    
    If you are going to have a large number of IF...THEN statements then maybe you should use SELECT...CASE instead.
    SELECT receivedData
      CASE 1
        GOSUB LED_1
      CASE 2
        GOSUB LED_2
      CASE 3
        GOSUB LED_3
      ...
      CASE 12
        GOSUB LED_12
    ENDSELECT
    

    What are you trying to do because your current program is very different from the program you started with?
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2014-05-08 16:58
    Hi,

    use the DEC formatter without specifying the number of digits like this
    SERIN 16, 16624, [DEC datoRecibido]
    

    Instead of telling SERIN to expect a certain number of digits we will append a newline to our data that tells SERIN all characters have been received. (read more in the Stamp manual)

    Appending the newline means adjusting the C# code a little.

    So first we must replace this
    puertoSerie.ReadExisting;
    puertoSerie.Write("1");
    

    with this
    puertoSerie.ReadExisting;
    puertoSerie.WriteLine("1");
    

    The difference between puertoSerie.Write("1"); and puertoSerie.WriteLine("1"); is that WriteLine automatically appends the newline character.
Sign In or Register to comment.