如何从VB应用程序和arduino通过RF-433模块发送数据

时间:2022-05-01 01:40:26

I am confused a bit. I have written the VB code to make ON and OFF the LED connected to Arduino. I am sending data from VB app over COM port (instead of serial monitor) and the data is '1' for LED ON and '0' for OFF. Here I want to send this signal through RF-433 module. I have connected the TX pin of Arduino to Data pin of the RF module. On other hand, the second Arduino is connected to RF receiver with LED on Pin 12. Now I am not getting how to write code for Arduino of TX side to send data through RF? I mean if I use serial monitor to send data, then Serial.available() and Serial.read() can be used to send data over serial monitor with help of keyboard, but here I am sending that data from VB app. So what is the code for Arduino to activate RF TX connected on TX pin of Arduino?

我有点困惑。我编写了VB代码,用于打开和关闭连接到Arduino的LED。我通过COM端口(而不是串行监视器)从VB应用程序发送数据,LED ON的数据为“1”,OFF为“0”。在这里,我想通过RF-433模块发送此信号。我已将Arduino的TX引脚连接到RF模块的Data引脚。另一方面,第二个Arduino连接到引脚12上带有LED的RF接收器。现在我还没有得到如何为TX端的Arduino编写代码来通过RF发送数据?我的意思是如果我使用串行监视器发送数据,那么Serial.available()和Serial.read()可以用来通过串口监视器在键盘的帮助下发送数据,但是我在这里从VB应用程序发送数据。那么Arduino激活连接在Arduino TX引脚上的RF TX的代码是什么?

Here is my VB code:

这是我的VB代码:

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com12" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub

    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOn.Visible = True
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
        picOn.Visible = False
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub
End Class

3 个解决方案

#1


2  

Huh.... Finally did it... Following code is working successfully. I used SoftwareSerial library. The Tx code is simple and can be implemented without any library. I just took data from VB app on RX pin of arduino and sent it to the TX of arduino to which the RF module is connected. The receiver requires software serial library.

嗯....终于做到了......以下代码成功运行。我使用了SoftwareSerial库。 Tx代码很简单,无需任何库即可实现。我只是从arduino的RX引脚上的VB应用程序中获取数据并将其发送到RF模块所连接的arduino的TX。接收器需要软件串行库。

Tx Code :

Tx代码:

  1. WITHOUT LIBRARY.

    没有图书馆。

    (no library)

        int inByte;
        void setup()
        {
          Serial.begin(2400);
        }
        void loop()
        {
          if(Serial.available()>0)
          {
            inByte=Serial.read();
            switch(inByte)
            {
              case '0':
                  Serial.write(inByte);
                  break;
              case '1':
                  Serial.write(inByte);
                  break;               
               default:
                  break;
            delay(100);
            } 
          } 
        }
    
  2. WITH LIBRARY.

    与图书馆。

    #include <SoftwareSerial.h>
    #define rxPin 10
    #define txPin 11
    SoftwareSerial mySerial(10,11); //RX & TX
    int ch;
    
    void setup()
    {
      pinMode(rxPin,INPUT);
      pinMode(txPin,OUTPUT);
      Serial.begin(9600);
      //Serial.println("Hi");
    
      mySerial.begin(2400);
      //mySerial.println("Hello");
    } 
    void loop()
    {
      if(Serial.available()>0)
      {
        ch=Serial.read();
        mySerial.write(ch);
      }
    }
    

RX CODE:

RX代码:

 #include <SoftwareSerial.h>
    #define rxPin 10
    #define txPin 11
    SoftwareSerial mySerial(10,11); //RX & TX
    int ch=0;

    void setup()
    {
      pinMode(rxPin,INPUT);
      pinMode(13,OUTPUT);
      //pinMode(txPin,OUTPUT);
      Serial.begin(9600);
      //Serial.println("Hi");

      mySerial.begin(2400);
      //mySerial.println("Hello");
    } 
    void loop()
    {
      if(mySerial.available()>0)
      {
        ch=mySerial.read();
        //Serial.write(ch);
        switch(ch)
        {
          case '0':
              digitalWrite(13,LOW);
              break;
          case '1':
              digitalWrite(13,HIGH);
              break;

          default:
              break;
        }
      }
    }

Btw thanx a lot @Yve for the guidance and the time you gave me to complete this code... :) implementation.

Btw thanx很多@Yve的指导和你给我完成这段代码的时间...... :)实现。

#2


1  

First you have declared _serialPort As SerialPort and then proceeded to use SerialPort1
You need to test if serial port is open, as shown below. Opening (or closing) a port that is already open will throw an error.
You have no start or stop bits for your read and write.

首先,您已声明_serialPort为SerialPort,然后继续使用SerialPort1您需要测试串口是否打开,如下所示。打开(或关闭)已打开的端口将引发错误。您没有读取和写入的开始或停止位。

Public Class Form1

' unsure what this is being used for 
    Shared _continue As Boolean
' you had not declared SerialPort1
    Shared SerialPort1 As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'I don't understand why you are closing the port???
            SerialPort1.Close()
'A statement like this would be better to check if it is open
If SerialPort1.IsOpen = True Then
SerialPort1.close()
        SerialPort1.PortName = "com12" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub

    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOn.Visible = True
If SerialPort1.IsOpen = False Then
        SerialPort1.Open()
        SerialPort1.Write("1")
End if
        SerialPort1.Close()
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
        picOn.Visible = False
If SerialPort1.IsOpen = True Then
        SerialPort1.Write("0")
        SerialPort1.Close()
End if
    End Sub
End Class

Edit

编辑

See this, from the following link and incorporate your if statements into your button on and off events.

请参阅以下链接,并将if语句合并到按钮开启和关闭事件中。

int SerialValue = 0;

void setup(){

  pinMode(13, OUTPUT);
  Serial.begin(9600);

}

void loop(){
  SerialValue = Serial.read();
  if(SerialValue == 50){
    digitalWrite(13, HIGH);
  }
  if(SerialValue == 10){
    digitalWrite(13, LOW);
  }
}

http://forum.arduino.cc/index.php/topic,8566.0.html

http://forum.arduino.cc/index.php/topic,8566.0.html

I would also suggest looking at this site:

我也建议看看这个网站:

http://competefornothing.com/?p=738

http://competefornothing.com/?p=738

I know you are on this site and I would recommend utilizing it thoroughly:

我知道你在这个网站上,我建议你彻底使用它:

http://arduino.cc/

http://arduino.cc/

#3


0  

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com12" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub

    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOn.Visible = True
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
        picOn.Visible = False
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub
End Class

but I would suggest if you can send a byte not a bit once on a button_click, and check your baud rate , for 433mhz module it is good to use the as least baud rate as possible. If your data is not that much bigger use 1200bps, and set both micro controller with same baudrate

但我建议你是否可以在button_click上发送一个字节,并检查你的波特率,对于433mhz模块,最好使用尽可能低的波特率。如果您的数据不是那么大,请使用1200bps,并将两个微控制器设置为相同的波特率

#1


2  

Huh.... Finally did it... Following code is working successfully. I used SoftwareSerial library. The Tx code is simple and can be implemented without any library. I just took data from VB app on RX pin of arduino and sent it to the TX of arduino to which the RF module is connected. The receiver requires software serial library.

嗯....终于做到了......以下代码成功运行。我使用了SoftwareSerial库。 Tx代码很简单,无需任何库即可实现。我只是从arduino的RX引脚上的VB应用程序中获取数据并将其发送到RF模块所连接的arduino的TX。接收器需要软件串行库。

Tx Code :

Tx代码:

  1. WITHOUT LIBRARY.

    没有图书馆。

    (no library)

        int inByte;
        void setup()
        {
          Serial.begin(2400);
        }
        void loop()
        {
          if(Serial.available()>0)
          {
            inByte=Serial.read();
            switch(inByte)
            {
              case '0':
                  Serial.write(inByte);
                  break;
              case '1':
                  Serial.write(inByte);
                  break;               
               default:
                  break;
            delay(100);
            } 
          } 
        }
    
  2. WITH LIBRARY.

    与图书馆。

    #include <SoftwareSerial.h>
    #define rxPin 10
    #define txPin 11
    SoftwareSerial mySerial(10,11); //RX & TX
    int ch;
    
    void setup()
    {
      pinMode(rxPin,INPUT);
      pinMode(txPin,OUTPUT);
      Serial.begin(9600);
      //Serial.println("Hi");
    
      mySerial.begin(2400);
      //mySerial.println("Hello");
    } 
    void loop()
    {
      if(Serial.available()>0)
      {
        ch=Serial.read();
        mySerial.write(ch);
      }
    }
    

