May 22, 2011
by kle8309
|
I bought a couple of these modules recently from sureelectronics.net for $30 (I almost bought the xbee but decided to save money)
rf modules
I got them to work and I'm gonna explain how to use them.
The nice features that these modules have are: uart ready, and easy set up (only 7 pins)
The pins are GND, 3.3V/5V, Enable, RXD, TXD, B/RX, A/TX
Ground the B/RX and A/TX if you want to use uart
The enable is active high
The module has a nice data buffer 512 bytes
basically if you want to send say char you would send it using uart_send from
the MCU (you need to connect the MCU's RXD and TXD to the corresponding module
pins)the module will load it up and send immediately. This is like shoot and
forget.
Or you can do what I did, using a hyperterminal (so you don't have to use the
MCUs for testing)
either putty or something else nicer (but you need to compile it code using visual studio like I did)
microsoft c++ code
Note RF baudrate is 9600 on default
RFID is also default (unless you can use rf-magic software)
Here is a demo of the setup and test
RF demo
I'm still working on the rf-magic. I need to get the rs-232 cable first.
But unless you plan on buying more than a set of two, you have to care about
changing the default settings.
I'm planning on using this to control a robot from my own room!
Did I mention it can transmit up to 1000 meters! Well on paper anyways.
Cheers |
May 24, 2011
by kle8309
|
Alright, so decided to pimp out my uart interface a bit and work on some of my programming skills.
This is the c++ code for form.h that I used for my Windows Forms Application (New CRL project and Windows Forms App).
I learn a lot by doing this. Just a hint of how to access the different components of the windows forms: use this->name of oject->property.
It took me a while to learn how to use the "this" function.
Last thing I want to say is use the Microsoft support page for example, it's great help!
I will post a video demonstrating this later on.
My next objective will be to use threads in the program (backgroundworker as they call it).
Lastly, I will try to communicate with a social network page like facebook to update information
(nice feature if you're building a robot)
#pragma once
namespace test3 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO::Ports;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
findPorts();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::IO::Ports::SerialPort^ _serialPort;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::ComboBox^ comboBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::ComboBox^ comboBox2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::ProgressBar^ progressBar1;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Label^ label1;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->_serialPort = (gcnew System::IO::Ports::SerialPort(this->components));
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->button2 = (gcnew System::Windows::Forms::Button());
this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
this->button3 = (gcnew System::Windows::Forms::Button());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->button4 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// _serialPort
//
this->_serialPort->PortName = L"COM5";
this->_serialPort->ReadTimeout = 500;
this->_serialPort->WriteTimeout = 500;
//
// button1
//
this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button1->Location = System::Drawing::Point(36, 125);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(101, 62);
this->button1->TabIndex = 0;
this->button1->Text = L"Send";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(128)));
this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox1->ForeColor = System::Drawing::SystemColors::Desktop;
this->textBox1->Location = System::Drawing::Point(36, 203);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(261, 26);
this->textBox1->TabIndex = 1;
this->textBox1->Text = L"Enter Here";
//
// comboBox1
//
this->comboBox1->AllowDrop = true;
this->comboBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System::Drawing::Point(395, 12);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(102, 28);
this->comboBox1->TabIndex = 2;
this->comboBox1->Text = L"COM ports";
//
// button2
//
this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button2->Location = System::Drawing::Point(395, 98);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(101, 62);
this->button2->TabIndex = 3;
this->button2->Text = L"Init Port";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// comboBox2
//
this->comboBox2->AllowDrop = true;
this->comboBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->comboBox2->FormattingEnabled = true;
this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(3) {L"9600", L"38400", L"115200"});
this->comboBox2->Location = System::Drawing::Point(395, 64);
this->comboBox2->Name = L"comboBox2";
this->comboBox2->Size = System::Drawing::Size(102, 28);
this->comboBox2->TabIndex = 4;
this->comboBox2->Text = L"Baud rate";
//
// button3
//
this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button3->Location = System::Drawing::Point(195, 125);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(101, 62);
this->button3->TabIndex = 5;
this->button3->Text = L"Read";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// textBox2
//
this->textBox2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(128)));
this->textBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox2->Location = System::Drawing::Point(36, 82);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(260, 26);
this->textBox2->TabIndex = 7;
this->textBox2->Text = L"Received Here";
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(37, 32);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 32);
this->progressBar1->TabIndex = 9;
this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
//
// button4
//
this->button4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button4->Location = System::Drawing::Point(395, 167);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(101, 62);
this->button4->TabIndex = 10;
this->button4->Text = L"Close Port";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(32, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(100, 20);
this->label1->TabIndex = 11;
this->label1->Text = L"Port Status";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Silver;
this->ClientSize = System::Drawing::Size(545, 262);
this->Controls->Add(this->label1);
this->Controls->Add(this->button4);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->button3);
this->Controls->Add(this->comboBox2);
this->Controls->Add(this->button2);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Uart Gui v1.1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: void findPorts(void)
{
// get port names
array<Object^>^ objectArray = SerialPort::GetPortNames();
// add string array to combobox
this->comboBox1->Items->AddRange( objectArray );
}
// Send button
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
// add sender name
String^ name = this->_serialPort->PortName;
// grab text and store in send buffer
String^ message = this->textBox1->Text;
// write to serial
if(this->_serialPort->IsOpen)
this->_serialPort->WriteLine(String::Format("<{0}>: {1}",name,message));
else
this->textBox1->Text="Port Not Opened";
}
// Init button
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
// set COM port
StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
if(stringComparer->Equals("COM ports",this->comboBox1->Text)){
this->textBox1->Text="Please Select COM Port";
}
else if(stringComparer->Equals("Baud rate",this->comboBox2->Text)){
this->textBox1->Text="Please Select Baud Rate";
}
else{
if(!this->_serialPort->IsOpen){
this->_serialPort->PortName=this->comboBox1->Text;
//this->textBox1->Text=this->comboBox1->Text;
this->_serialPort->BaudRate=Int32::Parse(this->comboBox2->Text);
//this->textBox1->Text=this->comboBox2->Text;
this->textBox1->Text="Enter Message Here";
//open serial port
this->_serialPort->Open();
}
// is open status
if(this->_serialPort->IsOpen)
this->progressBar1->Value=100;
}
}
// Read button
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
if(this->_serialPort->IsOpen){
// discard input buffer
// this->_serialPort->DiscardInBuffer();
try {
this->textBox2->Text=this->_serialPort->ReadLine();
}
catch (TimeoutException ^) {
this->textBox2->Text="TimeoutException: No Input";
}
}
else
this->textBox2->Text="Port Not Opened";
}
private: System::Void progressBar1_Click(System::Object^ sender, System::EventArgs^ e) {
}
// close port button
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
//close serialPort
this->_serialPort->Close();
if(!this->_serialPort->IsOpen){
this->progressBar1->Value=0;
}
}
};
}
|