Wednesday, 15 May 2013

vb.net - VB2013 & MySQL not talking properly and program freezing -



vb.net - VB2013 & MySQL not talking properly and program freezing -

i have been working on little project while - takes plain text input ardiuno board (simple text, few characters). it's simple nfc card tagging program. programme supposed take input serialport , check input text against mysql database. if finds name in database, update 1 column, that's it.

the problem 2 fold - first, while can query database , list of people within database, tagging side of programme not update database, not matter input passed it. sec - whenever programme receives input, programme locks , gui becomes unresponsive.

my code below, , happy questions, comments, or criticisms. yes, mysql server on port '8228'. code driving me mad, , apologize may seem messy...

imports system.io.ports imports system.threading imports system.text imports system.data.odbc public class form1 dim withevents serialport new io.ports.serialport dim liststr string dim sqlstring string dim fstoday string = today.day.tostring + "_" + today.month.tostring + "_" + today.year.tostring dim nextline string dim portnum string dim sqlconn string dim sw streamwriter private sub form1_load(sender object, e eventargs) timer1.start() serialport.receivedbytesthreshold = 4 portnum = "com6" sqlconn = "mydomain.local" phone call testsqlconnectiontoolstripmenuitem_click() phone call connectserial() 'create todays log file dim filesys new filestream("c:\taglog\log-" + fstoday + ".txt", filemode.append, fileaccess.write, fileshare.write) filesys.close() end sub private sub connectserial() 'set usb (com) port , bandwidth (baudrate) seek serialport.baudrate = 115200 serialport.portname = portnum serialport.open() labport.text = "reader ok" labport.forecolor = color.green grab serialport.close() labport.text = "reader not found" labport.forecolor = color.red end seek end sub 'this handles setting , reading serial port private sub serialport_datareceived(byval sender object, byval e system.io.ports.serialdatareceivedeventargs) handles serialport.datareceived dim str string = serialport.readexisting() invoke(myd1, str) end sub 'this makes serial input string format delegate sub mymethoddelegate(byval [text] string) dim myd1 new mymethoddelegate(addressof myshowstringmethod) sub myshowstringmethod(byval mystring string) liststr = "" sqlstring = "" 'display text our textbox textbox2.appendtext(mystring) 'add lastly input listbox listbox1.items.add(textbox2.text) liststr = listbox1.items(listbox1.items.count - 1) sqlstring = "select * tagtable quickname='" + mystring + "';" seek dim cn odbcconnection = new odbcconnection("driver={mysql odbc 5.3 unicode driver};server=" + sqlconn + ";port=8228;database=tagging;uid=taguser;pwd=tagging;") cn.open() dim cmd new odbccommand(sqlstring, cn) sqlstring = "update tagtable set state = not state, time=now() quickname='" + mystring + "';" sw.writeline(vbcr + now() + " " + mystring) phone call updatesql() cn.close() grab ex exception sw.writeline(vbcr + now() + ex.tostring) end seek sw.close() end sub private sub timer1_tick(sender object, e eventargs) handles timer1.tick 'once per tick, check com port saying. if serialport.isopen textbox2.text = "" else textbox2.text = "" phone call connectserial() end if end sub private function iif(fileexists boolean, p2 string) object throw new notimplementedexception end function private sub updatesql() dim cn odbcconnection = new odbcconnection("driver={mysql odbc 5.3 unicode driver};server=" + sqlconn + ";port=8228;database=tagging;uid=taguser;pwd=tagging;") 'check connection sql server , update records dim cmd odbccommand = new odbccommand(sqlstring, cn) cn.open() labsql.forecolor = color.green labsql.text = "sql ok" if sqlstring <> "" seek cmd.executenonquery() cn.close() labsql.forecolor = color.green grab ex exception labsql.forecolor = color.red labsql.text = "sql not ok" sw.writeline(vbcr + now() + " " + ex.tostring) cn.close() end seek else cn.open() labsql.forecolor = color.green end if 'if there problem, alter sql label, , clsoe error'd connection. cn.close() sqlstring = "" end sub private sub testsqlconnectiontoolstripmenuitem_click() handles testsqlconnectiontoolstripmenuitem.click dim cn odbcconnection cn = new odbcconnection("driver={mysql odbc 5.3 unicode driver};server=" + sqlconn + ";port=8228;database=tagging;uid=taguser;pwd=tagging;") 'check connection sql server seek cn.open() labsql.forecolor = color.green labsql.text = "sql ok" cn.close() 'if there problem, alter sql label, display error in message box , close error'd connection. grab ex odbcexception labsql.forecolor = color.red labsql.text = "sql not ok" cn.close() end seek end sub private sub exittoolstripmenuitem_click(sender object, e eventargs) handles exittoolstripmenuitem.click dim msgans string msgans = msgbox("are sure want exit?", msgboxstyle.yesno, "exit?") if msgans = vbyes serialport.close() me.close() end if end sub private sub toolstripmenuitem3_click(sender object, e eventargs) handles toolstripmenuitem3.click portnum = "com1" phone call connectserial() end sub private sub toolstripmenuitem4_click(sender object, e eventargs) handles toolstripmenuitem4.click portnum = "com2" phone call connectserial() end sub private sub toolstripmenuitem5_click(sender object, e eventargs) handles toolstripmenuitem5.click portnum = "com3" phone call connectserial() end sub private sub toolstripmenuitem6_click(sender object, e eventargs) handles toolstripmenuitem6.click portnum = "com4" phone call connectserial() end sub private sub toolstripmenuitem7_click(sender object, e eventargs) handles toolstripmenuitem7.click portnum = "com5" phone call connectserial() end sub private sub toolstripmenuitem8_click(sender object, e eventargs) handles toolstripmenuitem8.click portnum = "com6" phone call connectserial() end sub private sub abouttoolstripmenuitem_click(sender object, e eventargs) handles abouttoolstripmenuitem.click msgbox("made phill c @ epoq it. tag command v2.3") end sub private sub localmachinetoolstripmenuitem_click(sender object, e eventargs) handles localmachinetoolstripmenuitem.click sqlconn = "localhost" phone call testsqlconnectiontoolstripmenuitem_click() end sub private sub otherlocationtoolstripmenuitem_click(sender object, e eventargs) handles otherlocationtoolstripmenuitem.click sqlconn = inputbox("enter sql address or location.", "sql location input", "localhost").tostring phone call testsqlconnectiontoolstripmenuitem_click() end sub private sub showuserlisttoolstripmenuitem_click() handles showuserlisttoolstripmenuitem.click dim cn odbcconnection dim outstring string = "" sqlstring = "select person,quickname tagtable" cn = new odbcconnection("driver={mysql odbc 5.3 unicode driver};server=" + sqlconn + ";port=8228;database=tagging;uid=taguser;pwd=tagging;") cn.open() dim cmd new odbccommand(sqlstring, cn) dim query = cmd.executereader() outstring = "full name, quick name" + vbcr while query.read outstring = outstring + query.item(0) + " , " + query.item(1) + vbcr end while msgbox(outstring) query.close() cn.close() end sub private sub addusertoolstripmenuitem_click(sender object, e eventargs) handles addusertoolstripmenuitem.click dim queryfull = inputbox("enter new users total name.", "new user input", "new user").tostring dim queryqname = inputbox("enter new users quick name / nickname.", "new user input", "new user").tostring sqlstring = "insert tagtable (quickname,person,state,time) values ('" + queryqname + "','" + queryfull + "','in', now());" dim cn odbcconnection cn = new odbcconnection("driver={mysql odbc 5.3 unicode driver};server=" + sqlconn + ";port=8228;database=tagging;uid=taguser;pwd=tagging;") cn.open() seek dim cmd new odbccommand(sqlstring, cn) msgbox("new user has been setup on server.") cn.close() grab ex exception cn.close() msgbox("new user setup has failed, please contact support") end seek end sub private sub deleteusertoolstripmenuitem_click(sender object, e eventargs) handles deleteusertoolstripmenuitem.click dim queryfull = inputbox("enter users total name.", "delete user input", "user deleted").tostring dim queryqname = inputbox("enter users quick name / nickname.", "delete user input", "user deleted").tostring sqlstring = "delete tagtable quickname='" + queryqname + "' , person='" + queryfull + "';" dim cn odbcconnection cn = new odbcconnection("driver={mysql odbc 5.3 unicode driver};server=" + sqlconn + ";port=8228;database=tagging;uid=taguser;pwd=tagging;") cn.open() seek dim cmd new odbccommand(sqlstring, cn) msgbox("user has been removed server.") cn.close() grab ex exception cn.close() msgbox("user revomal has failed, please check total name , quick names, , seek again.") end seek end sub end class

mysql vb.net visual-studio-2013 serial-port arduino

No comments:

Post a Comment