Wednesday, November 18, 2015

MsComm32 with error "Port already Open" on Windows XP

I post this article as I can't find the solution on Internet. I hope this article can help anybody who has same issue with me.

Few days ago, I had an issue to send output to Werma LED light on Windows XP. It works perfectly on Windows 7. I can say that Windows XP is more fussy than Windows 7. I need to change few codes to fit to Windows XP's requirement.

One of the issue when trigger the light by using mscomm32.ocx is that the light will turn ON on the first time and can't be turned ON afterward. If you try to keep turning ON the light, there is error 8005 - Port already Open.

Trying to turn ON and OFF the light from Hyperterminal and Putty is OK. After further investigation and helped from other colleagues, we found out that Windows XP can't accept Carriage Return and Line Feed. It only accept Carriage Return on output command. Windows 7 can accept and work perfectly.

Windows XP
Chr := 13;
CRLF := FORMAT(Chr);

...
mscomm. Output = 'WR 00 01 01 64' + CRLF;

...


Windows 7
Chr := 13;
CRLF := FORMAT(chr);
Chr := 10;
CRLF := CRLF + FORMAT(Chr)

...
mscomm. Output = 'WR 00 01 01 64' + CRLF;

...

Windows 7 can accept both type of above coding :)

Thursday, November 5, 2015

Serial Port - PuTTY - VBScript - Werma LED Beacon Multicolour

On this session, I would like to give a documentation of my mini project. There are few ways to communicate with serial port. you may use HyperTerminal, PuTTY, or other applications.

At the moment, I focus on PuTTY to communicate with Serial Port (WERMA LED). Instead of running PuTTY and trigger commands manually to LED, I create a script in VBScript to trigger it.

This sample script will turn on BLUE colour of LED for one second and will turn off the LED afterward. I also add a function how to get the correct port dynamically regardless any port you have plug the LED in your PC.

Option Explicit
On Error Resume next 

Dim oshell
Dim PortNum

Set oshell = createobject("Wscript.Shell") 
oshell.run "cmd.exe"
Wscript.sleep 200

PortNum = GetPortNumber
Wscript.sleep 200

If PortNum = "INVALID" Then
  Wscript.sleep 200
  oshell.sendkeys "exit"+ ("{Enter}")
Else
  oshell.Run("putty.exe -serial" + " " + PortNum + " " + "-sercfg 9600,8,n,1,N") 
  Wscript.sleep 200
  oshell.sendkeys "WR 00 01 01 64" + ("{Enter}")     ' WR 00 RR GG BB <CR>
  Wscript.sleep 1000
  oshell.sendkeys "WR 00 01 01 01" + ("{Enter}")
  Wscript.sleep 200
  oshell.sendkeys "%" + ("{F4}")
  Wscript.sleep 200
  oshell.sendkeys ("{Enter}")
  Wscript.sleep 200
  oshell.sendkeys "exit"+ ("{Enter}")  
End If

Set oshell = nothing

Function GetPortNumber()
  Dim objShell
  Dim I
  Dim WermaPortFound
  Dim skey
  Dim skey2
  Dim InstanceID
  Dim PortName
  Dim WERMAPVID

  WERMAPVID = "VID_04B4&PID_E00A"
  
  Set objShell = WScript.CreateObject("WScript.Shell") 
  I = 0
  with CreateObject("WScript.Shell")
    Do Until WermaPortFound OR (I = 15)
      
      InstanceID = " "
      PortName = " "
      WermaPortFound = FALSE
  
      skey = "HKLM\SYSTEM\CurrentControlSet\Services\usbser\Enum\" + Cstr(I) 
      On Error Resume Next       
      InstanceID = .regread(sKey)     
      
      If (InstanceID <> " ") AND (InStr(UCase(InstanceID),UCase(WERMAPVID)) > 0) Then
        skey2 = "HKLM\SYSTEM\CurrentControlSet\Enum\" + InstanceID + "\Device Parameters\PortName"   
        On Error Resume Next 
        PortName = .regread(sKey2)
        
        If (PortName <> " ")  AND (InStr(UCase(PortName),"COM") > 0)Then 
          WermaPortFound = (Err.number = 0)
        End If

      End If

      I = I + 1
    Loop
  End with


  If WermaPortFound Then
     GetPortNumber = PortName
  Else
     GetPortNumber = "INVALID" 
  End If
  
  Err.Clear
  Set objShell = nothing

End Function

Sunday, November 1, 2015

AutoHotKey - Windows on Top



I created simple application to support Point of Sales application by using AutoHotKey open source application. It's easy to learn and implement. The point of my simple application is that I want to make a form to be on top of others windows form on the desktop. In my case, "a form" is run by RunCommand.exe. By creating this simple script, compile it into executable file (.exe), and put it on windows startup folder, it will run and force "a form" to be on top of other window forms.

Simple and pretty cool !


AutoHotKey script:

#Persistent
SetTimer, WatchExistXButtonWindow, 500
return

WatchExistXButtonWindow:
if WinExist("ahk_exe C:\RunCommand.exe")
{  
    WinSet, AlwaysOnTop, ON, ahk_exe C:\RunCommand.exe
}
return

To know more about AutoHotKey : http://autohotkey.com/

.NET Framework in Concise