vba - Retrieving weights from scales to excel -


i have connected weighing scale pc via rs-232 usb converter cable. goal create command button in excel 2007 place weight scale selected cell. got work using following code in userform.

private sub xmcommcrc1_oncomm()     static sinput string     dim sterminator string     dim buffer variant  ' branch according commevent property     select case xmcommcrc1.commevent     case xmcomm_ev_receive         buffer = xmcommcrc1.inputdata ' use input property mscomm         sinput = sinput & buffer         if worksheets("settings").range("terminator") = "cr/lf"             sterminator = vbcrlf         else             sterminator = vbcr         end if         if right$(sinput, len(sterminator)) = sterminator             xmcommcrc1.portopen = false             sinput = left$(sinput, len(sinput) - len(sterminator))             select case left$(sinput, 2)             case "st", "s "                 activecell.value = cdbl(mid$(sinput, 7, 8))                 activecell.activate             case "us", "sd"                 msgbox "the balance unstable."             case "ol", "si"                 msgbox "the balance showing eror value."             end select             sinput = ""         end if     end select end sub   public sub requestbalancedata()     worksheets("settings") ' configure , open com port         if not xmcommcrc1.portopen             xmcommcrc1.rthreshold = 1             xmcommcrc1.rtsenable = true             xmcommcrc1.commport = .range("com_port")             xmcommcrc1.settings = .range("baud_rate") & "," & _             .range("parity") & "," & _             .range("data_bits") & "," & _             .range("stop_bits")             xmcommcrc1.portopen = true         end if  ' send balance's "si" (send immediate) command ' request weighing data         if .range("terminator") = "cr/lf"             xmcommcrc1.output = "r" & vbcrlf         else             xmcommcrc1.output = "r" & vbcr         end if     end end sub 

i created command button following code.

private sub commandbutton1_click()      userform1.requestbalancedata  end sub 

when click on command button weight placed in selected cell. however, not consistently happen. when click button nothing placed in cell, , have click multiple times until weight placed in cell. fix this, i'm not sure start. problem code itself, or more problem converter or scale itself?

any appreciated.

here scale: https://www.optimascale.com/product-page/op-915-bench-scale

here converter cable: https://www.amazon.com/gp/product/b06xjzhcv8/ref=ox_sc_act_title_3?smid=a33n7o64f8fsdl&psc=1

here tutorial used code: http://www.msc-lims.com/lims/diybalance.html

here activex control tutorial used: http://www.hardandsoftware.net/xmcomm.htm

edit: have done wedge has suggested , placed mgsbox sinput after first end if. have been getting inconsistent results. wondering if need change scales sending format. scale set sending format 4.

here scale manual (sending formats on page 21-23: https://docs.wixstatic.com/ugd/78eff6_e629ae5fe7004c7189060cca4bc7c3de.pdf

2nd edit: have connected serial port putty. scale in continuos sending mode. in putty scale consistently sending following: st,gs+ 0.00lb. however, when try enter weight value in cell, message box displays part of data sent (st,gs+ 0.00lb) has got cut off, or has been sent multiple times 1 button press. know how fix this?

3rd edit: seems me continuous sending mode (mode 4) scale set sending data fast , causing code mess up. try make work command request mode (mode 3), can't figure out how parse data string , place cell. sending format command request mode : enter image description here

if me figure out how working appreciate it.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -