Saturday, May 14, 2011

Serial Communication in java

import javax.comm.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class SerialComm implements SerialPortEventListener
{
CommPort thePort;
SerialPort myPort;
CommPortIdentifier cpi;
InputStream in;
OutputStream out;
String req,index,res1,sql,no;
String res=" ";

String s[]=new String[10];
int z=0;
public SerialComm() throws IOException
{
try
{
cpi = CommPortIdentifier.getPortIdentifier("COM36");
}
catch (NoSuchPortException e)
{
System.out.println("CPI");
}
try
{
thePort=cpi.open("mobile",30000);
myPort=(SerialPort)thePort;
}
catch (PortInUseException e)
{
System.out.println("mobile");
}
try
{
myPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e)
{
System.out.println("SetParam");
}
try
{
in=thePort.getInputStream();
}
catch (Exception e)
{
myPort.close();
System.out.println("InputStream");
}
try
{
out=thePort.getOutputStream();
}
catch (Exception e)
{
myPort.close();
System.out.println("outputStream");

}
try
{
myPort.notifyOnDataAvailable(true);
myPort.notifyOnBreakInterrupt(true);
}
catch (Exception e)
{
System.out.println("DATA avail");
}
try
{
myPort.enableReceiveTimeout(30);
}
catch (UnsupportedCommOperationException e)
{
System.out.println("enable receive");
}
try
{
myPort.addEventListener(this);
}
catch (Exception e)
{
System.out.println("ADD EVENT LISTEN");
}

System.out.println("Opened COM1");

}

public void serialEvent(SerialPortEvent ev)
{

StringBuffer inputBuffer = new StringBuffer();
int newData = 0;
int c=0,dat=0;
char h=0;

switch(ev.getEventType())
{
case SerialPortEvent.DATA_AVAILABLE:
try
{
byte[] b2 = new byte[9];
in.read(b2);

dat = in.read();

dat = dat << 8;

dat += in.read();




closePort();

System.out.println("Closed");

}catch (Exception e)
{
System.out.println("NEWDATA");
System.out.println(e);
return;
}
break;
case SerialPortEvent.BI:System.out.println(" Received Break ");
break;
}
}


class CloseThread extends Thread
{
@Override
public void run()
{
myPort.removeEventListener();
myPort.close();
}
}


public void closePort()
{

try
{
if (myPort != null)
{

myPort.getInputStream().close();
myPort.getOutputStream().close();
new CloseThread().start();

}

}
catch (Exception e) {}
}

}

Thursday, April 22, 2010

Declare string variables using class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace train
{
public class Class1
{
public static string tkt;
public static string trainno;
public static string date;
public static string Class;
public static string Seatno;
public static string Name;
public static string Age;
public static string Gender;
public static string Boardingfrom;
public static string Destination;
public static string Amount;
public static string Coach;


}

}

Saturday, February 20, 2010

Data Reading method in C# using DataRow

MySqlDataAdapter adp = new MySqlDataAdapter("select * from lim", cn);
DataSet ds = new DataSet();
adp.Fill(ds);
DataRow dr = ds.Tables[0].Rows[0];
DataRow dr1 = ds.Tables[0].Rows[1];
DataRow dr2 = ds.Tables[0].Rows[2];

radioButton1.Text = dr.ItemArray.GetValue(0).ToString();
radioButton2.Text = dr1.ItemArray.GetValue(0).ToString();
radioButton3.Text = dr2.ItemArray.GetValue(0).ToString();

MySql Connection String in C#

string str = "server=BITZ;password=tt;uid=root;Initial Catalog=billing";
MySqlConnection cn = new MySqlConnection(str);
cn.Open();

MIFARE Communication with c#

