Posts Tagged ‘modem’

Hi, so I know ive been disconnected for a while.  But always trying to provide some goodies.

One of the cool things of the PLC is that if you know serial commands you can actually talk to it bypassing the SDM software.  My reason being that X10 motion sensors are cheeper than the Insteon and smaller which I can hide in different places but I would run in to the problem of the x10 signals not making it back to my main computer.  So what If you can have something on another computer which can transalate it to Insteon ?:)

So what I was right on the back of the PLC I have a transceiver and use the PLC with this small code to convert it to Insteon.  This way I basically have a x10 receiver on the other leg of my home.

So below is a small little program I use to  ‘convert’ X10 signals to a Insteon signal.  In my case the PLC will sending a ON  or OFF command to the PLM and depending the rate is the house code for example (B5,B6).   For the example im using VB 2005.  2008 and 2010 will work fine as well.  Note that i am bypassing entirely the SDM software.  if you don’t call it in your code it wont load, but you do have the port locked and isn’t sharable  🙂

A very good starting point is this site and I basically started from here http://www.madreporite.com/insteon/insteon.html which will show you how read bytes, which is how the plc will respond as well.

Basically the PLC talks at 4800,8,N,1.  The program consist of a simple form with  just one button to exit.


Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports System.IO
Imports System.IO.Ports
Imports System.Net

Public Class Form1
 Public WithEvents PLC As New IO.Ports.SerialPort
 Public X10_House_Code As String
 Public X10_House_VALUE As Integer
 Public X10_Command As String
 Public PLC_Queue As String
 Public handler As New mySerialDelegate(AddressOf Process_Queue)  ' Once we read the data process it in a separate thread.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 '------ OPEN PLC SERIAL CONNECTION SETUP PLC AND LEAVE READY - 
 PLC.PortName = "COM1"
 PLC.BaudRate = 4800
 PLC.DataBits = 8
 PLC.Parity = Parity.None
 PLC.StopBits = StopBits.One
 If PLC.IsOpen = False Then PLC.Open()

End Sub

 '---- THIS IS THE EVENT THAT WILL FIRE EACH TIME DATA IS RECEIVED BASICALLY WE CAPTURE ALL THE DATA AND
 '---- CALL THE OTHER THEAD TO CONVERT IT.
 Private Sub PLC_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles PLC.DataReceived
 Dim A, Bytes_to_read As Integer
 Dim rcvBuf(4096) As Byte
 Bytes_to_read = PLC.BytesToRead
 Do While PLC.BytesToRead > 0
 PLC.Read(rcvBuf, 0, rcvBuf.Length)
 For A = 0 To Bytes_to_read - 1
 PLC_Queue = PLC_Queue + Hex(rcvBuf(A)).PadLeft(2, "0") + " "
 Next
 Bytes_to_read = PLC.BytesToRead

 Loop

 Me.BeginInvoke(handler)
 End Sub

 Public Delegate Sub mySerialDelegate()
 Public Sub Process_Queue()

 'CONVERT STEAM TO X10 HOUSE CODES and the VALUE THAT WILL BE PASS TO THE OTHER DEVICE
 ' SO WHEN IT SEEN B2 IT WILL SEND TO THE OTHER SIDE A VALUE OF 18 ON THE RATE OR 'ON' VALUE
 ' Im only converting the B Code nothing else. 

 If PLC_Queue.Contains("02 4A 00 EE ") Then X10_House_Code = "B2" : X10_House_VALUE = 18
 If PLC_Queue.Contains("02 4A 00 E2 ") Then X10_House_Code = "B3" : X10_House_VALUE = 19
 If PLC_Queue.Contains("02 4A 00 EA ") Then X10_House_Code = "B4" : X10_House_VALUE = 20
 If PLC_Queue.Contains("02 4A 00 E1 ") Then X10_House_Code = "B5" : X10_House_VALUE = 21
 If PLC_Queue.Contains("02 4A 00 E9 ") Then X10_House_Code = "B6" : X10_House_VALUE = 22
 If PLC_Queue.Contains("02 4A 00 E5 ") Then X10_House_Code = "B7" : X10_House_VALUE = 23
 If PLC_Queue.Contains("02 4A 00 E4 ") Then X10_House_Code = "B8" : X10_House_VALUE = 24
 If PLC_Queue.Contains("02 4A 00 E7 ") Then X10_House_Code = "B9" : X10_House_VALUE = 25
 If PLC_Queue.Contains("02 4A 00 EF ") Then X10_House_Code = "B10" : X10_House_VALUE = 26
 If PLC_Queue.Contains("02 4A 00 E3 ") Then X10_House_Code = "B11" : X10_House_VALUE = 27
 If PLC_Queue.Contains("02 4A 00 EB ") Then X10_House_Code = "B12" : X10_House_VALUE = 28
 If PLC_Queue.Contains("02 4A 00 E0 ") Then X10_House_Code = "B13" : X10_House_VALUE = 30
 If PLC_Queue.Contains("02 4A 00 E8 ") Then X10_House_Code = "B14" : X10_House_VALUE = 31
 If PLC_Queue.Contains("02 4A 00 E4 ") Then X10_House_Code = "B15" : X10_House_VALUE = 32
 If PLC_Queue.Contains("02 4A 00 EC ") Then X10_House_Code = "B16" : X10_House_VALUE = 32

 '- ON OR OFF
 If PLC_Queue.Contains("02 4A 01 E2 ") Then X10_Command = "ON" : Send_Bytes(X10_House_VALUE, True) : Debug.Print(X10_House_Code) : X10_House_Code = "" : X10_Command = ""
 If PLC_Queue.Contains("02 4A 01 E3 ") Then X10_Command = "OFF" : Send_Bytes(X10_House_VALUE, False) : Debug.Print(X10_House_Code) : X10_House_Code = "" : X10_Command = ""

 Debug.Print(PLC_Queue)

 ' Clear and start again
 PLC_Queue = ""
 End Sub

