如何从VB应用程序和arduino通过RF

编程入门 行业动态 更新时间:2024-10-23 09:37:47
如何从VB应用程序和arduino通过RF-433模块发送数据(How to send data from VB application and arduino thru RF-433 module)

我有点困惑。 我编写了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.available()可以用来通过串口监视器在键盘的帮助下发送数据,但是我在这里从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

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?

Here is my VB code:

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

最满意答案

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

Tx代码:

没有图书馆。

(没有图书馆)

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

与图书馆。

#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代码:

#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很多@Yve的指导和你给我完成这段代码的时间...... :)实现。

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.

Tx Code :

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

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:

#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.

更多推荐

本文发布于:2023-07-17 13:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144980.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   VB   RF   arduino

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!