|
-
Sep 1st, 2008, 06:21 PM
#1
Thread Starter
New Member
Processing of texts received by serial & Winsock
Really hope you'll have the patience to read my message and help me.
I have a project which interfaces with a fire alarm system and need to
send DDE messages to a software based on the data received from the
fire alarm system. The whole structure of the project is that the Fire
Alarm system is sending messages to PC A (ClientProgram) via serial
and PC A (Client Program) will send the messages received to PC B
(ServerProgram) via Winsock. Then, Server Program will send DDE
messages to a software in PC B based on the messages received from PC
A.
The data sent by Fire Alarm system to PC A (Client Program) are as
below:
134313-6020708 @2-2-0 F1*
134313-6020708 @2-2-0 T1*
134313-6020708 @2-2-1 F1*
134313-6020708 @2-2-1 T1*
.
.
.
134313-6020708 @2-2-0 F0-
134313-6020708 @2-2-0 T0-
134313-6020708 @2-2-1 F0-
(Note:
1. F1* - fire at a sensor, T1* - trouble at a sensor, F0- - fire
cleared at a sensor, T0- - Trouble cleared at a sensor
2. @2-2-0, @2-2-1 and so on are the sensors involved and are defined
by user in a configuration file. The maximum sensors involved are 1000
sensors.
3. The messages sent by fire alarm system have fixed format but the
length of sensor can be different, i.e. @2-2-0, @2-22-111, @3-10-1 and
etc.
4. The meaningful message is in the format of @x-x-x F1*, @x-x-x T1*,
@x-x-x F0- or @x-x-x T0- )
Once PC A(Client Program) receives the data, it will send the data to
PC B (Server Program) and the Server Program will process those data
and proceed to send DDE messages to a software.
The whole thing works if only 1 or 2 lines of data are sent by Fire
Alarm System to PC A (Client Program). When more lines of data are
sent by Fire Alarm System to PC A(Client Program), the Server Program
either hangs and do nothing or takes enormously long time to send DDE
messages to the software.
I list down the relevant codes below and hope someone can optimize my
codes. Thanks a lot.
1) In PC A (Client Program),
'To receive messages from Fire Alarm System
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
Dim Buffer As Variant
Buffer = MSComm1.Input
If InStr(Buffer, Chr(10)) Then Debug.Print "found carriage
return"
tTerminal2.Text = tTerminal2.Text & Buffer
tTerminal1.Text = tTerminal1.Text & Buffer
End Select
End Sub
Private Sub tTerminal2_Change()
ExtractData
End Sub
' Extract the relevent data and send to PC B (Server Program)
'TcpClient is a WinSock control
Private Sub ExtractData()
Dim i As Integer
Dim first As Integer
Dim Second1 As Integer
Dim second2 As Integer
Dim DataLength As Integer
Dim DataToSend As String
Dim starti As Integer
i = Len(tTerminal2.Text)
'Loop through tTerminal2.text
For starti = 0 To i
first = InStr(1, tTerminal2.Text, "@")
If first Then
Second1 = InStr(first, tTerminal2.Text, "1*")
second2 = InStr(first, tTerminal2.Text, "0-")
If Second1 Or second2 Then
If Second1 > second2 Then
DataLength = Second1 - first + 2
DataToSend = Mid(tTerminal2.Text, first, DataLength)
TcpClient.SendData tTerminal2.Text
tTerminal2.Text = Mid(tTerminal2.Text, first + DataLength)
starti = Second1 + 2
ElseIf second2 > Second1 Then
DataLength = second2 - first + 2
DataToSend = Mid(tTerminal2.Text, first, DataLength)
TcpClient.SendData tTerminal2.Text
tTerminal2.Text = Mid(tTerminal2.Text, first + DataLength)
starti = second2 + 2
End If
End If
End If
Next starti
End Sub
2) In PC B (Server Program),
' Receive data via Winsock from PC A (Client Program)
Private Sub tcpServer_DataArrival _
(ByVal bytesTotal As Long)
Dim strData As String
TCPServer.GetData strData
tTerminal1.Text = ""
tTerminal1.Text = strData
tTerminal1.SelStart = Len(tTerminal1.Text)
tTerminal2.Text = tTerminal1.Text
tTerminal2.SelStart = Len(tTerminal2.Text)
CheckFireData2
End Sub
' To process the received data and send DDE messages to the software.
' PointAddress(j,k) is the sensor's code defined in a configuration
file by the user, i.e. @2-2-1
' and etc.
' Bit1(1) = "F1*", Bit1(2) = "F0-", Bit2(1) = "T1*", Bit2(2) = "T0-"
'PALenFinal is the maximum length of PointAdress(j,k) defined when the
program is initialised.
Private Sub CheckFireData2()
Dim i As Integer, j As Integer, k As Integer
Dim tempLoc As Integer, tempLen As Integer
Dim tempString As String
Dim tempLoc2 As Integer
tempLen = Len(tTerminal2)
CheckFlag = True
If tempLen > 0 Then
For i = 1 To tempLen
For j = 1 To totalreg
For k = 1 To 16
If InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & Bit1(1))) And PointAddress(j, k) <> "" Then
txtPoint(j).LinkMode = 0
txtPoint(j).LinkMode = 2
txtPoint(j).Text = txtPoint(j).Text Or (2 ^ ((k - 1)
* 2))
txtPoint(j).LinkPoke
i = InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & Bit1(1))) + Len(LCase(PointAddress(j, k) &
Bit1(1))) + i
ElseIf InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & Bit1(2))) And PointAddress(j, k) <> "" Then
txtPoint(j).LinkMode = 0
txtPoint(j).LinkMode = 2
txtPoint(j).Text = txtPoint(j).Text And (2 ^ 30 - 1 -
(2 ^ ((k - 1) * 2)))
txtPoint(j).LinkPoke
i = InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & Bit1(2))) + Len(LCase(PointAddress(j, k) &
Bit1(2))) + i
ElseIf InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & bit2(1))) And PointAddress(j, k) <> "" Then
txtPoint(j).LinkMode = 0
txtPoint(j).LinkMode = 2
txtPoint(j).Text = txtPoint(j).Text Or (2 ^ ((k - 1)
* 2 + 1))
txtPoint(j).LinkPoke
i = InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & bit2(1))) + Len(LCase(PointAddress(j, k) &
bit2(1))) + i
ElseIf InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & bit2(2))) And PointAddress(j, k) <> "" Then
txtPoint(j).LinkMode = 0
txtPoint(j).LinkMode = 2
txtPoint(j).Text = txtPoint(j).Text And (2 ^ 30 - 1 -
(2 ^ ((k - 1) * 2 + 1)))
txtPoint(j).LinkPoke
i = InStr(LCase(Mid(tTerminal2, i, PALenFinal + 3)),
LCase(PointAddress(j, k) & bit2(2))) + Len(LCase(PointAddress(j, k) &
bit2(2))) + i
End If
Next k
Next j
Next i
End If
tTerminal2.Text = ""
CheckFlag = False
End Sub
-
Sep 2nd, 2008, 11:28 AM
#2
Re: Processing of texts received by serial & Winsock
what language / release?
my signature has a link for serial port.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|