VBA COM port communication (Sending and receiving ASCII text) -


so problem working piece of hardware, , on 1 computer works cod works fine.

on another, doesn't work. not coder assume doing bad causing software unstable. here code

this function think causing problem.

declare sub sleep lib "kernel32" (byval dwmilliseconds long)   function writetobm5a(data string, bm5acom long) dim lengthofstring integer dim integer  lengthofstring = len(data) = 1 lengthofstring     writecom bm5acom, mid(trim(data), i, 1)     sleep 100 ' have tried changing sleep duration doesn't help. different random characters based on sleep duration next writecom bm5acom, chr(13)  end function 

when function above working correctly "ok" when readcom. when not random ascii characters.

below main subroutine

private sub commandbutton1_click() dim flag boolean   if commandbutton1.caption = "start"     commandbutton1.caption = "stop"     flag = true     dim bm5acom long     bm5acom = opencom(5, 9600&)     writetobm5a "rm", bm5acom 'puts hardware remote mode     sleep 3000     writetobm5a "st", bm5acom 'takes test measurement     sleep 3000     clearcom bm5acom     closecom bm5acom     sleep 3000 else     commandbutton1.caption = "start"     flag = false end if  while commandbutton1.caption = "stop"     sleep 1000     doevents     range("c1").value = range("c1").value - 1     if range("c1").value = 1         range("c1").value = 300         takeameasurement     end if wend  msgbox "i exited while loop!" writetobm5a "lm", bm5acom 'puts bm5a local mode exit sub end sub 

for reference these communication commands, didn't write these communication protocols, , kind of understand them.

declare function buildcommdcb lib "kernel32" alias "buildcommdcba" (byval lpdef string, lpdcb dcb) long declare function setcommstate lib "kernel32" (byval hcommdev long, lpdcb dcb) long declare function createfile lib "kernel32" alias "createfilea" (byval lpfilename string, byval dwdesiredaccess long, byval dwsharemode long, lpsecurityattributes long, byval dwcreationdisposition long, byval dwflagsandattributes long, byval htemplatefile long) long declare function readfile lib "kernel32" (byval hfile long, lpbuffer any, byval nnumberofbytestoread long, lpnumberofbytesread long, lpoverlapped long) long declare function writefile lib "kernel32" (byval hfile long, lpbuffer any, byval nnumberofbytestowrite long, lpnumberofbyteswritten long, lpoverlapped long) long declare function setcommtimeouts lib "kernel32" (byval hfile long, lpcommtimeouts commtimeouts) long declare function setupcomm lib "kernel32" (byval hfile long, byval dwinqueue long, byval dwoutqueue long) long declare function closehandle lib "kernel32" (byval hobject long) long declare function getlasterror lib "kernel32" () long declare sub sleep lib "kernel32" (byval dwmilliseconds long) declare function purgecomm lib "kernel32" (byval hfile long, byval dwflags long) long 

opencom commands

function opencom(portnum integer, baud long) long  dim lpdcb dcb dim comtimeout commtimeouts  com$ = "com" + trim(str(portnum))  'open communications port hcomtemp& = createfile(com$, generic_read or generic_write, 0, byval 0, open_existing, 0, byval 0)  'check errors if hcomtemp& < 0     opencom = hcomtemp&     exit function end if  'create control string build$ = "baud=" + trim(str(baud)) + " parity=odd data=7 stop=1"  'build data communications block r& = buildcommdcb(build$, lpdcb)  'set communications port's parameters dcb r& = setcommstate(hcomtemp&, lpdcb)  comtimeout.readintervaltimeout = 100       'maximum time wait between received bytes (milliseconds) comtimeout.readtotaltimeoutconstant = 1000  'maximum time wait receive data (milliseconds)  'set timeouts r& = setcommtimeouts(hcomtemp&, comtimeout)  'set input buffer size 4096 bytes , output buffer size 4096 bytes r& = setupcomm(hcomtemp&, 4096, 4096)  'return handle of newly opened communications port opencom = hcomtemp&  end function 

the write com command.

sub writecom(hcom long, writestring$)  r& = writefile(hcom, byval writestring$, len(writestring$), lpnumberofbyteswritten&, byval 0) end sub 

the read com command.

function readcom(hcom long, numberofbytes long) string  buffer$ = string(numberofbytes + 1, 0)  r& = readfile(hcom, byval buffer$, numberofbytes, lpnumberofbytesread&, byval 0)  if lpnumberofbytesread& > 0     readcom = left$(buffer$, lpnumberofbytesread&) else     readcom = "" end if  end function 

the clear com command

sub clearcom(hcom long)  'clear comm port , set input buffer size 4096 bytes , output buffer size 4096 bytes 'r& = setupcomm(hcom, 4096, 4096)  r& = purgecomm(hcom, &hf&)  end sub 

the close com command

sub closecom(hcom long)  r& = closehandle(hcom)  end sub 


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -