Reputation: 5
I'm trying to communicate with the GC420t via serial port in c#.
I need to get the status of the printer.
I want to send the command ~HQES and receive the status.
I'm able to write commands: I've tryied to print succesfully some qrcodes.
But when I write the status info command I don't get any answer.
This is my test code:
//define serial port
static SerialPort _serialPort = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One);
public Serial()
{
try
{
_serialPort.Open();
WriteCommand();
ReadStatus();
}
catch (Exception ex)
{
_serialPort.Close();
}
}
public void WriteCommand()
{
string qrcode = "^XA^FO,20,20^BQ,2,10^FDD03048F,LM,N0123456789,A12AABB,B0006qrcode^FS^XZ";
string statusInfo = " ~HQES";
_serialPort.Write(statusInfo);
}
public void ReadStatus()
{
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
EDIT: It will be fine for me also to be able to get that info on request:
WriteCommand();
int timeout = 3000;
while (timeout > 0)
{
Thread.Sleep(10);
timeout--;
// Timeout
string status = _serialPort.ReadExisting();
}
Upvotes: 0
Views: 895
Reputation: 6773
You should check ifs the printer configured to use any form of handshaking? Particularly XON/XOFF or DTR .
Ref page 24 of User Guide. https://www.uline.com/PDF/IH-7246VND.PDF
In the event the link breaks - the relevant text is
"The printer has a DCE serial communication port for hardware compatibility with legacy 888 printers. The required cable must have a nine-pin "D" type (DB-9P) male connector on one end which plugs into the mating (DB-9S) serial port located on the back of the printer. The other end of this signal interface cable connects to a serial port on the host computer. The cable is a Null-Modem (cross-over signal connections) cable. For pinout information, refer to Appendix A.
The serial port communication settings between the printer and host (typically a PC) must match for reliable communication. The Bits per second (or Baud rate) and Flow control are the most common settings that get changed. The host (typically a Windows PC) needs to have the data Flow control changed to match the printer's default communication method: Hardware and is noted by the Host Handshake setting DTR/Xon/Xoff. This combined hardware (DTR) and software (Xon/Xoff) mode may need to change depending{ upon use with non-Zebra application software or the serial cable variation in use."
Upvotes: 1
Reputation: 11889
You need to hook up the DataReceived
handler before you make any calls, otherwise it might have answered before you hook it up.
Move ReadStatus
up 1 line so that it comes straight after the constructor.
_serialPort.Open();
ReadStatus();
WriteCommand();
Upvotes: 1