Hice una clase para poder imprimir la información del depósito realizado en una máquina. Lo hace todo bien excepto las letras que vienen con acentos, me manda esa letra como un símbolo. Este es el código:
Namespace DEPO
{
class ModulePrint
{
public static int ImageSizeX { get; private set; }
public static int ImageSizeY { get; private set; }
SerialPort ComPort = new SerialPort();
private bool isFirstLine=true;
public ModulePrint( )
{
if ( ComPort.IsOpen )
ComPort.Close( );
ComPort.PortName = Convert.ToString( "COM5" ); //read COM number
ComPort.BaudRate = Convert.ToInt32( 9600 );
ComPort.DataBits = Convert.ToInt16( 8 );
ComPort.StopBits = (StopBits) Enum.Parse( typeof( StopBits ) , "One" );
ComPort.Handshake = (Handshake) Enum.Parse( typeof( Handshake ) , "None" );
ComPort.Parity = (Parity) Enum.Parse( typeof( Parity ) , "None" );
ComPort.RtsEnable = true;
ComPort.DtrEnable = true;
ComPort.WriteTimeout = 2000;
ComPort.Encoding = Encoding.UTF8;
ComPort.Open( );
ComPort.DiscardOutBuffer( );
isFirstLine = true;
}
public void AddHeaderLine(string line)
{
if ( line.Length > 24 )
{
ComPort.Write( line.Substring( 0 , 24 ) );
ComPort.Write( line.Substring( 24 ) );
ComPort.Write( Convert.ToChar( 0x0D ).ToString() + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
else
{
ComPort.Write( line );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
}
public void AddFooterLine( string line )
{
if ( line.Length > 24 )
{
ComPort.Write( line.Substring( 0 , 24 ) );
ComPort.Write( line.Substring( 25 ) );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
else
{
ComPort.Write( line );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
}
public void AddSubHeaderLine( string line )
{
if ( line.Length > 24 )
{
ComPort.Write( line.Substring( 0 , 24 ) );
ComPort.Write( line.Substring( 25 ) );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
else
{
ComPort.Write( line );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
}
public void AddItem_Mov( string cantidad , string item , string price )
{
int padTo = 8;
if ( isFirstLine )
{
string a = "Usuario";
string b = "Total";
string c = "Fecha";
int padSizeA = ( padTo - a.Length ) / 2;
a = a.Trim( ).PadLeft( a.Length + padSizeA ).PadRight( a.Length + 2 * padSizeA );
int padSizeB = ( padTo - b.Length ) / 2;
b = b.Trim( ).PadLeft( b.Length + padSizeB ).PadRight( b.Length + 2 * padSizeB );
int padSizeC = ( padTo - c.Length ) / 2;
c = c.Trim( ).PadLeft( c.Length + padSizeC ).PadRight( c.Length + 2 * padSizeC );
ComPort.Write( a + b + c );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
isFirstLine = false;
padTo = 6;
int padSizeCantidad = ( padTo - ( cantidad.Length == 1 ? 4 : cantidad.Length ) ) / 2;
cantidad = cantidad.Trim( ).PadLeft( ( cantidad.Length == 1 ? 4 : cantidad.Length ) + padSizeCantidad ).PadRight( ( cantidad.Length == 1 ? 4 : cantidad.Length ) + 2 * padSizeCantidad );
ComPort.Write( cantidad + item.PadLeft( 8 , ' ' ) + price.PadLeft( 6 , ' ' ) );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
public void AddItem( string cantidad , string item , string price )
{
int padTo = 8;
if ( isFirstLine )
{
string a = "Cant.";
string b = "Denom.";
string c = "Subtot.";
int padSizeA = ( padTo - a.Length ) / 2;
a = a.Trim( ).PadLeft( a.Length + padSizeA ).PadRight( a.Length + 2 * padSizeA );
int padSizeB = ( padTo - b.Length ) / 2;
b = b.Trim( ).PadLeft( b.Length + padSizeB ).PadRight( b.Length + 2 * padSizeB );
int padSizeC = ( padTo - c.Length ) / 2;
c = c.Trim( ).PadLeft( c.Length + padSizeC ).PadRight( c.Length + 2 * padSizeC );
ComPort.Write( a + b + c );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
isFirstLine = false;
padTo = 6;
int padSizeCantidad = ( padTo - ( cantidad.Length == 1 ? 4 : cantidad.Length ) ) / 2;
cantidad = cantidad.Trim( ).PadLeft( ( cantidad.Length == 1 ? 4 : cantidad.Length ) + padSizeCantidad ).PadRight( ( cantidad.Length == 1 ? 4 : cantidad.Length ) + 2 * padSizeCantidad );
ComPort.Write( cantidad + item.PadLeft( 6 , ' ' ) + price.PadLeft( 12 , ' ' ) );
ComPort.Write( Convert.ToChar( 0x0D ).ToString( ) + Convert.ToChar( 0x0A ).ToString( ) );//CrLf
}
public void FooterImage( string PathImg )
{
PathImg =Application.StartupPath + "\\" + PathImg;
Bitmap imageOri = new Bitmap( PathImg );
ImageSizeX = 192;
ImageSizeY = (int) ( (double) imageOri.Height / (double) ( (double) imageOri.Width / (double) ImageSizeX ) );
Bitmap image = new Bitmap( imageOri , new Size( ImageSizeX , ImageSizeY ) );
byte[] strstrPx = new byte[500];
for ( int z = 0; z < ImageSizeY + ( ImageSizeY % 8 ); z = z + 8 )
{
int cnt = 0;
strstrPx[cnt++] = 0x1B;
strstrPx[cnt++] = 0x2A;
strstrPx[cnt++] = 0x0;
strstrPx[cnt++] = 0xC0;
strstrPx[cnt++] = ( 0xC0 >> 8 );
ComPort.Write( strstrPx , 0 , cnt );
cnt = 0;
for ( int x = 0; x < ImageSizeX; x++ )
{
int px = 0;
for ( int y = 0; y < 8; y++ )
{
if ( z + y >= ImageSizeY )
{
px += 0;
}
else
{
Color pixelColor = image.GetPixel( x , z + y );
if ( pixelColor.R < 127 )
{
if ( y == 7 )
px += 1;
else
px += ( 2 << ( 7 - y - 1 ) );
}
}
}
strstrPx[cnt++] = Convert.ToByte( px );
}
ComPort.Write( strstrPx , 0 , cnt );
}
}
public void HeaderImage( string PathImg )
{
PathImg = Application.StartupPath + "\\" + PathImg;
Bitmap imageOri = new Bitmap( PathImg );
ImageSizeX = 192;
ImageSizeY = (int) ( (double) imageOri.Height / (double) ( (double) imageOri.Width / (double) ImageSizeX ) );
Bitmap image = new Bitmap( imageOri , new Size( ImageSizeX , ImageSizeY ) );
byte[] strstrPx = new byte[500];
for ( int z = 0; z < ImageSizeY + ( ImageSizeY % 8 ); z = z + 8 )
{
int cnt = 0;
strstrPx[cnt++] = 0x1B;
strstrPx[cnt++] = 0x2A;
strstrPx[cnt++] = 0x0;
strstrPx[cnt++] = 0xC0;
strstrPx[cnt++] = ( 0xC0 >> 8 );
ComPort.Write( strstrPx , 0 , cnt );
cnt = 0;
for ( int x = 0; x < ImageSizeX; x++ )
{
int px = 0;
for ( int y = 0; y < 8; y++ )
{
if ( z + y >= ImageSizeY )
{
px += 0;
}
else
{
Color pixelColor = image.GetPixel( x , z + y );
if ( pixelColor.R < 127 )
{
if ( y == 7 )
px += 1;
else
px += ( 2 << ( 7 - y - 1 ) );
}
}
}
strstrPx[cnt++] = Convert.ToByte( px );
}
ComPort.Write( strstrPx , 0 , cnt );
}
}
public void PrintTicket( string impresora , string p_folioimpresion )
{
ComPort.Write( Convert.ToChar( 0x1B ).ToString( ) + Convert.ToChar( 0x64 ).ToString( ) + Convert.ToChar( 0x04 ).ToString( ) );
ComPort.Close( );
}
internal void Dispose( )
{
throw new NotImplementedException( );
}
}
}
Según yo debería salir ya codificada la información con ComPort.Encoding = Encoding.UTF8;
pero estoy dudando si es cosa del puerto COM5 o estoy convirtiendo mal en mi código. Alguien que me ayude por favor.