[Reprint] Minghua IC card application C #

Source: Internet
Author: User

An IC (Integrated Circuit) card, also known as a smart card, has the ability to write data and store data. The content of the memory in the IC card can be read externally as needed, complete Information Processing and determination. Because it has an integrated circuit, it not only can store a large amount of information, but also has strong confidentiality, and has the characteristics of anti-interference, no wear, long life and so on. Therefore, it is widely used in various fields. The following two examples describe the simple application of IC card.

IC cards are media that carry application information and data. Blank IC cards cannot be used immediately. The IC card application system must be initialized and written into the system IC card and password, personal information, and application data. The following describes how to write data to an IC card. In this example, enter the data to be stored in the IC card in the "data" text box, and click "Write Data" to write the input data to the IC card. 13.6.

Technical Points
In this example, the Minghua IC Card Reader manufactured by Minghua in Shenzhen is used. After the driver is installed, the system can be used normally.

In this example, the mwic_32.dll library is called to read and write the IC card. The following describes functions related to IC card write operations.

(1) auto_init Function

This function is used to initialize an IC card reader. Syntax:

Public static extern int auto_init (INT port, int baud );

The parameters are described as follows.

L port: identifies the port number. The port number corresponding to COM1 is 0; the port number corresponding to com2 is 1, and so on.

L Baud: identifies the baud rate.

L return value: If Initialization is successful, the return value is the IC card device handle. If initialization fails, the return value is less than zero.

(2) setsc_md Function

This function is used to set the device password mode. Syntax:

Public static extern int setsc_md (INT icdev, int mode );

The parameters are described as follows.

L icdev: identifies the device handle, usually the return value of the auto_init function.

L mode: indicates the device password mode. If it is set to 0, the device password is valid. The device must verify the device password when powered on to operate the device. If the value is 1, the device password is invalid.

L return value: if the function is successfully executed, the return value is zero. Otherwise, the return value is smaller than zero.

(3) get_status Function

This function is used to obtain the current status of the device. Syntax:

Public static extern int16 get_status (INT icdev, int16 * State );

The parameters are described as follows.

L icdev: identifies the device handle, usually the return value of the auto_init function.

L state: used to receive the results returned by the function. If the value is 0, there is no card in the card reader. If the value is 1, there is a card in the card reader.

L return value: if the function is successfully executed, the return value is zero. Otherwise, the return value is smaller than zero.

(4) csc_4442 Function

This function is used to check the IC card password. Syntax:

Public static extern int16 csc_4442 (INT icdev, int Len, [financialas (unmanagedtype. lparray)] Byte [] p_string );

The parameters are described as follows.

L icdev: identifies the device handle, usually the return value of the auto_init function.

L Len: the length of the password. The value is 3.

L p_string: the password set.

L return value: if the function is successfully executed, the return value is zero. Otherwise, the return value is smaller than zero.

(5) swr_4442 Function

This function is used to write data to an IC card. Syntax:

Public static extern int swr_4442 (INT icdev, int offset, int Len, char * w_string );

The parameters are described as follows.

L icdev: identifies the device handle, usually the return value of the auto_init function.

L offset: the offset from 0 ~ 255.

L Len: Specifies the string length.

L w_string: identifies the written data.

(6) ic_exit Function

This function is used to disable the device port. Syntax:

Public static extern int ic_exit (INT icdev );

The parameters are described as follows.

L icdev: identifies the device handle, usually the return value of the auto_init function.

(7) dv_beep Function

This function makes the card reader beep. Syntax:

Public static extern int dv_beep (INT icdev, int time );

The parameters are described as follows.

L icdev: identifies the device handle, usually the return value of the auto_init function.

L time: indicates the duration of the beep attack. The unit is 10 milliseconds. Srd_4442 Function

This function is used to read data from an IC card. Syntax:

Public static extern int srd_4442 (INT icdev, int offset, int Len, char * r_string );

The parameters are described as follows.

L icdev: identifies the device handle, usually the return value of the auto_init function.

L offset: the offset from 0 ~ 255.

L Len: Specifies the string length.

L r_string: used to store the returned data.

Implementation Process
(1) create a new project named ex13_05. The default form is form1.

(2) In the form1 form, add two buttons to write data to the card and exit the program. Add a Textbox Control to write data in the textbox to the IC card.

(3) Main program code.

Encapsulate the functions used by the program to operate on the IC card in a class IC. The Code is as follows:

[Structlayout (layoutkind. Sequential)]

Public unsafe class IC

{

// Initialize the device

[Dllimport ("mwic_32.dll", entrypoint = "auto_init", setlasterror = true, charset = charset. ANSI, exactspelling = true, callingconvention = callingconvention. stdcall)]

Public static extern int auto_init (INT port, int baud );

// Device password format

[Dllimport ("mwic_32.dll", entrypoint = "setsc_md", setlasterror = true, charset = charset. ANSI, exactspelling = true, callingconvention = callingconvention. stdcall)]

Public static extern int setsc_md (INT icdev, int mode );

// Obtain the current status of the device

[Dllimport ("mwic_32.dll", entrypoint = "get_status", setlasterror = true, charset = charset. ANSI, exactspelling = true, callingconvention = callingconvention. stdcall)]

Public static extern int16 get_status (INT icdev, int16 * State );

// Disable the device Communication Interface

[Dllimport ("mwic_32.dll", entrypoint = "ic_exit", setlasterror = true, charset = charset. ANSI, exactspelling = true, callingconvention = callingconvention. stdcall)]

Public static extern int ic_exit (INT icdev );

// MAKE THE DEVICE BEEP

[Dllimport ("mwic_32.dll", entrypoint = "dv_beep", setlasterror = true, charset = charset. ANSI, exactspelling = true, callingconvention = callingconvention. stdcall)]

Public static extern int dv_beep (INT icdev, int time );

// Write data to the IC Card

[Dllimport ("mwic_32.dll", entrypoint = "swr_4442", setlasterror = true, charset = charset. ANSI, exactspelling = true, callingconvention = callingconvention. stdcall)]

Public static extern int swr_4442 (INT icdev, int offset, int Len, char * w_string );

// Check the card Password

[Dllimport ("mwic_32.dll", entrypoint = "csc_4442", setlasterror = true, charset = charset. Auto, exactspelling = true, callingconvention = callingconvention. winapi)]

Public static extern int16 csc_4442 (INT icdev, int Len, [financialas (unmanagedtype. lparray)] Byte [] p_string );

}

The following code writes data in textbox to the IC card. The Code is as follows:

Private void button#click (Object sender, eventargs E)

{

// Initialization

Int icdev = IC. auto_init (0, 9600 );

If (icdev <0)

MessageBox. Show ("port initialization failed. Please check whether the interface line is correctly connected. "," Error message ", messageboxbuttons. OK, messageboxicon. information );

Int MD = IC. setsc_md (icdev, 1); // device password format

Unsafe

{

Int16 status = 0;

Int16 result = 0;

Result = IC. get_status (icdev, & status );

If (result! = 0)

{

MessageBox. Show ("the current status of the device is incorrect! ");

Int d1 = IC. ic_exit (icdev); // disable the device

Return;

}

If (status! = 1)

{

MessageBox. Show ("Please insert an IC Card ");

Int D2 = IC. ic_exit (icdev); // disable the device

Return;

}

}

Unsafe

{

// The default password for the card is 6 F (the password is ffffff), The hexadecimal value of one f is 15, and the hexadecimal value of two F is 255.

Byte [] Pwd = new byte [3] {255,255,255 };

// Byte [] Pwd = new byte [3] {0xff, 0xff, 0xff };

// Char [] Pass = new ch {0xff, 0xff, 0xff };

Int16 checkic_pwd = IC. csc_4442 (icdev, 3, PWD );

If (checkic_pwd <0)

{

MessageBox. Show ("Incorrect IC card password! ");

Return;

}

Char STR = 'a ';

Int write =-1;

For (Int J = 0; j <textbox1.text. length; j ++)

{

STR = convert. tochar (textbox1.text. substring (J, 1 ));

Write = IC. swr_4442 (icdev, 33 + J, textbox1.text. length, & Str );

}

If (write = 0)

{

Int beep = IC. dv_beep (icdev, 20); // beep

MessageBox. Show ("data has been successfully written into the IC card! ");

}

Else

MessageBox. Show ("An error occurred while writing data to the IC card! ");

}

Int d = IC. ic_exit (icdev); // disable the device

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.