vb.net 如何通过RF-433模块从VB应用程序和arduino发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17467745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send data from VB application and arduino thru RF-433 module
提问by Dexter
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 应用程序发送数据,数据为“1”表示 LED 亮,“0”表示关闭。这里我想通过RF-433模块发送这个信号。我已将 Arduino 的 TX 引脚连接到 RF 模块的数据引脚。另一方面,第二个 Arduino 连接到 RF 接收器,引脚 12 上有 LED。现在我不知道如何为 TX 端的 Arduino 编写代码以通过 RF 发送数据?我的意思是,如果我使用串行监视器发送数据,那么Serial.available()并且Serial.read()可以用于在键盘的帮助下通过串行监视器发送数据,但在这里我从 VB 应用程序发送该数据。那么Arduino激活连接在Arduino TX pin上的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
采纳答案by Dexter
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 :
发送代码:
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); } }
没有图书馆。
(没有图书馆)
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 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.
顺便说一句,@Yve 提供了很多指导以及您给我完成此代码的时间...... :) 实现。
回答by Yvette
First you have declared _serialPort As SerialPortand 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 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
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:
我知道你在这个网站上,我建议你彻底使用它:
回答by yido
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,并将两个微控制器设置为相同的波特率

