rf433 module with c#

Hello
i want Send (e.g. ‘123456’) with C#
but when press the button ‘Send’ in C# code not Send (e.g. 123456)

C# code:

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;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeComponent();
serialPort1.PortName = “COM14”; // Select the Com Port
serialPort1.BaudRate = 9600; // Select the BoudRate
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
serialPort1.Open();
if (serialPort1.IsOpen)
{
serialPort1.WriteLine(textBox1.Text);

}
serialPort1.Close();
}

}
}


Arduino code:

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {

Serial.begin(9600);
mySwitch.enableTransmit(10);
mySwitch.setRepeatTransmit(15);

}

void loop() {
start:
long int code = Serial.read();
if (code > 0)
{
mySwitch.send(code,24);
Serial.print(code);
}
else
{
goto start;
}
}

First, loop() loops. Get rid of the stupid goto.

Second, opening the serial port resets the Arduino, unless you have modified it to not do that. If you had, you should have said so.

Jamming data at it while it is resetting is pointless. Closing the serial port to block a reply is no way to debug anything.

Disable the send button on the form in the _Load function.

Put a button on the form to open the serial port. In the callback, open the port. If the port is successfully opened, enable the send button.

Put a button on the form to close the serial port. In the callback, close the port and disable the send button.

 long int code = Serial.read();

Serial.read() returns an int. There is not need to waste space storing the 2-byte int in a 4-byte long.

RCSwitch mySwitch = RCSwitch();

is wrong.

RCSwitch mySwitch;

is right.

I change the code :slight_smile:

when Send the code from C# , Receive Decimal code :confused:
but i want Receive ASCII code !?

attached C# and Arduino code !
thanks

arduino.txt (310 Bytes)

c#.txt (3.04 KB)

char code = Serial.read();
if (code > 0)
{
mySwitch.send(code,24);

A char is NOT 24 bytes. Get over it!