__CONFIG (EC & WDTDIS & PWRTDIS & UNPROTECT & MCLREN & BORDIS & FCMEN & IESOEN);
void UARTInitialise(void)
{
TXSTA=0; // set up transmitter
TXEN=1; // Asychronous, 8 bit, BRGH=1
BRGH=1;
TRMT=1;
TRISC6=0; // C6 - TX set as output pin
RCSTA=0; // set up receiver
SPEN=1; // Asychronous, 8 bit, continuous receive, serial enable
CREN=1;
TRISC7=1; // C7 - RX receive set as input pin
// calculate Baud Rate = FOSC/(16 (X + 1))
//#define BaudRate 51 //SPBRG value for 9600 Baud when Fosc=8MHz and BRGH=1
#define BaudRate 8 //SPBRG value for 57600 Baud when Fosc=8MHz and BRGH=1
SPBRG=BaudRate; // setup baud rate
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
serialPort1->Open();
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
private: System::IO::Ports::SerialPort^ serialPort1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::ComponentModel::IContainer^ components;
protected:
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->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(12, 174);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Both;
this->textBox1->Size = System::Drawing::Size(515, 150);
this->textBox1->TabIndex = 0;
this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::textBox1_KeyPress);
//
// serialPort1
//
this->serialPort1->BaudRate = 19200;
this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(12, 12);
this->textBox2->Multiline = true;
this->textBox2->Name = L"textBox2";
this->textBox2->ScrollBars = System::Windows::Forms::ScrollBars::Both;
this->textBox2->Size = System::Drawing::Size(515, 156);
this->textBox2->TabIndex = 1;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(594, 351);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
// key hit over textbox send down serial line
cout << "key press " << char(e->KeyChar) << endl;
char data[2]="";
data[0]=char(e->KeyChar);
String ^sdata=gcnew String(data);
serialPort1->Write(sdata);
}
private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
// character received from serial line, display it in textbox
String^ message = serialPort1->ReadLine();
textBox2->Text = textBox2->Text + message + "\r\n";
}
};
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CsharpTerminal
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.Open();
backgroundWorker1.RunWorkerAsync();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// key on textbox pressed, read key and transmit down serial line
Console.WriteLine("keypress " + e.KeyChar);
char[] data = { e.KeyChar };
string s = new string(data);
serialPort1.Write(s);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine("background worker");
while(true)
{
// check for characters received from serial port and display on textbox
char[] data=new char[100];
serialPort1.Read(data,0,100);
// replace "\n" with "\r\n" for newlines in textbox
char[] data2 = new char[200];
int i, j;
for (i = j = 0; i < 100; i++)
if(data[i]=='\n')
{ data2[j++] = '\r'; data2[j++] = '\n'; }
else
data2[j++]=data[i];
string sdata = new string(data2);
textBox1.AppendText(sdata);
}
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?