Announcement

Collapse
No announcement yet.

Problems with serial ports

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • jlikonen
    replied
    Uncle Michael and Stevech,

    Thanks a lot for the sample codes, I'll have a look at them. Uncle Michael, it seems that you are just sending binary code to the com port. I am wondering whether you are reading anything from the port.

    Jon,

    The reason why I am opening and closing the serial ports is that I have to send several handshaking commands and read the response from the port before I can send the actual command for changing e.g. the setpoint for temperature. Some of the responses are quite long (>60 characters) and sometimes I don't get all the characters. I have to use mode 0 in opencomport because the binary commands have the checksum as the last byte and therefore I can't use mode 1.

    I am still wondering whether anyone has used .Net Com port API.

    Jari

    Leave a comment:


  • Uncle Michael
    replied
    Jari,
    Here's an example that is working very reliably for me. It's possible that shorter delays will work as well, but once I got this working reliably I stopped experimenting.

    Code:
    Public Sub Main(ByVal Params As Object)
    
            ' Get status of all Zones
    
            Dim intComnum As Integer = 6
    
            hs.CloseComPort(intComnum)     'Close the serial port so it can be reopened to assign a data handling script
    
            hsp.WaitMS(200)             'Sometimes fails to obtain a handle if there is no pause
    
            hs.OpenComPort(intComnum, "19200,N,8,1", 1, "ReturnZoneStatus.vb", "GetData", Chr(&HF7)) 'Reopen serial port
    
            ' Send requests for status for each audio zone
            ' Zone 1
            If hs.DeviceStatus("u1") = 3 Then
                hs.SendToComPort(intComnum, Chr(&HF0) & Chr(&H0) & Chr(&H0) & Chr(&H7F) & Chr(&H0) & Chr(&H0) & Chr(&H70) & Chr(&H1) & Chr(&H4) & Chr(&H2) & Chr(&H0) & Chr(&H0) & Chr(&H7) & Chr(&H0) & Chr(&H0) & Chr(&H7C) & Chr(&HF7))
            End If
            hsp.WaitMS(200)
    
            ' Zone 2
            If hs.DeviceStatus("u2") = 3 Then
                hs.SendToComPort(intComnum, Chr(&HF0) & Chr(&H0) & Chr(&H0) & Chr(&H7F) & Chr(&H0) & Chr(&H0) & Chr(&H70) & Chr(&H1) & Chr(&H4) & Chr(&H2) & Chr(&H0) & Chr(&H1) & Chr(&H7) & Chr(&H0) & Chr(&H0) & Chr(&H7D) & Chr(&HF7))
            End If
            hsp.WaitMS(200)
    
            ' Zone 3
            If hs.DeviceStatus("u3") = 3 Then
                hs.SendToComPort(intComnum, Chr(&HF0) & Chr(&H0) & Chr(&H0) & Chr(&H7F) & Chr(&H0) & Chr(&H0) & Chr(&H70) & Chr(&H1) & Chr(&H4) & Chr(&H2) & Chr(&H0) & Chr(&H2) & Chr(&H7) & Chr(&H0) & Chr(&H0) & Chr(&H7E) & Chr(&HF7))
            End If
            hsp.WaitMS(200)
    
            ' Zone 4
            If hs.DeviceStatus("u4") = 3 Then
                hs.SendToComPort(intComnum, Chr(&HF0) & Chr(&H0) & Chr(&H0) & Chr(&H7F) & Chr(&H0) & Chr(&H0) & Chr(&H70) & Chr(&H1) & Chr(&H4) & Chr(&H2) & Chr(&H0) & Chr(&H3) & Chr(&H7) & Chr(&H0) & Chr(&H0) & Chr(&H7F) & Chr(&HF7))
            End If
            hsp.WaitMS(200)
    
            ' Zone 5
            If hs.DeviceStatus("u5") = 3 Then
                hs.SendToComPort(intComnum, Chr(&HF0) & Chr(&H0) & Chr(&H0) & Chr(&H7F) & Chr(&H0) & Chr(&H0) & Chr(&H70) & Chr(&H1) & Chr(&H4) & Chr(&H2) & Chr(&H0) & Chr(&H4) & Chr(&H7) & Chr(&H0) & Chr(&H0) & Chr(&H0) & Chr(&HF7))
            End If
            hsp.WaitMS(200)
    
            ' Zone 6
            If hs.DeviceStatus("u6") = 3 Then
                hs.SendToComPort(intComnum, Chr(&HF0) & Chr(&H0) & Chr(&H0) & Chr(&H7F) & Chr(&H0) & Chr(&H0) & Chr(&H70) & Chr(&H1) & Chr(&H4) & Chr(&H2) & Chr(&H0) & Chr(&H5) & Chr(&H7) & Chr(&H0) & Chr(&H0) & Chr(&H1) & Chr(&HF7))
            End If
    
            hsp.WaitMS(200)
    
            hs.CloseComPort(intComnum)     'Restore serial port with no data handling script
            hsp.WaitMS(200)
            hs.OpenComPort(intComnum, "19200,N,8,1", 1, "", "")
    
        End Sub
    Jon,
    In my case there is lots of traffic to and from the audio controller. When I was first setting this system up I thought the extraneous traffic was causing a problem (I was also using VBS at the time), so I decided to use the data handler only when I was looking for a response to a query for data. Since it works I've decided not to "fix" it.

    Leave a comment:


  • jon00
    replied
    If the port is being used by Homeseer exclusively, why not just keep the port open?

    I have always opened my ports in the startup.txt and just leave them open. Homeseer automatically closes the ports on shutdown.

    Leave a comment:


  • Stevech
    replied
    Still interested? If so... here's some code I have used. Utilizes HomeSeer API goodies. Note the "HaveData" call-back in the hs.OpenComPort call. This allows the script to open the com port then go dormant until a line of text arrives - and the call-back function is called by HS.
    Code:
     --------------------------------------
    const port = 7
    
    Function init()
        dim comport, OpenErr
    
       on error resume next
       hs.CloseComPort(port)
       on error goto 0
    
       OpenErr = hs.OpenComPort(port,"57600,n,8,1",1,"GarageDoorSensors.txt","HaveData")
       If OpenErr <> "" Then
          hs.WriteLog "ERROR", LogString & "GarageDoorSensors init() returned " & OpenErr
       Else
          hs.WriteLog "INFO", LogString & "GarageDoorSensors opened on COM" & cstr(port)
          hs.SendToComPort port, "R" ' force the AVR to reset
          call HSupdate("Script Started", "")
          call HSupdate2("Script Started", "")
    	  call hs.SendToComPort(port, "E")
       End If
    end Function 
    
    Public sub HaveData(s)
    	dim code
    	dim msg
    	
    	if mid(s,1,1) = "*" then hs.WriteLog "INFO", "GarageDoorSensors CallBack() s=" & s
        
    etc, etc etc
    end sub

    Leave a comment:


  • stipus
    replied
    Just google for VB.Net serial

    Leave a comment:


  • jlikonen
    replied
    Stipus,

    OK, thanks for the info. I tried to modify your example in the remote script gallery but I didn't get it working. I wonder whether you have a very simple example of a VB script how to send data to a com port and how to read from the port. I am not expert at all with .Net Com port API.

    Jari

    Leave a comment:


  • stipus
    replied
    I tried to look for help in this board and I found a post about .Net Com port API (see http://board.homeseer.com/showthread.php?t=123250). I wonder whether .Net Com port API would work in VB scripts.
    Yes the .Net COM port API is available from VB.NET scripts.

    Leave a comment:


  • jlikonen
    replied
    Mike,

    The OpenComPort command is just before I send a string to the com port. In fact I have several open and close comport commands in my script. Basically I open the com port just before sending data and close the port after reading and I do this every time when I send data to the port. I need to read e.g. 6 temperature values from the controller and I have read them one by one. The scipt is quite complex so it wouldn't be very helpful. One thing is the the controller has a binary protocol.

    I don't quite understand how a delay before the opencomport would help. I can understand if there is a short delay between sending data to the port and reading the data from the port.

    I tried to look for help in this board and I found a post about .Net Com port API (see http://board.homeseer.com/showthread.php?t=123250). I wonder whether .Net Com port API would work in VB scripts.

    Jari

    Leave a comment:


  • Uncle Michael
    replied
    Where is your OpenComPort command? Is it in a startup script or does it run in the script that is attempting to send data to the port?

    What is the index referring to? Is the OpenComPort statement in a loop? Can you post it?

    I have found that if I put a short delay (100-200 msec) immediately before an OpenComPort that I get more consistent performance, especially if there are other serial commands preceding it.

    Leave a comment:


  • jlikonen
    replied
    I have made some debugging and I have checked that my VB script sends a correct command. It seems that Homeseer doesn't get any characters on the serial port. The situation is now even worse. One of the ports doesn't seem to work with Homeseer at all even though I have restarted HS and Windows. Strange thing is that the controller's own program works OK. This is really frustrating problem.

    Jari

    Leave a comment:


  • jlikonen
    started a topic Problems with serial ports

    Problems with serial ports

    Every now and then I am getting following error messages in the HS log:

    OpenComPort: Index was outside the bounds of the array.
    14.11.2008 16:11:57 Warning Attempt to send to COM port 3, port is not open
    14.11.2008 16:11:58 Error Scripting runtime error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Conversion from string "&H" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info) at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDoub le(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger (String Value) --- End of inner exception stack trace --- at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger (String Value) at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger (Object Value) at scriptcode158.scriptcode158.Indoortemp(Object chan2) at scriptcode158.scriptcode158.main(Object parms) --- End of inner exception stack trace --- at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at Scheduler.VsaScriptHost.Invoke(String ModuleName, String MethodName, Object[] Arguments)
    14.11.2008 16:11:59 Error OpenComPort: Index was outside the bounds of the array.
    14.11.2008 16:11:59 Warning Attempt to send to COM port 6, port is not open

    The actual error message is related to the binary serial protocol and if there is no reply from my serial controller my script will crash. It seems that if some of the serial ports have "crashed" only possibility is to restart HS or reboot the computer. Strange thing is that if I close HS (without rebooting HS server) and I'll test the serial connection to my serial controllers with the software provided by the manufacturer there are no problems. So, it seems that this problem is related to HS. I have had this problem for long time with different HS2 versions. I am currently running v2.2.0.130 and I have two Sunix 4 port serial boards. Any ideas?

    Jari
Working...
X