Vous êtes sur la page 1sur 5

C:\Users\user\Desktop\backup usb 081215\MT418\Programa serial del proyecto DSP\Serial28335\Serial28335\Principal.cs sbado, 12 de diciembre de 2015 10:57 a.m.

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];
int RxBytes=0;
string RxString;
int TxBytes = 0;
string TxString;
public Main()
{
InitializeComponent();
try
{
serialPuerto.PortName = "COM11";
//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);
};
-1-
C:\Users\user\Desktop\backup usb 081215\MT418\Programa serial del proyecto DSP\Serial28335\Serial28335\Principal.cs sbado, 12 de diciembre de 2015 10:57 a.m.
if (this.serialPuerto.IsOpen == false) this.buttonOpen.Text = "CONECTAR";
}

// dataTX[0] indica el comando que se utilizar


// dataTX[1-4] indica la direccin inicial a la cual se escribir/de la cual se leer
// dataTX[5-8] (como nmero) indica la cantidad de direcciones que se escribirn/leern
// dataTX[9] (solo) debe ser 64 cuando se lee direcciones
// dataTX[9-...] cuando se escriben direcciones se mandan 2 bytes por direccin (9 y
10: 1ra direccin, 11 y 12: 2da direccin, etc)
private void buttonActualiza_Click(object sender, EventArgs e)
{
// Se escribe el valor de la referencia que se quiere cambiar
uint direccion32 = Convert.ToUInt32(this.textBoxDir.Text, 10);
// Envo el '3' para ejecutar el comando '3', que es EscribirDSP()
dataTX[0] = 3;
// En los siguientes 4 bytes se enva la direccin (32 bits) en donde se encuentra
la referencia de control
// Para nuestro cdigo, la referencia es la variable 'theta_res_int' el cual es un
entero.
dataTX[1] = 0;
dataTX[2] = 0;
dataTX[3] = 192;
dataTX[4] = 2;
// En los siguientes 4 bytes se enva el nmero de Direcciones de memoria del DSP
ms 1, en los cuales se
// ESCRIBIR los datos a enviar.
dataTX[5] = 0;
dataTX[6] = 0;
dataTX[7] = 0;
dataTX[8] = 0; // Cantidad = 0 -> Se escribir solo 1 registro en el DSP
// Los siguientes 2 bytes son los datos a enviar, ya que se ESCRIBIR solo 1
registro, se necesitan enviar
// solamente 2 bytes.
dataTX[9] = (byte)(direccion32 >> 8 & 0xff); // Primero se enva el byte alto
dataTX[10] = (byte)(direccion32 & 0xff); // Luego el bajo
Transmitir(dataTX);
}
private void Transmitir(byte[] datos)
{
int i,enviado,recibe;
byte[] actualizaRX = new byte[1];
byte[] actualizaTX = new byte[1];
for (i = 0; i < 9; i++)
{
this.serialPuerto.Write(datos, i, 1);
enviado = datos[i];
actualizaTX[0] = (byte)enviado;
TxDataUpdateBin(actualizaTX);
Thread.Sleep(20);
recibe=this.serialPuerto.ReadByte();
actualizaRX[0] = (byte)recibe;
RxDataUpdateBin(actualizaRX);
if (enviado != recibe) {
System.Console.WriteLine("error de envio");
MessageBox.Show("Error de Comando", "Fall al enviar", MessageBoxButtons.OK);
return;
};
}
Thread.Sleep(20);
for (i = 0; i < (dataTX[8]+1)*2; i++) // Enva los datos (2 bytes por Direccin, 1
Direccin)
{
this.serialPuerto.Write(datos, 9 + i, 1); // Enva cada byte del valor de
referencia (el 9 y 10)
-2-
C:\Users\user\Desktop\backup usb 081215\MT418\Programa serial del proyecto DSP\Serial28335\Serial28335\Principal.cs sbado, 12 de diciembre de 2015 10:57 a.m.
enviado = datos[9+i];
actualizaTX[0] = (byte)enviado;
TxDataUpdateBin(actualizaTX);
Thread.Sleep(20);
recibe = this.serialPuerto.ReadByte();
dataRX[i] = (byte)recibe;
actualizaRX[0]=(byte)recibe;
RxDataUpdateBin(actualizaRX);
}
}
private void RxDataUpdateBin(byte[] bb)
{
string s = "";
foreach (byte i in bb)
{
s += String.Format("{0:X2}", i);
}
RxBytes += s.Length;
RxString = RxString + s;
this.RXContador.Text = RxBytes.ToString();
//this.textBoxRX.Text += this.AtoX(s);
this.textBoxRX.Text = RxString;
return;
}
private void RxDataUpdate(string s)
{

RxBytes += s.Length;
RxString = RxString + s;
this.RXContador.Text = RxBytes.ToString();
this.textBoxRX.Text = AtoX(RxString);
return;
}
/// <summary>
/// Updates the TX byte data info.
/// </summary>
private void TxDataUpdate(string s)
{
TxBytes += s.Length;
TxString = TxString + s;
this.TXContador.Text = TxBytes.ToString();
this.textBoxTX.Text += this.AtoX(s);
return;
}
/// <summary>
/// Updates the TX byte byte data info.
/// </summary>
private void TxDataUpdateBin(byte[] bb)
{
string s = "";

foreach (byte i in bb)


{
s += String.Format("{0:X2}", i);
}
TxBytes += s.Length;
TxString = TxString + s;
this.TXContador.Text = TxBytes.ToString();
this.textBoxTX.Text = TxString;//this.AtoX(TxString);
//this.textBoxTX.Text += this.AtoX(s);
return;
}
/// <summary>
/// Converts an ASCII string to hex formatted lines.
-3-
C:\Users\user\Desktop\backup usb 081215\MT418\Programa serial del proyecto DSP\Serial28335\Serial28335\Principal.cs sbado, 12 de diciembre de 2015 10:57 a.m.
/// </summary>
private string AtoX(string asc)
{
int nLines;
int nChars;
int offset;
string hex = "";

// Compute number of hex lines.


if ((asc.Length % 16) > 0)
nLines = asc.Length / 16 + 1;
else
nLines = asc.Length / 16;

// Convert into hex lines.


for (int i = 0; i < nLines; i++)
{
offset = i * 16;
if ((asc.Length - offset) > 16)
nChars = 16;
else
nChars = asc.Length - offset;
hex += this.HexLine(i, asc.Substring(offset, nChars)) + "\r\n";
}
return hex;
}
/// <summary>
/// Converts a 16 byte ASCII string into one hex formatted line.
/// </summary>
private string HexLine(int lNum, string asc)
{
string hex = "";

// Copy line to char buffer.


char[] c = new char[16];
asc.CopyTo(0, c, 0, asc.Length);

// Create offset prefix.


hex += String.Format("{0:X4} - {1:X4}", lNum * 16, (lNum + 1) * 16 - 1);
hex += " ";

// Convert chars to hex representation.


for (int i = 0; i < asc.Length; i++)
{
if ((i != 0) && ((i % 4) == 0))
hex += " ";
hex += String.Format("{0:X2}", (int)c[i]);
}

// Add padding.
int nSpaces = 62 - hex.Length;
for (int i = 0; i < nSpaces; i++)
hex += " ";

// Add ASCII to end of line.


for (int i = 0; i < asc.Length; i++)
{
if (((int)c[i] < 32) || ((int)c[i] > 126))
hex += ".";
else
hex += c[i].ToString();
}

// Return hex dump line.


-4-
C:\Users\user\Desktop\backup usb 081215\MT418\Programa serial del proyecto DSP\Serial28335\Serial28335\Principal.cs sbado, 12 de diciembre de 2015 10:57 a.m.
return hex;
}
}
}

-5-

Vous aimerez peut-être aussi