G
GAMinTN
Guest
I have tried several different ways to read data from a serial port an nothing seems to work. What I have is an Epson receipt printer and I am trying to determine the status of a cash drawer attached to the printer. I have to Epson programming manual and know what I should be looking for as far as what should be returned. I just cannot seem to get the code right to read this data and then do something with it.
The question in all of this is - What is the best way to get this done? Here is what I have so far (Visual Studio 2013 and C#):
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.IO;
using System.IO.Ports;
using System.Threading;
namespace Printer_Status_Test
{
public partial class Form1 : Form
{
SerialPort serial1 = new SerialPort();
public Form1()
{
InitializeComponent();
CommPortSetup();
}
private void button1_Click(object sender, EventArgs e)
{
serial1.Open();
serial1.Write((Char)(29) + "r" + (Char)(2) + "\r\n");
serial1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}
private delegate void SetTextDeleg(string text);
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(500);
string data = serial1.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data.Trim() });
}
private void si_DataReceived(string data)
{
if (data == "0")
{
MessageBox.Show("The drawer is closed.");
}
else
{
MessageBox.Show("The drawer is open.");
}
}
private void CommPortSetup() //Setup for serial port
{
//Serial Port 1 Parameters
serial1.PortName = "COM1";
serial1.BaudRate = 19200;
serial1.DataBits = 8;
serial1.Parity = Parity.None;
serial1.StopBits = StopBits.One;
serial1.Handshake = Handshake.None;
}
}
}
Thank you for your help.
Continue reading...
The question in all of this is - What is the best way to get this done? Here is what I have so far (Visual Studio 2013 and C#):
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.IO;
using System.IO.Ports;
using System.Threading;
namespace Printer_Status_Test
{
public partial class Form1 : Form
{
SerialPort serial1 = new SerialPort();
public Form1()
{
InitializeComponent();
CommPortSetup();
}
private void button1_Click(object sender, EventArgs e)
{
serial1.Open();
serial1.Write((Char)(29) + "r" + (Char)(2) + "\r\n");
serial1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}
private delegate void SetTextDeleg(string text);
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(500);
string data = serial1.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data.Trim() });
}
private void si_DataReceived(string data)
{
if (data == "0")
{
MessageBox.Show("The drawer is closed.");
}
else
{
MessageBox.Show("The drawer is open.");
}
}
private void CommPortSetup() //Setup for serial port
{
//Serial Port 1 Parameters
serial1.PortName = "COM1";
serial1.BaudRate = 19200;
serial1.DataBits = 8;
serial1.Parity = Parity.None;
serial1.StopBits = StopBits.One;
serial1.Handshake = Handshake.None;
}
}
}
Thank you for your help.
Continue reading...