MAC address recording and duplicate detection system

Source: Internet
Author: User
Tags mysql code sprintf

First, communication modules such as WiFi, ZigBee will have a unique MAC address, these modules in the factory need a set of systems to ensure uniqueness.

This set of MAC address recording and repeat detection system has been through the KK-Class shipment verification, difficult to slip through.

Second, the system design ideas:

The client program reads the module MAC address and then goes to the pass database to find out if it already exists, and if it does not exist, save the Mac to the pass database, show the pass, if it already exists, prove it has been produced, and repeat it, then save the Mac to the repeat database.

Third, the system implementation steps:

1, build a windows2003 server, external switch, through the network cable and the production line of dozens of computers connected.

2, install the MySQL server and the MySQL client program, the MySQL client first set up two databases: Pass database, repeat database.

3, write the client program, upload mac For comparison, show the results to the production line staff.

Of course, in order to better track the Mac duplicate module, you can upload the detection time, the computer number.

Four, give the MySQL code snippet:

void Sendmactoserver (char *sendmac)
{
//*******************************************************************************
Char szsqltext[500];

MYSQL *conn;
Mysql_res *rs;
Mysql_row ROW; Note that it declares a typedef char**mysql_row, an array of strings

conn = Mysql_init (NULL);
if (conn = = NULL)
{
fprintf (stderr, "Mysql_init () failed (probably out of memory) \ n");
Exit (1);
}

if (Mysql_real_connect (Conn,host_name,user_name,password,
macdbname,mysql_port,null,0) = = NULL)
{
Operation after MYSQL initialization if there is an error, you can use Mysql_errno (mysql*) and
Mysql_errer (mysql*) Get error codes and descriptions respectively
fprintf (stderr, "mysql_real_connect () Failed:\nerror%u (%s) \ n",
Mysql_errno (conn), MYSQL_ERROR (conn));
Exit (1);
}

printf ("Connect to%s database successful.\n", macdbname);

//*******************************************************************************

Char mysqlcmd[64];
sprintf (Mysqlcmd, "Select value from%s where value = ' AA:BB:AA:DD:CC:FF '", macdbname);

int macdbnamelen = strlen (macdbname);

int k;
for (k=0;k<17;k++)
{
mysqlcmd[33+macdbnamelen+k]=* (SENDMAC+K);
}

sprintf (Szsqltext, mysqlcmd);
printf ("Szsqltext =%s\n", szsqltext);
//********************************************************************************

The execution succeeds returns zero
if (mysql_query (conn,szsqltext)! = 0)
{
Mysql_close (conn);

if (DEBUG)
printf ("Select value from%s where value = ' AA:BB:AA:DD:CC:FF ' return 0 \ n", macdbname);

while (1);
}
else {
Returns all rows from the server immediately, stores them locally, produces a result set, and fails returns null
rs = Mysql_store_result (conn);

The result set is persisted on the server, and Fetch_row is taken from the server line by row
rs = Mysql_use_result (conn);
Get query result.
int count = (int) mysql_num_rows (RS);

while (row = Mysql_fetch_row (rs)) = null) {//returns NULL, indicating no more rows

if (!strcmp (sendmac,row[0]))
{
Wcolor=setconsolecolor (foreground_red| foreground_intensity| Background_blue);
printf ("***************************************************\n");
printf ("*******mac address detection: Repeated repeated repeated repeats \ n");
printf ("***************************************************\n");
Resetconsolecolor (Wcolor);

Sendrepeatmactoserver (SENDMAC);

while (1);
Exit (1);

}
else continue;

//}
FPUTC (' \ n ', stdout);
}

void Sendrepeatmactoserver (char *sendmac)
{
//*******************************************************************************
Char szsqltext[500];

MYSQL *conn;
Mysql_res *rs;
Mysql_row ROW; Note that it declares a typedef char**mysql_row, an array of strings

conn = Mysql_init (NULL);
if (conn = = NULL)
{
fprintf (stderr, "Mysql_init () failed (probably out of memory) \ n");
Exit (1);
}

if (Mysql_real_connect (Conn,host_name,user_name,password,
repeat_db_name,mysql_port,null,0) = = NULL)
{
Operation after MYSQL initialization if there is an error, you can use Mysql_errno (mysql*) and
Mysql_errer (mysql*) Get error codes and descriptions respectively
fprintf (stderr, "mysql_real_connect () Failed:\nerror%u (%s) \ n",
Mysql_errno (conn), MYSQL_ERROR (conn));
while (1);
Exit (1);
}

printf ("Connect to repeat DB successful.\n");

//*****************************************************************************************************
Getlocaltime (&sys);
sprintf (date, "%4d-%02d-%02d%02d:%02d:%02d\n", Sys.wyear,sys.wmonth,sys.wday,sys.whour,sys.wminute, Sys.wSecond);

sprintf (Szsqltext, "insert into REPEATMAC values ('%s ', '%s ', '%s ')", sendmac,pcnum,date);
printf ("Duplicate Mac value to upload:%s\n", Sendmac);

if (mysql_query (Conn,szsqltext)) {
printf ("Upload database Error:\nerror%u (%s) \ n",
Mysql_errno (conn), MYSQL_ERROR (conn));
Mysql_close (conn);
while (1);
Exit (1);

}
else {
Insert/delete/update statements can be used mysql-affected_rows () to get the rows that are affected
printf ("INSERT statement succeeded:%lu rows affected\n",
(unsigned long) mysql_affected_rows (conn));
printf ("Repeat Mac upload database OK! \ n ");
}

//*****************************************************************************************************
}

MAC address recording and duplicate detection system

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.