0% found this document useful (0 votes)
104 views4 pages

Programa C# DSP

This document contains code for a C# program that communicates with a serial port. It defines classes and methods for opening/closing the serial port connection, sending data as bytes over the serial port by writing to the port, and receiving data as bytes by reading from the port. The data being sent and received includes 16-bit integers, doubles, and strings.

Uploaded by

Omar Cirineo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views4 pages

Programa C# DSP

This document contains code for a C# program that communicates with a serial port. It defines classes and methods for opening/closing the serial port connection, sending data as bytes over the serial port by writing to the port, and receiving data as bytes by reading from the port. The data being sent and received includes 16-bit integers, doubles, and strings.

Uploaded by

Omar Cirineo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Serial28335
{
public partial class Main : Form
{
byte[] dataTX = new byte[1024];
byte[] dataRX = new byte[1024];

public Main()
{
InitializeComponent();
try
{
serialPuerto.PortName = "COM10";
//serialPuerto.Open();
}
catch (Exception ex) { System.Console.WriteLine(ex.ToString()); };
}

private void buttonOpen_Click(object sender, EventArgs e)


{
if (this.buttonOpen.Text == "CONECTAR")
{
if (this.serialPuerto.IsOpen == false)
{
try
{
this.serialPuerto.Open();
}
catch (Exception error)
{
System.Console.WriteLine("Error" + error.ToString());
System.Console.WriteLine(error.Message);
MessageBox.Show("Error de Puerto", "Puerto Ocupado",
MessageBoxButtons.OK);
};
if (this.serialPuerto.IsOpen == true) this.buttonOpen.Text =
"DESCONECTAR";
};
}
else if (this.serialPuerto.IsOpen == true)
try
{
this.serialPuerto.Close();
}
catch (Exception error)
{
System.Console.WriteLine("Error" + error.ToString());
System.Console.WriteLine(error.Message);
MessageBox.Show("Error de Puerto", "Error al cerrar Puerto",
MessageBoxButtons.OK);
};
if (this.serialPuerto.IsOpen == false) this.buttonOpen.Text =
"CONECTAR";
}

private void button1_Click(object sender, EventArgs e)


{
uint direccion16 = Convert.ToUInt16(this.textBoxDir1.Text, 10);
//Conviertiendo en un entero 16 bits
//Enviar caso
dataTX[0] = 1;
this.serialPuerto.Write(dataTX, 0, 1);
//Enviar los 2 bytes
dataTX[0] = (byte)(direccion16 >> 8 & 0xff);
this.serialPuerto.Write(dataTX, 0, 1);
dataTX[0] = (byte)(direccion16 & 0xff);
this.serialPuerto.Write(dataTX, 0, 1);

}
private void button1_Click_1(object sender, EventArgs e)
{
double numdouble = Convert.ToDouble(this.textBox1.Text);
//Enviar caso
dataTX[0] = 2;
this.serialPuerto.Write(dataTX, 0, 1);
//Enviar los 2 bytes
dataTX[0] = (byte)numdouble;
this.serialPuerto.Write(dataTX, 0, 1);
dataTX[0] = (byte)((numdouble * 100) % 100);
this.serialPuerto.Write(dataTX, 0, 1);

private void button2_Click(object sender, EventArgs e)


{
int number16 = Convert.ToInt16(this.textBox2.Text, 10);
//Enviar caso
dataTX[0] = 3;
this.serialPuerto.Write(dataTX, 0, 1);
//Enviar los 2 bytes
dataTX[0] = (byte)(number16>> 8);
this.serialPuerto.Write(dataTX, 0, 1);
dataTX[0] = (byte)((number16) & ((1 << 8) - 1));
this.serialPuerto.Write(dataTX, 0, 1);
}

private void label3_Click(object sender, EventArgs e)


{

private void label4_Click(object sender, EventArgs e)


{
}

private void textBoxDir1_TextChanged(object sender, EventArgs e)


{

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void textBox2_TextChanged(object sender, EventArgs e)


{

private void textBox3_TextChanged(object sender, EventArgs e)


{

private void button4_Click(object sender, EventArgs e)


{
//Enviar caso
dataTX[0] = 5;
this.serialPuerto.Write(dataTX, 0, 1);
Thread.Sleep(20);
int[] recis = new int[2];
recis[0] = this.serialPuerto.ReadByte();
Thread.Sleep(20);
recis[1] = this.serialPuerto.ReadByte();
this.textBox3.Text = ((recis[0] << 8) + recis[1]).ToString();

private void button5_Click(object sender, EventArgs e)


{
//Enviar caso
dataTX[0] = 6;
string res="";
this.serialPuerto.Write(dataTX, 0, 1);
Thread.Sleep(20);
int aux = this.serialPuerto.ReadByte();
int[] resis=new int[2];
for (int i = 0; i < aux; i++)
{
Thread.Sleep(20);
resis[0] = this.serialPuerto.ReadByte();
resis[1] = this.serialPuerto.ReadByte();
res = res + ((resis[0] << 8) + resis[1]).ToString() + '\t' + '\r' +
'\n';
if (i % 5 == 0) this.serialPuerto.Write(dataTX, 0, 1);
}
this.textBoxRX.Text = res;

private void textBoxRX_TextChanged_2(object sender, EventArgs e)


{

private void textBoxRX_TextChanged(object sender, EventArgs e)


{

private void textBox3_TextChanged_1(object sender, EventArgs e)


{

}
}
}

You might also like