byte []autoscan=new byte[]{0x01, 0x00, 0x00, 0x00, 0xc0, 0x71,0x01, 0x00, 0x01, 0x00, 0x50, 0x70,0x01, 0x00, 0x3f, 0x00, 0x30, 0x60 };
byte[] crctable = new byte[512];
byte a, b,aa,ba;
string j=null, r=null, n=null,s;
byte[] recv3 = new byte[50];
int i,r1=0,k = 0,v=0,q=0,ka=0,vy=0,bu=0,bankupdate=0,flag=0,z=0,l;
int m = 0,w=0;
string p, t;
int result = 0;
byte[] crc = new byte[8];
string user;
MySqlCommand cmd;
MySqlConnection cn;
MySqlDataReader rd;
byte str1, str2;
string c, d, e, f,nn;
public Form3(string username )
{
user = username.ToString();
InitializeComponent();
crctable[0] = 0x0;
crctable[1] = 0xC1;
crctable[2] = 0x81;
crctable[3] = 0x40;
crctable[4] = 0x1;
crctable[5] = 0xC0;
crctable[6] = 0x80;
crctable[7] = 0x41;
crctable[8] = 0x1;
crctable[9] = 0xC0;
crctable[10] = 0x80;
crctable[11] = 0x41;
crctable[12] = 0x0;
crctable[13] = 0xC1;
crctable[14] = 0x81;
crctable[15] = 0x40;
crctable[16] = 0x1;
crctable[17] = 0xC0;
crctable[18] = 0x80;
crctable[19] = 0x41;
crctable[20] = 0x0;
crctable[21] = 0xC1;
crctable[22] = 0x81;
crctable[23] = 0x40;
crctable[24] = 0x0;
crctable[25] = 0xC1;
crctable[26] = 0x81;
crctable[27] = 0x40;
crctable[28] = 0x1;
crctable[29] = 0xC0;
crctable[30] = 0x80;
crctable[31] = 0x41;
crctable[32] = 0x1;
crctable[33] = 0xC0;
crctable[34] = 0x80;
crctable[35] = 0x41;
crctable[36] = 0x0;
crctable[37] = 0xC1;
crctable[38] = 0x81;
crctable[39] = 0x40;
crctable[40] = 0x0;
crctable[41] = 0xC1;
crctable[42] = 0x81;
crctable[43] = 0x40;
crctable[44] = 0x1;
crctable[45] = 0xC0;
crctable[46] = 0x80;
crctable[47] = 0x41;
crctable[48] = 0x0;
crctable[49] = 0xC1;
crctable[50] = 0x81;
crctable[51] = 0x40;
crctable[52] = 0x1;
crctable[53] = 0xC0;
crctable[54] = 0x80;
crctable[55] = 0x41;
crctable[56] = 0x1;
crctable[57] = 0xC0;
crctable[58] = 0x80;
crctable[59] = 0x41;
crctable[60] = 0x0;
crctable[61] = 0xC1;
crctable[62] = 0x81;
crctable[63] = 0x40;
crctable[64] = 0x1;
crctable[65] = 0xC0;
crctable[66] = 0x80;
crctable[67] = 0x41;
crctable[68] = 0x0;
crctable[69] = 0xC1;
crctable[70] = 0x81;
crctable[71] = 0x40;
crctable[72] = 0x0;
crctable[73] = 0xC1;
crctable[74] = 0x81;
crctable[75] = 0x40;
crctable[76] = 0x1;
crctable[77] = 0xC0;
crctable[78] = 0x80;
crctable[79] = 0x41;
crctable[80] = 0x0;
crctable[81] = 0xC1;
crctable[82] = 0x81;
crctable[83] = 0x40;
crctable[84] = 0x1;
crctable[85] = 0xC0;
crctable[86] = 0x80;
crctable[87] = 0x41;
crctable[88] = 0x1;
crctable[89] = 0xC0;
crctable[90] = 0x80;
crctable[91] = 0x41;
crctable[92] = 0x0;
crctable[93] = 0xC1;
crctable[94] = 0x81;
crctable[95] = 0x40;
crctable[96] = 0x0;
crctable[97] = 0xC1;
crctable[98] = 0x81;
crctable[99] = 0x40;
crctable[100] = 0x1;
crctable[101] = 0xC0;
crctable[102] = 0x80;
crctable[103] = 0x41;
crctable[104] = 0x1;
crctable[105] = 0xC0;
crctable[106] = 0x80;
crctable[107] = 0x41;
crctable[108] = 0x0;
crctable[109] = 0xC1;
crctable[110] = 0x81;
crctable[111] = 0x40;
crctable[112] = 0x1;
crctable[113] = 0xC0;
crctable[114] = 0x80;
crctable[115] = 0x41;
crctable[116] = 0x0;
crctable[117] = 0xC1;
crctable[118] = 0x81;
crctable[119] = 0x40;
crctable[120] = 0x0;
crctable[121] = 0xC1;
crctable[122] = 0x81;
crctable[123] = 0x40;
crctable[124] = 0x1;
crctable[125] = 0xC0;
crctable[126] = 0x80;
crctable[127] = 0x41;
crctable[128] = 0x1;
crctable[129] = 0xC0;
crctable[130] = 0x80;
crctable[131] = 0x41;
crctable[132] = 0x0;
crctable[133] = 0xC1;
crctable[134] = 0x81;
crctable[135] = 0x40;
crctable[136] = 0x0;
crctable[137] = 0xC1;
crctable[138] = 0x81;
crctable[139] = 0x40;
crctable[140] = 0x1;
crctable[141] = 0xC0;
crctable[142] = 0x80;
crctable[143] = 0x41;
crctable[144] = 0x0;
crctable[145] = 0xC1;
crctable[146] = 0x81;
crctable[147] = 0x40;
crctable[148] = 0x1;
crctable[149] = 0xC0;
crctable[150] = 0x80;
crctable[151] = 0x41;
crctable[152] = 0x1;
crctable[153] = 0xC0;
crctable[154] = 0x80;
crctable[155] = 0x41;
crctable[156] = 0x0;
crctable[157] = 0xC1;
crctable[158] = 0x81;
crctable[159] = 0x40;
crctable[160] = 0x0;
crctable[161] = 0xC1;
crctable[162] = 0x81;
crctable[163] = 0x40;
crctable[164] = 0x1;
crctable[165] = 0xC0;
crctable[166] = 0x80;
crctable[167] = 0x41;
crctable[168] = 0x1;
crctable[169] = 0xC0;
crctable[170] = 0x80;
crctable[171] = 0x41;
crctable[172] = 0x0;
crctable[173] = 0xC1;
crctable[174] = 0x81;
crctable[175] = 0x40;
crctable[176] = 0x1;
crctable[177] = 0xC0;
crctable[178] = 0x80;
crctable[179] = 0x41;
crctable[180] = 0x0;
crctable[181] = 0xC1;
crctable[182] = 0x81;
crctable[183] = 0x40;
crctable[184] = 0x0;
crctable[185] = 0xC1;
crctable[186] = 0x81;
crctable[187] = 0x40;
crctable[188] = 0x1;
crctable[189] = 0xC0;
crctable[190] = 0x80;
crctable[191] = 0x41;
crctable[192] = 0x0;
crctable[193] = 0xC1;
crctable[194] = 0x81;
crctable[195] = 0x40;
crctable[196] = 0x1;
crctable[197] = 0xC0;
crctable[198] = 0x80;
crctable[199] = 0x41;
crctable[200] = 0x1;
crctable[201] = 0xC0;
crctable[202] = 0x80;
crctable[203] = 0x41;
crctable[204] = 0x0;
crctable[205] = 0xC1;
crctable[206] = 0x81;
crctable[207] = 0x40;
crctable[208] = 0x1;
crctable[209] = 0xC0;
crctable[210] = 0x80;
crctable[211] = 0x41;
crctable[212] = 0x0;
crctable[213] = 0xC1;
crctable[214] = 0x81;
crctable[215] = 0x40;
crctable[216] = 0x0;
crctable[217] = 0xC1;
crctable[218] = 0x81;
crctable[219] = 0x40;
crctable[220] = 0x1;
crctable[221] = 0xC0;
crctable[222] = 0x80;
crctable[223] = 0x41;
crctable[224] = 0x1;
crctable[225] = 0xC0;
crctable[226] = 0x80;
crctable[227] = 0x41;
crctable[228] = 0x0;
crctable[229] = 0xC1;
crctable[230] = 0x81;
crctable[231] = 0x40;
crctable[232] = 0x0;
crctable[233] = 0xC1;
crctable[234] = 0x81;
crctable[235] = 0x40;
crctable[236] = 0x1;
crctable[237] = 0xC0;
crctable[238] = 0x80;
crctable[239] = 0x41;
crctable[240] = 0x0;
crctable[241] = 0xC1;
crctable[242] = 0x81;
crctable[243] = 0x40;
crctable[244] = 0x1;
crctable[245] = 0xC0;
crctable[246] = 0x80;
crctable[247] = 0x41;
crctable[248] = 0x1;
crctable[249] = 0xC0;
crctable[250] = 0x80;
crctable[251] = 0x41;
crctable[252] = 0x0;
crctable[253] = 0xC1;
crctable[254] = 0x81;
crctable[255] = 0x40;
crctable[256] = 0x0;
crctable[257] = 0xC0;
crctable[258] = 0xC1;
crctable[259] = 0x1;
crctable[260] = 0xC3;
crctable[261] = 0x3;
crctable[262] = 0x2;
crctable[263] = 0xC2;
crctable[264] = 0xC6;
crctable[265] = 0x6;
crctable[266] = 0x7;
crctable[267] = 0xC7;
crctable[268] = 0x5;
crctable[269] = 0xC5;
crctable[270] = 0xC4;
crctable[271] = 0x4;
crctable[272] = 0xCC;
crctable[273] = 0xC;
crctable[274] = 0xD;
crctable[275] = 0xCD;
crctable[276] = 0xF;
crctable[277] = 0xCF;
crctable[278] = 0xCE;
crctable[279] = 0xE;
crctable[280] = 0xA;
crctable[281] = 0xCA;
crctable[282] = 0xCB;
crctable[283] = 0xB;
crctable[284] = 0xC9;
crctable[285] = 0x9;
crctable[286] = 0x8;
crctable[287] = 0xC8;
crctable[288] = 0xD8;
crctable[289] = 0x18;
crctable[290] = 0x19;
crctable[291] = 0xD9;
crctable[292] = 0x1B;
crctable[293] = 0xDB;
crctable[294] = 0xDA;
crctable[295] = 0x1A;
crctable[296] = 0x1E;
crctable[297] = 0xDE;
crctable[298] = 0xDF;
crctable[299] = 0x1F;
crctable[300] = 0xDD;
crctable[301] = 0x1D;
crctable[302] = 0x1C;
crctable[303] = 0xDC;
crctable[304] = 0x14;
crctable[305] = 0xD4;
crctable[306] = 0xD5;
crctable[307] = 0x15;
crctable[308] = 0xD7;
crctable[309] = 0x17;
crctable[310] = 0x16;
crctable[311] = 0xD6;
crctable[312] = 0xD2;
crctable[313] = 0x12;
crctable[314] = 0x13;
crctable[315] = 0xD3;
crctable[316] = 0x11;
crctable[317] = 0xD1;
crctable[318] = 0xD0;
crctable[319] = 0x10;
crctable[320] = 0xF0;
crctable[321] = 0x30;
crctable[322] = 0x31;
crctable[323] = 0xF1;
crctable[324] = 0x33;
crctable[325] = 0xF3;
crctable[326] = 0xF2;
crctable[327] = 0x32;
crctable[328] = 0x36;
crctable[329] = 0xF6;
crctable[330] = 0xF7;
crctable[331] = 0x37;
crctable[332] = 0xF5;
crctable[333] = 0x35;
crctable[334] = 0x34;
crctable[335] = 0xF4;
crctable[336] = 0x3C;
crctable[337] = 0xFC;
crctable[338] = 0xFD;
crctable[339] = 0x3D;
crctable[340] = 0xFF;
crctable[341] = 0x3F;
crctable[342] = 0x3E;
crctable[343] = 0xFE;
crctable[344] = 0xFA;
crctable[345] = 0x3A;
crctable[346] = 0x3B;
crctable[347] = 0xFB;
crctable[348] = 0x39;
crctable[349] = 0xF9;
crctable[350] = 0xF8;
crctable[351] = 0x38;
crctable[352] = 0x28;
crctable[353] = 0xE8;
crctable[354] = 0xE9;
crctable[355] = 0x29;
crctable[356] = 0xEB;
crctable[357] = 0x2B;
crctable[358] = 0x2A;
crctable[359] = 0xEA;
crctable[360] = 0xEE;
crctable[361] = 0x2E;
crctable[362] = 0x2F;
crctable[363] = 0xEF;
crctable[364] = 0x2D;
crctable[365] = 0xED;
crctable[366] = 0xEC;
crctable[367] = 0x2C;
crctable[368] = 0xE4;
crctable[369] = 0x24;
crctable[370] = 0x25;
crctable[371] = 0xE5;
crctable[372] = 0x27;
crctable[373] = 0xE7;
crctable[374] = 0xE6;
crctable[375] = 0x26;
crctable[376] = 0x22;
crctable[377] = 0xE2;
crctable[378] = 0xE3;
crctable[379] = 0x23;
crctable[380] = 0xE1;
crctable[381] = 0x21;
crctable[382] = 0x20;
crctable[383] = 0xE0;
crctable[384] = 0xA0;
crctable[385] = 0x60;
crctable[386] = 0x61;
crctable[387] = 0xA1;
crctable[388] = 0x63;
crctable[389] = 0xA3;
crctable[390] = 0xA2;
crctable[391] = 0x62;
crctable[392] = 0x66;
crctable[393] = 0xA6;
crctable[394] = 0xA7;
crctable[395] = 0x67;
crctable[396] = 0xA5;
crctable[397] = 0x65;
crctable[398] = 0x64;
crctable[399] = 0xA4;
crctable[400] = 0x6C;
crctable[401] = 0xAC;
crctable[402] = 0xAD;
crctable[403] = 0x6D;
crctable[404] = 0xAF;
crctable[405] = 0x6F;
crctable[406] = 0x6E;
crctable[407] = 0xAE;
crctable[408] = 0xAA;
crctable[409] = 0x6A;
crctable[410] = 0x6B;
crctable[411] = 0xAB;
crctable[412] = 0x69;
crctable[413] = 0xA9;
crctable[414] = 0xA8;
crctable[415] = 0x68;
crctable[416] = 0x78;
crctable[417] = 0xB8;
crctable[418] = 0xB9;
crctable[419] = 0x79;
crctable[420] = 0xBB;
crctable[421] = 0x7B;
crctable[422] = 0x7A;
crctable[423] = 0xBA;
crctable[424] = 0xBE;
crctable[425] = 0x7E;
crctable[426] = 0x7F;
crctable[427] = 0xBF;
crctable[428] = 0x7D;
crctable[429] = 0xBD;
crctable[430] = 0xBC;
crctable[431] = 0x7C;
crctable[432] = 0xB4;
crctable[433] = 0x74;
crctable[434] = 0x75;
crctable[435] = 0xB5;
crctable[436] = 0x77;
crctable[437] = 0xB7;
crctable[438] = 0xB6;
crctable[439] = 0x76;
crctable[440] = 0x72;
crctable[441] = 0xB2;
crctable[442] = 0xB3;
crctable[443] = 0x73;
crctable[444] = 0xB1;
crctable[445] = 0x71;
crctable[446] = 0x70;
crctable[447] = 0xB0;
crctable[448] = 0x50;
crctable[449] = 0x90;
crctable[450] = 0x91;
crctable[451] = 0x51;
crctable[452] = 0x93;
crctable[453] = 0x53;
crctable[454] = 0x52;
crctable[455] = 0x92;
crctable[456] = 0x96;
crctable[457] = 0x56;
crctable[458] = 0x57;
crctable[459] = 0x97;
crctable[460] = 0x55;
crctable[461] = 0x95;
crctable[462] = 0x94;
crctable[463] = 0x54;
crctable[464] = 0x9C;
crctable[465] = 0x5C;
crctable[466] = 0x5D;
crctable[467] = 0x9D;
crctable[468] = 0x5F;
crctable[469] = 0x9F;
crctable[470] = 0x9E;
crctable[471] = 0x5E;
crctable[472] = 0x5A;
crctable[473] = 0x9A;
crctable[474] = 0x9B;
crctable[475] = 0x5B;
crctable[476] = 0x99;
crctable[477] = 0x59;
crctable[478] = 0x58;
crctable[479] = 0x98;
crctable[480] = 0x88;
crctable[481] = 0x48;
crctable[482] = 0x49;
crctable[483] = 0x89;
crctable[484] = 0x4B;
crctable[485] = 0x8B;
crctable[486] = 0x8A;
crctable[487] = 0x4A;
crctable[488] = 0x4E;
crctable[489] = 0x8E;
crctable[490] = 0x8F;
crctable[491] = 0x4F;
crctable[492] = 0x8D;
crctable[493] = 0x4D;
crctable[494] = 0x4C;
crctable[495] = 0x8C;
crctable[496] = 0x44;
crctable[497] = 0x84;
crctable[498] = 0x85;
crctable[499] = 0x45;
crctable[500] = 0x87;
crctable[501] = 0x47;
crctable[502] = 0x46;
crctable[503] = 0x86;
crctable[504] = 0x82;
crctable[505] = 0x42;
crctable[506] = 0x43;
crctable[507] = 0x83;
crctable[508] = 0x41;
crctable[509] = 0x81;
crctable[510] = 0x80;
crctable[511] = 0x40;

}
public int crc16(byte[] modbusframe, int Length)
{

int i;
int index;
int crc_Low = 0xFF;
int crc_High = 0xFF;

for (i = 0; i < Length; i++)
{
index = crc_High ^ (char)modbusframe[i];
crc_High = crc_Low ^ crctable[index];
crc_Low = (byte)crctable[index + 256];
}

return crc_High * 256 + crc_Low;
}
private void card(object sender, EventArgs e)
{
MessageBox.Show("place the card");
}
private void cardid(object sender, EventArgs e)
{
textBox2.Text = c + d +nn+ f;
}
private void balance(object sender, EventArgs e)
{
label1.Visible = true;
label1.Text = "Your Current Balance in Card is:" + result.ToString();

}
private void Form3_Load(object sender, EventArgs e)
{
serialPort1.Open();


serialPort1.Write(autoscan, 0, 18);



}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{

switch (z)
{
case 0:
{
Thread.Sleep(10);
byte[] recv = new byte[25];
serialPort1.Read(recv, 0, 25);
Thread.Sleep(1000);
k = Convert.ToInt32(recv[2]);
if (k == 6)
{
byte[] ok = new byte[] { 0x01, 0x00, 0x3f, 0x01, 0x00, 0xb8, 0x31 };
serialPort1.DiscardInBuffer();
serialPort1.Write(ok, 0, 7);
Thread.Sleep(1000);
z++;
}
else
{

byte[] autoscan = new byte[] { 0x01, 0x00, 0x00, 0x00, 0xc0, 0x71, 0x01, 0x00, 0x01, 0x00, 0x50, 0x70, 0x01, 0x00, 0x3f, 0x00, 0x30, 0x60 };
serialPort1.Write(autoscan, 0, 18);
z = 0;
}
break;
}

case 1:
{
byte[] resp_ok = new byte[50];
Thread.Sleep(1000);
serialPort1.Read(resp_ok, 0, 10);
Thread.Sleep(1000);
k = Convert.ToInt32(resp_ok[2]);
if (k == 6)
{
MessageBox.Show("Place your card");
byte[] request = new byte[] { 0x01, 0x00, 0x20, 0x00, 0x00, 0x68 };
serialPort1.DiscardInBuffer();
serialPort1.Write(request, 0, 6);
Thread.Sleep(1000);
z++;
}
else
{

byte[] autoscan = new byte[] { 0x01, 0x00, 0x00, 0x00, 0xc0, 0x71, 0x01, 0x00, 0x01, 0x00, 0x50, 0x70, 0x01, 0x00, 0x3f, 0x00, 0x30, 0x60 };
serialPort1.Write(autoscan, 0, 18);
z = 0;
}
break;
}
case 2:
{
byte[] recv2 = new byte[8];
serialPort1.Read(recv2, 0, 8);
Thread.Sleep(1000);
k = Convert.ToInt32(recv2[2]);

if (k == 6)
{
MessageBox.Show("card ok");
byte[] cardid = new byte[] { 0x01, 0x00, 0x21, 0x00, 0x90, 0x69 };
serialPort1.Write(cardid, 0, 6);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}
case 3:
{
byte[] recv3 = new byte[10];
serialPort1.Read(recv3, 0, 10);
k = Convert.ToInt32(recv3[2]);
if (k == 6)
{
int x = Convert.ToInt32(recv3[4]);
int y = Convert.ToInt32(recv3[5]);
int l = Convert.ToInt32(recv3[6]);
int n = Convert.ToInt32(recv3[7]);
c = x.ToString("X");
d = y.ToString("X");
nn = l.ToString("X");
f = n.ToString("X");
this.Invoke(new EventHandler(cardid));
Thread.Sleep(100);
byte[] crc = new byte[] { 0x00, 0x22, 0x04, recv3[4], recv3[5], recv3[6], recv3[7] };
byte[] crc_result = BitConverter.GetBytes(crc16(crc, 7));
byte[] send_cardid = new byte[] { 0x01, 0x00, 0x22, 0x04, recv3[4], recv3[5], recv3[6], recv3[7], crc_result[0], crc_result[1] };
serialPort1.DiscardInBuffer();
serialPort1.Write(send_cardid, 0, 10);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}

case 4:
{
byte[] recv4 = new byte[8];
serialPort1.Read(recv4, 0, 8);
k = Convert.ToInt32(recv4[2]);
if (k == 6)
{
byte[] authent = new byte[] { 0x01, 0x00, 0x23, 0x02, 0x60, 0x00, 0x44, 0xa6 };
serialPort1.DiscardInBuffer();
serialPort1.Write(authent, 0, 8);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}
case 5:
{
byte[] recv5 = new byte[8];
serialPort1.Read(recv5, 0, 8);
k = Convert.ToInt32(recv5[2]);

if (k == 6)
{
MessageBox.Show("card checked");
byte[] valueread = new byte[] { 0x01, 0x00, 0x27, 0x01, 0x01, 0x7f, 0x70 };
serialPort1.DiscardInBuffer();
serialPort1.Write(valueread, 0, 7);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}
case 6:
{
byte[] recv6 = new byte[20];
serialPort1.Read(recv6, 0, 20);
k = Convert.ToInt32(recv6[2]);

if (k == 6)
{
m = Convert.ToInt32(recv6[6]);
w = Convert.ToInt32(recv6[7]);
result = w + (m * 255);
this.Invoke(new EventHandler(balance));
z++;
}
break;
}
case 7:
{
if (v == 1)
{
byte[] recv7 = new byte[29];
Thread.Sleep(100);
serialPort1.Read(recv7, 0, 29);
k = Convert.ToInt32(recv7[2]);

if (k == 6)
{

z++;
}
else
{


}

byte[] halt = new byte[] { 0x01, 0x00, 0x2a, 0x00, 0xa0, 0x6e };
serialPort1.Write(halt, 0, 6);
}
break;

}
case 8:
{
byte[] halt1 = new byte[20];
serialPort1.Read(halt1, 0, 20);
k = Convert.ToInt32(halt1[2]);

if (k == 6)
{


}
break;

}

}
}

Monday, August 10, 2009

Change Date Format of System using VB 6.0

If you want to change date format of your system to "dd/MM/yy" format:
First add the below code to Module name "modTime":

Option Explicit


Public Const LOCALE_SLANGUAGE As Long = &H2
Public Const LOCALE_SSHORTDATE As Long = &H1F

Public Const DATE_LONGDATE As Long = &H2
Public Const DATE_SHORTDATE As Long = &H1

Public Const HWND_BROADCAST As Long = &HFFFF&
Public Const WM_SETTINGCHANGE As Long = &H1A

Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Public Declare Function EnumDateFormats Lib "kernel32" _
Alias "EnumDateFormatsA" _
(ByVal lpDateFmtEnumProc As Long, _
ByVal Locale As Long, _
ByVal dwFlags As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)

Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long

Public Declare Function GetLocaleInfo Lib "kernel32" _
Alias "GetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cchData As Long) As Long

Public Declare Function SetLocaleInfo Lib "kernel32" _
Alias "SetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String) As Long


Public Function fGetUserLocaleInfo(ByVal lLocaleID As Long, _
ByVal lLCType As Long) As String

Dim sReturn As String
Dim lReturn As Long
lReturn = GetLocaleInfo(lLocaleID, lLCType, sReturn, Len(sReturn))
If lReturn Then

sReturn = Space$(lReturn)
If lReturn Then
fGetUserLocaleInfo = Left$(sReturn, lReturn - 1)
End If
End If

End Function


Public Function theEnumDates(lDateFormatString As Long) As Long
theEnumDates = 1
End Function


Private Function GetStrFromPointer(sString As Long) As String
Dim lPos As Long
Dim sBuffer As String
sBuffer = Space$(128)
Call CopyMemory(ByVal sBuffer, sString, ByVal Len(sBuffer))
lPos = InStr(sBuffer, Chr$(0))

If lPos Then
GetStrFromPointer = Left$(sBuffer, lPos - 1)
End If
End Function




Then Add Form with Command Button and add the below code in Button_click event:

Dim xCID As Long
Dim xChangedFormat As String

xCID = GetSystemDefaultLCID()

xChangedFormat = "dd/MM/yy"

If xChangedFormat <> "" Then

Call SetLocaleInfo(xCID, LOCALE_SSHORTDATE, xChangedFormat)

Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)

Call EnumDateFormats(AddressOf theEnumDates, xCID, DATE_SHORTDATE)
End If

Thursday, July 23, 2009

Use the toolbar control

Creating a Toolbar is not trivial. There are several steps to follow and some need to be performed in the right order. This HowTo's real usefulness is in its listing of the steps:

Press Ctrl-T to open the Tools dialog. Select Microsoft Windows Common Controls.
Add an ImageList control to the form.
Left-click on the ImageList and select the Properties command.
Click on the Images tab.
Click the Insert Picture button and find your button bitmaps. You can probably find the standard Windows 95 bitmaps in the Common/Graphics section of your computer's programming area. On my computer this is:
C:\Program Files\Microsoft Visual Studio\Common\Graphics\Bitmaps\TlBr_W95
When you have added all your button images, click the Ok button.
Add a Toolbar control to your form.
Left-click on the Toolbar and select the Properties command.
On the General tab, find the ImageList field (3rd from the top in my version). Select the ImageList control from step 2.
Click the Buttons tab.
Click Insert Button to add a button to the Toolbar. Fill in appropriate fields:
Caption appears on the button. For a normal button, leave this blank.
Key is passed to the Toolbar's event handler. This is one way you can tell which button was clicked. Enter a short key word here.
ToolTipText is displayed when the user lets the mouse rest over the button. Enter something short but helpful here. Remember that the user sees only a small picture if Caption is blank, so even very short text is useful here. For example, "Cut" is a good tip for the cut button.
Image is the index of the button's picture in the ImageList control. 0 means no image. 1 is the first image in the ImageList.
Experiment with the other button properties to see how they work.
When you have added all the buttons, click Ok.
Double click on the Toolbar to open the code editor in its ButtonClick event handler. This routine takes as a parameter a Button object representing the button pressed. Look at Button.Key to see which button was pressed.
Note that if you modify the ImageList after you have attached it to the Toolbar, you may mess up the Toolbar. For this reason, you should try to put every picture you will need in the ImageList first. Then initialize the Toolbar.

When the user clicks a button, the Toolbar control's ButtonClick event fires. You can use the buton's Key property to tell which button was clicked.



Private Sub Toolbar1_ButtonClick(ByVal Button As _
MSComctlLib.Button)
Select Case Button.Key
Case "Cut"
MsgBox "Cut"
Case "Copy"
MsgBox "Copy"
Case "Center"
MsgBox "Center"
Case "Snapshot"
MsgBox "Snapshot"
End Select
End Sub



Some additional tips from Gary German:

Toolbar graphics can be assigned from external files at runtime. But, the graphics on each toolbar must all be the same size, otherwise they'll look bad. Specifically, the first button to which a graphic is assigned "wins", and all following graphics will be stretched, if necessary, to fit in that same size.

Max number of buttons on a toolbar is 255.

If you need to create some buttons with graphics, and others with text only, don't use the button's .Caption property - it will cause all buttons to grow vertically because the text is placed below the missing graphic. Instead, use a routine to turn the text you want to display into a graphic, and assign this "graphic" to the image list, and then the toolbar button.

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...