Jump to content

Printing Barcode number in c sharp through ZDesigner S4M-203dpi ZPL

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Sreekar

Sreekar

    Newbie

  • Members
  • Pip
  • 1 posts
hi,
In button click of one button i have wrote c sharp code to print barcode number. My Barcode printer is connected through USB cable to my system. When i connected it through LPT1 cable it is getting printing. But after changing from LPT1 cable to USB cable my c sharp code is getting the following error:
Invalid handle.
Parameter name: handle
Can u suggest me how to solve this problem.
Below is the code which i wrote in C sharp.
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.Data.SqlTypes;
using System.Data.SqlClient;
using System.IO;

using System.IO.Ports;
using Microsoft.Win32.SafeHandles;
using System.Web;
using System.Runtime.InteropServices;


namespace TTDGODOWN
{


public partial class BarcodeNo : Form

{

Boolean dup = false;

int Lbl_cnt = 1;
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern SafeFileHandle CreateFile(
string fileName,
[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flags,
IntPtr template);

public byte[] ConvertStringToByteArray(string stringToConvert)
{
return (new UnicodeEncoding()).GetBytes(stringToConvert);
}

SqlConnection ObjSqlConnection;
SqlCommand ObjSqlCommand;
private string connectionstring = @"Initial Catalog=TTDPRESS;" +
@"Data Source=10.1.3.21;" +
@"User ID=sa;" +
@"Password=tirumala";
public BarcodeNo()
{

InitializeComponent();
}


private void button2_Click(object sender, EventArgs e)
{
DateTime Dt = DateTime.Today;
String BarcodeID;
BarcodeID = listBox1.SelectedItem.ToString();
SafeFileHandle printer = CreateFile("USB001", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
FileStream FS = new FileStream(printer, FileAccess.ReadWrite);
String HumanReadable;
HumanReadable = listBox1.SelectedItem.ToString();
BinaryReader Br = new BinaryReader(FS);
Byte[] Buff = new Byte[1024];
Buff = System.Text.Encoding.Unicode.GetBytes("^XA");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^PRC");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^LL240");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^MD6");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^MMT");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^MNY");
FS.Write(Buff, 0, Buff.Length);
//Buff = System.Text.Encoding.Unicode.GetBytes("^XA");
//FS.Write(Buff, 0, Buff.Length);
String ItemName;
ItemName = listBox2.Text.ToString();
//''''''''''''''''''''''''''''''''''''''''''''''''''LEFT,TOP,FONTHEIGHT,FONTSIZE,
Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,07^A0N,18,33^CI13^FR^FD" + "T.T.Devasthanams" + "^FS");
FS.Write(Buff, 0, Buff.Length);
//Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,70^A0N,22,33^CI13^FR^FD"+ItemName+"^FS");
//FS.Write(ConvertStringToByteArray("^FO10,70^A0N,22,33^CI13^FR^FD" + ItemName + "^FS"), 0, Buff.Length);
//Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,90^A0N,22,23^CI13^FR^FD" + Dt + "^FS");
//FS.Write(Buff, 0, Buff.Length);
if (dup == true)
{
Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,110^A0N,27,23^CI13^FR^FD" + "DUPLICATE-TICKET" + "^FS");
FS.Write(Buff, 0, Buff.Length);
}
else
{
Buff = System.Text.Encoding.Unicode.GetBytes("^FO10,110^A0N,27,23^CI13^FR^FD" + "BarcodeNo." + HumanReadable + "^FS");
FS.Write(Buff, 0, Buff.Length);
}
Buff = System.Text.Encoding.Unicode.GetBytes("^BY2,3.0^FO55,134^BCN,80,N,Y,N^FR^FD>;" + BarcodeID + "^FS");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^PQ1,0,0,N");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^XZ");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^FX End of job");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^XA");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^IDR:ID*.*");
FS.Write(Buff, 0, Buff.Length);
Buff = System.Text.Encoding.Unicode.GetBytes("^XZ");
FS.Write(Buff, 0, Buff.Length);

//bool bSuccess = false;
//IntPtr pUnmanagedBytes = new IntPtr(0);
//int nLength;
//nLength = Convert.ToInt32(1024);
//Buff = Br.ReadBytes(nLength);
//pUnmanagedBytes =
//Marshal.Copy(Buff, 0, pUnmanagedBytes, nLength);
Br.Close();
FS.Close();
Lbl_cnt = Lbl_cnt + 1;

}
}



}

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Please use code tags like this: [code.] put your code here [/code.] but don't add the periods, that will make it much easier for people to read your code, and much more likely to give an answer. ;)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users