RX CODE:

RX代码:

 #include <SoftwareSerial.h>
    #define rxPin 10
    #define txPin 11
    SoftwareSerial mySerial(10,11); //RX & TX
    int ch=0;

    void setup()
    {
      pinMode(rxPin,INPUT);
      pinMode(13,OUTPUT);
      //pinMode(txPin,OUTPUT);
      Serial.begin(9600);
      //Serial.println("Hi");

      mySerial.begin(2400);
      //mySerial.println("Hello");
    } 
    void loop()
    {
      if(mySerial.available()>0)
      {
        ch=mySerial.read();
        //Serial.write(ch);
        switch(ch)
        {
          case '0':
              digitalWrite(13,LOW);
              break;
          case '1':
              digitalWrite(13,HIGH);
              break;

          default:
              break;
        }
      }
    }

Btw thanx a lot @Yve for the guidance and the time you gave me to complete this code... :) implementation.

Btw thanx很多@Yve的指导和你给我完成这段代码的时间...... :)实现。

#2


1  

First you have declared _serialPort As SerialPort and then proceeded to use SerialPort1
You need to test if serial port is open, as shown below. Opening (or closing) a port that is already open will throw an error.
You have no start or stop bits for your read and write.

首先,您已声明_serialPort为SerialPort,然后继续使用SerialPort1您需要测试串口是否打开,如下所示。打开(或关闭)已打开的端口将引发错误。您没有读取和写入的开始或停止位。

Public Class Form1

' unsure what this is being used for 
    Shared _continue As Boolean
' you had not declared SerialPort1
    Shared SerialPort1 As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'I don't understand why you are closing the port???
            SerialPort1.Close()
'A statement like this would be better to check if it is open
If SerialPort1.IsOpen = True Then
SerialPort1.close()
        SerialPort1.PortName = "com12" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub

    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOn.Visible = True
If SerialPort1.IsOpen = False Then
        SerialPort1.Open()
        SerialPort1.Write("1")
End if
        SerialPort1.Close()
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
        picOn.Visible = False
If SerialPort1.IsOpen = True Then
        SerialPort1.Write("0")
        SerialPort1.Close()
End if
    End Sub
End Class

Edit

编辑

See this, from the following link and incorporate your if statements into your button on and off events.

请参阅以下链接,并将if语句合并到按钮开启和关闭事件中。

int SerialValue = 0;

void setup(){

  pinMode(13, OUTPUT);
  Serial.begin(9600);

}

void loop(){
  SerialValue = Serial.read();
  if(SerialValue == 50){
    digitalWrite(13, HIGH);
  }
  if(SerialValue == 10){
    digitalWrite(13, LOW);
  }
}

http://forum.arduino.cc/index.php/topic,8566.0.html

http://forum.arduino.cc/index.php/topic,8566.0.html

I would also suggest looking at this site:

我也建议看看这个网站:

http://competefornothing.com/?p=738

http://competefornothing.com/?p=738

I know you are on this site and I would recommend utilizing it thoroughly:

我知道你在这个网站上,我建议你彻底使用它:

http://arduino.cc/

http://arduino.cc/

#3


0  

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com12" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub

    Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOn.Visible = True
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
        picOn.Visible = False
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub
End Class

but I would suggest if you can send a byte not a bit once on a button_click, and check your baud rate , for 433mhz module it is good to use the as least baud rate as possible. If your data is not that much bigger use 1200bps, and set both micro controller with same baudrate

但我建议你是否可以在button_click上发送一个字节,并检查你的波特率,对于433mhz模块,最好使用尽可能低的波特率。如果您的数据不是那么大,请使用1200bps,并将两个微控制器设置为相同的波特率