Programa C# DSP

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy