Basic Stamp: turn led from application in C #
spro80
Posts: 7
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
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
One thing I notice in both programs is there is no looping main routine so each program will only run once.
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
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#.
IF Dato_Recivido = "1" THEN LOW 1
If you need more commands for each you can structure the command with an ENDIF.
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. )
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.
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
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
Good eye, I missed that. :thumb:
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
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.
The above will transmit a binary value of 49
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.
Alternatively you can enclose the value to compare in quotes
Lastly this should also work
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.
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
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
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)?
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
What are you trying to do because your current program is very different from the program you started with?
use the DEC formatter without specifying the number of digits like this
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
with this
The difference between puertoSerie.Write("1"); and puertoSerie.WriteLine("1"); is that WriteLine automatically appends the newline character.