Public Function Send_Bytes(ByVal value As Integer, ByVal State As Boolean) As Boolean
 Dim RECEIVER() As String = Split("0D.FE.9E", ".")  '-- THIS CONTAINS WHO WE ARE SENDING THE MESSAGE TO CHANGE AS NECESSARY
 '-- MAGIC CODE TO SEND TO PLC (I'LL EXPLAIN THEM LATER)
 Dim xmtbuf() As Byte = {&H2, &H40, &H1, &HA1, &H0, &H9, &HFD, &HE4, &H0, &H0, &H0, &HD, &HB, &H44, &H5, &H11, &H1}

 '-- HERE WE WILL REPLACE THE BYTES ABOVE IN BOLD TO WANT TO SEND TO,
 xmtbuf(11) = System.Convert.ToInt32(RECEIVER(0), 16)
 xmtbuf(12) = System.Convert.ToInt32(RECEIVER(1), 16)
 xmtbuf(13) = System.Convert.ToInt32(RECEIVER(2), 16)

 '- DEPENDING ON THE DEVICE BEING ON OR OFF WE SWAP ONE OF THE BYTES
 If State = True Then xmtbuf(15) = 17 Else xmtbuf(15) = 19 ' If on then push 17 if off push 19

 '--- HERE WE INSERT THE VALUE / X10 HOUSE CODE WE HAVE PREDIFINED 
 xmtbuf(16) = value

 '--- CALL OUR LITTLE FUNCTION TO WRITE THE BYTES TO THE SERIAL PORT
 PLC_Write(xmtbuf)

 '--- SETUP SOME MORE BYTES (THIS DOENST CHANGE) AND WRITE THEM, THIS TELLS THE PLC TO PROCESS THE BYTES WE SENT
 '--- BEFORE
 Dim xmtBuf2() As Byte = {&H2, &H46, &H1, &H42, &H10, &H9F}
 PLC_Write(xmtBuf2)
 End Function

 '--- THIS FUNCTION BASICALY WRITES ALL BYTES TO THE SERIAL PORT WE HAVE PREVIOUSLY DEFINED
 Public Function PLC_Write(ByVal XmtBuf As Object) As Boolean
 Try
 PLC.Write(XmtBuf, 0, XmtBuf.Length)
 Catch
 Debug.Print("COULDNT SEND DATA CHECK PORT")
 End Try

 End Function

 '--- SIMPLE BUTTON ON OUR FORM TO EXIT THE PROGRAM
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 End
 End Sub
End Class

One thing I was always interested was being able to notify me when a certain person calls.  And in this case flash a inteson light or two.

My code is in VB 2005, for a VB6 Sample you can go here and basically requires only some little coding.  In the case of VB6 I think you only need to add the Serial object and adjust the port via the objects property settings.

What I used,

  • Analog phone with Caller ID (Vonage actually)
  • Internal / External Modem – Mine is a 56k External modem and have caller Id capability.
  • Hook them up, from the wall to the modem, to the modem to the handset.  🙂

The best way to see if it does is to open Hyper terminal and type the following command AT #CID=1 if it respond’s ok your set. This of course if for the modem i’m using, depending on your device it may be different.  This page includes how you can check for Caller ID capability on your modem

Here is a example of what the modem will pickup and we will parse.

RING

DATE = 0621

TIME = 1113
NMBR = 1407xxxxxxxxx
NAME = Carlos Cobol

If you modem is external like mine you need to make sure AutoAwnser is disabled this is done by the DIP switches, For the US robotics its Dip switch #5 as down. In the case its internal you will need to add the command to disable auto answer. This is done with the command ATS0=0.

My Dip settings are as follows,

1-DOWN

2-UP

3-DOWN

4-UP

5-DOWN

6-UP

7-UP

8-DOWN

Since the code below allows to send some strings up startup, just add the in there if you need to.

Note: I do make reference to textbox called txtDataReceived in my form which will include the output.

First we make our imports include the serial devices (My program writes to a SQL database so some lines may not be needed)

Imports System.IO
Imports System.IO.Ports
Imports System.Net
Imports System.Data
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Threading

In our Class definitions we include the reference to the serial port and we’ll call it ‘Modem’

Public Class Form1
    Dim WithEvents Modem As New IO.Ports.SerialPort

In our Load event we will attach to the serial port and give it the basic settings, Here is where you would also add any initialization commands

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Modem.PortName = "COM1"
        Modem.BaudRate = 9600
        Modem.DataBits = 8
        Modem.Parity = Parity.None
        Modem.StopBits = StopBits.One
        If Modem.IsOpen = False Then Modem.Open()

        Modem.Write("AT #CID=1" & vbCrLf)  '-------- YOU CAN ADD ADDITIONAL LINES SUCH AS #ATS0=0 TO DISABLE AUTO ANSWER IN INTERNAL MODEMS
    End Sub

Next using the “Data Received” event we want to capture the Data which was received, Since there is a thread already running it wont allow us to update the textbox so simple we need to call use using the definition below,

 Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Modem.DataReceived

        Try
            txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
        Catch EX As Exception
            Debug.Print("ERROR")
        End Try
    End Sub

Second function to update the textbox and check for the number, and check for one of the text information above, Ive stripped my code for simplicity but you can search for specific strings here, match it with fields in a database, even FTP the data somewhere else, but that i’ll show you on another post.
Here we will check for the number ‘123456789’


 Public Sub updateTextBox()
        Dim temp As String
        With txtDataReceived
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red

            temp = Modem.ReadExisting.ToString
            Buffer = Buffer + temp
            .AppendText(temp)
            .ScrollToCaret()
        End With

        If Buffer.Contains("NMBR") = True Then
            Dim FILENAME As String = "c:\cALLINFO.TXT"  ' - Set a filename
            File.WriteAllText(FILENAME, Buffer)    '---- Write all the buffer for testing if we want to see it
            If Buffer.contains("123456789") then   '----- check for specific #
                'DO INSTEON COMMANDS HERE
                Buffer=""
                End If
        End if                  
End sub