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

时间:2013-07-04 10:17:53

标签: vb.net serial-port arduino communication

我有点困惑。我编写了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的代码是什么?

这是我的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 个答案:

答案 0 :(得分:2)

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

Tx代码:

  1. 没有图书馆。

    (没有图书馆)

        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);
      }
    }
    
  3. 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;
            }
          }
        }
    

    非常感谢@Yve的指导和你给我完成这段代码的时间...... :)实施。

答案 1 :(得分:1)

首先,您已声明_serialPort As 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

修改

请参阅以下链接,并将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://competefornothing.com/?p=738

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

http://arduino.cc/

答案 2 :(得分: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

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