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;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Collections;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static Queue<String> q = new Queue<String>();
static String mensaje = "";
ArrayList arr;
static long cant_queue;
static bool comienzo = false;
int paso = 1;
String buffer; //voy a usar de 1 a 8, el cero lo ignoro.
String buffer_ant;
String str_recibido = "";
String log = "";
static bool enviar_a_arduino = false;
[DllImport("inpout32.dll", EntryPoint = "Out32")]
public static extern void Output(int adress, int value);
[DllImport("inpout32.dll", EntryPoint = "Inp32")]
public static extern short Input(int adress);
static SerialPort port;
public Form1()
{
InitializeComponent();
}
void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int bytesToRead = port.BytesToRead;
char[] z = new char[bytesToRead];
port.Read(z, 0, bytesToRead);
// ESTO NO VA ACA!!!!!!! str_recibido = "";
//PORQUE SI VA ACA, COMO NO RECIBE TODA LA LINEA DE UN SAQUE, CUANDO VUELVE A DISPARARSE ESTE EVENTO LO PONE VACIO DE NUEVO.
foreach (char s in z)
{
if (s == '\0')
{
Console.WriteLine(str_recibido);
str_recibido = "";
}
else
str_recibido += s;
}
}
private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//Write the serial port data to the console.
Console.WriteLine(port.ReadExisting());
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
port = new System.IO.Ports.SerialPort("COM17", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
//port.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
} catch(Exception ex)
{
MessageBox.Show("No se pudo abrir com17: " + ex.Message);
}
}
private void prueba()
{
Queue<String> q = new Queue<String>();
q.Enqueue("abc");
q.Enqueue("zzz");
q.Enqueue("fff");
q.Enqueue("hhh");
while (cant_queue > 0)
{
Console.WriteLine(q.Dequeue());
cant_queue--;
}
//foreach (String v in q)
// {
// Console.WriteLine(v);
// }
}
private void cmdMonitorLPT_Click(object sender, EventArgs e)
{
short v, v_ant;
int cant_ceros = 0;
buffer_ant = " "; // pin2_ant = "";
Step("stop");
if (cmdMonitorLPT.Text == "Monitor LPT")
{
arr = new ArrayList();
cmdMonitorLPT.Text = "Detener";
Application.DoEvents();
v = 0;
v_ant = -1;
while (v == 0)
{
v = leerLPT();
}
do
{
if (v != v_ant)
{
EnviarArduino();
v_ant = v;
}
v = leerLPT();
if (v == v_ant)
cant_ceros++;
else
cant_ceros = 0;
} while (cant_ceros < 600000);
Step("stop");
} else
cmdMonitorLPT.Text = "Monitor LPT";
}
private short leerLPT()
{
short v;
v = Input(888);
buffer = Convert.ToString(v, 2).PadLeft(8, '0');
return v;
}
private void EnviarArduino()
{
char pin2, pin6;
pin2 = buffer[7]; //.Substring(7, 1);
pin6 = buffer[3]; // t.Substring(3, 1);
if (buffer[7].CompareTo(buffer_ant[7]) != 0) //pin2.CompareTo(pin2_ant) != pin2_ant)
{
if (buffer[3] == '0') //pin6 == "0")
{
port.Write(paso.ToString());
paso++;
if (paso > 4)
paso = 1;
}
else
{
port.Write(paso.ToString());
paso--;
if (paso < 1)
paso = 4;
}
buffer_ant = buffer;
//pin2_ant = pin2;
System.Threading.Thread.Sleep(2);
}
}
public static int ToDecimal(string bin)
{
long l = Convert.ToInt64(bin, 2);
int i = (int)l;
return i;
}
private void Step(String dir)
{
switch (dir)
{
case "der" :
port.Write(paso.ToString());
paso++;
if (paso > 4)
paso = 1;
break;
case "izq":
port.Write(paso.ToString());
paso--;
if (paso < 1)
paso = 4;
break;
case "stop":
port.Write("X");
break;
}
}
private static void enviarOctetosANT(int cant)
{
int i = cant;
String s = ""; String octeto = "";
bool fin_datos = false;
while (i > 0 && cant_queue > 0 && !fin_datos)
{
octeto = q.Dequeue();
if (octeto == "TERMINO")
{
s += "{" + octeto + "}";
fin_datos = true;
q.Clear();
cant_queue = 0;
} else
{
s += "{" + octeto + "}";
i--;
cant_queue--;
}
}
if (!fin_datos)
s += "{FIN}"; //FIN indica que termino el set de octetos. TERMINO indica que termino todo.
port.Write(s);
}
private static void enviarOctetos(int cant)
{
int i = cant;
String s = ""; String octeto = "";
bool fin_datos = false;
while (i > 0 && cant_queue > 0 && !fin_datos)
{
octeto = q.Dequeue();
if (octeto == "TERMINO")
{
s += "{" + octeto + "}";
fin_datos = true;
q.Clear();
cant_queue = 0;
}
else
{
s += "{" + octeto + "}";
i--;
cant_queue--;
}
}
if (!fin_datos)
s += "{FIN}"; //FIN indica que termino el set de octetos. TERMINO indica que termino todo.
port.Write(s);
}
private void button5_Click(object sender, EventArgs e)
{
short v, v_ant;
int cant_ceros = 0;
buffer_ant = ""; // pin2_ant = "";
Step("stop");
if (cmdMonitorLPT.Text == "Monitor LPT")
{
arr = new ArrayList();
cmdMonitorLPT.Text = "Detener";
Application.DoEvents();
v = 0;
v_ant = -1;
while (v == 0)
{
v = leerLPT();
}
do
{
if (v != v_ant)
{
EnviarArduino();
v_ant = v;
}
v = leerLPT();
if (v == v_ant)
cant_ceros++;
else
cant_ceros = 0;
} while (cant_ceros < 600000);
Step("stop");
}
else
cmdMonitorLPT.Text = "Monitor LPT";
}
private void button6_Click(object sender, EventArgs e)
{
comienzo = false;
q.Clear();
cant_queue = 0;
Thread t1 = new Thread(new ParameterizedThreadStart(monitorLPT));
t1.Start();
}
private static void enviarDatosArduino()
{
if (cant_queue > 0)
{
//ESTA BIEN! SE EJECUTA 2 VECES PORQUE MANDA 1RO LOS 50 Y LUEGO LOS 22 (SON 72 EN TOTAL)
enviarOctetos(200);
}
}
private static void envioArduino(object num)
{
while (true)
{
if (cant_queue > 100)
{
port.Write("S");
comienzo = true;
}
if (enviar_a_arduino)
{
enviar_a_arduino = false;
enviarOctetos(50);
}
Thread.Sleep(200);
}
}
private static void CargarArchivo(object num)
{
cant_queue = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(Application.StartupPath + "..\\..\\..\\archivos\\a2.txt");
while (!sr.EndOfStream)
{
String linea = sr.ReadLine();
q.Enqueue(linea);
cant_queue++;
}
sr.Close();
}
private static void monitorLPT(object num)
{
short v, v_ant;
int cant_ceros = 0;
String octeto = "";
//bool comenzo_arduino = false;
v = 0;
v_ant = -1;
while (v == 0)
{
v = Input(888);
octeto = Convert.ToString(v, 2).PadLeft(8, '0');
//v = leerLPT();
}
do
{
if (v != v_ant)
{
q.Enqueue(octeto);
cant_queue++;
//Console.WriteLine("Guardando en queue: " + octeto + " Cantidad: " + cant_queue);
//if (cant_queue > 100 && !comenzo_arduino)
//{
// port.Write("S"); //luego que cargue 100 en el buffer, le digo a arduino que comience
// comenzo_arduino = true;
//}
//EnviarArduino();
v_ant = v;
} else
{
mensaje = "Evitando duplicados...";
}
v = Input(888);
octeto = Convert.ToString(v, 2).PadLeft(8, '0');
if (v == v_ant)
cant_ceros++;
else
cant_ceros = 0;
} while (true); // (cant_ceros < 600000);
}
private void MoverMotores()
{
for (int i = 0; i < 6; i++)
{
port.Write("00010001\0");
port.Write("00010000\0");
port.Write("00010001\0");
port.Write("00010000\0");
port.Write("00010001\0");
port.Write("00010000\0");
port.Write("00010001\0");
port.Write("00010000\0");
}
port.Write("00010001\0");
port.Write("XXXXXXXX\0");
}
private void button3_Click(object sender, EventArgs e)
{
MoverMotores();
}
}
}