Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,295 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 874 people online right now. Registration is fast and FREE... Join Now!




Serial Port Communication-Problem with modems

2 Pages V  1 2 >  
Reply to this topicStart new topic

Serial Port Communication-Problem with modems

dimos8enis
10 Nov, 2008 - 12:42 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
Hello! I've writed a program that function as a serial multiplexer, thus I can read from 9 ports simultaneously and forward the messages to the main port. Also, the messages incoming from the main port are distributed to the aforementioned multiplexed ports.
I have used the tutorial found on this site and modified it for my needs.

I have bought some pci serial port controllers so I can have at least 6 ports to work, and I have connected an async Analog modem 9600kbps to each port.

The program works just fine even when the incoming messages are not right, but the problems start if the leased line is cut off for some reason (it happens very often). then, the input buffer gets full of garbage data and the program gets stuck, so if I try to close the port that causes the problem, the program ends.
I tried to make input/output buffer larger but no result. I tried to introduce the following:
If Not comport.Breakstate then
comport.ReadTo(xxx)
end if
but again no result.
Take in mind that I have writed a sub for data filtering, so the messages that contain non-readable characters are not forwarded(only ABC... and 123...)
The code is very large to paste in its whole, so if somebody can give me an opinion, I can paste some parts here.
Please help...


The sub comport_DataReceived is as follows:

CODE
Try
                    Dim msg As String
                                       If Not comPort.BreakState Then
                        comPort.ReadTimeout = -1
                        msg = comPort.ReadTo(Chr(13))
                        _type = MessageType.Incoming
                        msg = msg.Trim
                        msg = Chr(10) & msg & Chr(13)
                        _msg = msg
                        DisplayData(MessageType.Incoming, msg)
                    Else
                        comPort.Close()
                        comPort.Open()
                        'reset port
                    End If
                    Exit Select
                Catch ex As Exception
                    comPort.Close()
                    comPort.Open()
                    'reset port-these resets  I added them yesterday,not yet tested if work properly
                End Try


msg = Chr(10) & msg & Chr(13)

this cannot change. It is the way modems communicate

User is offlineProfile CardPM
+Quote Post


n8wxs
RE: Serial Port Communication-Problem With Modems
10 Nov, 2008 - 01:22 PM
Post #2

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 223 times
My Contributions
vb

Try
Dim msg As String
If Not comPort.BreakState Then
comPort.ReadTimeout = -1
msg = comPort.ReadTo(Chr(13))
_type = MessageType.Incoming
msg = msg.Trim
msg = Chr(10) & msg & Chr(13)
_msg = msg
DisplayData(MessageType.Incoming, msg)

Else
comPort.Close()
comPort.Open()
'reset port
End If

Exit Select
Catch ex As Exception
comPort.Close()
comPort.Open()
'reset port-these resets I added them yesterday,not yet tested if work properly
End Try

When BreakState is true and the else branch is taken, can you use the comport.BytesToRead property and the comport.Read()
method to empty the serialport's input buffer?


User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Serial Port Communication-Problem With Modems
10 Nov, 2008 - 03:34 PM
Post #3

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
i think you need to monitor carrier detect.

Debug.WriteLine(SerialPort1.CDHolding)

if it is false the modem is not connected. btw - when issuing modem commands, they are acted on when carriage return is received.

SerialPort1.Write("AT" & vbCr) 'returns OK

This post has been edited by dbasnett: 10 Nov, 2008 - 03:36 PM
User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 08:56 AM
Post #4

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
QUOTE(n8wxs @ 10 Nov, 2008 - 01:22 PM) *

When BreakState is true and the else branch is taken, can you use the comport.BytesToRead property and the comport.Read()
method to empty the serialport's input buffer?


when breakstate is true the modem is supposed to has lost communication and the program gets stuck. how can I empty the buffer?
actually all this is theoretical because I realised that breakstate is always false...
User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 09:20 AM
Post #5

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
QUOTE(dbasnett @ 10 Nov, 2008 - 03:34 PM) *

i think you need to monitor carrier detect.

Debug.WriteLine(SerialPort1.CDHolding)

if it is false the modem is not connected. btw - when issuing modem commands, they are acted on when carriage return is received.

SerialPort1.Write("AT" & vbCr) 'returns OK


I have tried to enclose the read command in an if..then where I checked the CDHolding status before reading from the buffer but then the program never read anything, so I gave up with this idea.

As for the commands, I don't want to give commands to the modem itself, but to another system connected to the modem, so I just need these characters, tested and works.
User is offlineProfile CardPM
+Quote Post

n8wxs
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 10:08 AM
Post #6

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 223 times
My Contributions
QUOTE(dimos8enis @ 10 Nov, 2008 - 12:42 PM) *

The program works just fine even when the incoming messages are not right, but the problems start if the leased line is cut off for some reason (it happens very often). then, the input buffer gets full of garbage data and the program gets stuck, so if I try to close the port that causes the problem, the program ends.

Why is the program ending?

User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 01:36 PM
Post #7

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
QUOTE(n8wxs @ 11 Nov, 2008 - 10:08 AM) *

QUOTE(dimos8enis @ 10 Nov, 2008 - 12:42 PM) *

The program works just fine even when the incoming messages are not right, but the problems start if the leased line is cut off for some reason (it happens very often). then, the input buffer gets full of garbage data and the program gets stuck, so if I try to close the port that causes the problem, the program ends.

Why is the program ending?


if I knew, I would solve that issue... I guess something might be wrong with the input buffer (despite the fact I have set the buffers to 256K (!)) the program doesn't end on its own, it just gets very very slow or even completely stuck and if I click on any button, I get a windows error message, I click on "Don't Send" (!) and then the program terminates.
Is there any possibility that the port could go to an undefined status or something like that?
Or maybe because of the mass garbage data, the bus that carries serial data from the ports to CPU gets stuck...(?)

I suppose that if it was an input buffer issue, then only the specific port that is connected to the offline modem would get stuck, but the program in its whole malfunction.
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 02:08 PM
Post #8

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
break is an indication from the other end that if you are sending data to stop, or that the other end needs attention.

if carrier detect is false the modem is not connected.
User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 02:32 PM
Post #9

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
QUOTE(dbasnett @ 11 Nov, 2008 - 02:08 PM) *

break is an indication from the other end that if you are sending data to stop, or that the other end needs attention.

if carrier detect is false the modem is not connected.


so, I may not use breakstate...
but (I think) Carrier Detect is false all the time, even if the modem is connected and has Carrier with the modem at the other end. otherwise I would get the messages from the port at that case
User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 02:45 PM
Post #10

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
If I would filter the garbage data before it would enter the main program, would that solve my problem?
I have a data filter sub, that is true when the parameter string is readable (ABC..., 123...). What if I would apply this in the comport_DataReceived sub?

You see the garbage data I get is like that :
CODE
wR~01&1R6z_Dh7d6k|fU3to8?/@k$8iQw=anJ):"C    `!QgX?
h 575Ic'94-hV:V2"8 H8c+{ 

and always in the end there is a message "NO CARRIER" but it is too late...

This is my data filter:

CODE
Private Function Allowed(ByVal msg1 As String) As Boolean
        Dim permit As Boolean
        Dim character As Char
        Dim asciiofchar, i As Integer
        msg1 = msg1.ToUpper
        permit = False
        If msg1.Length > 1 Then
            For i = 0 To (msg1.Length - 1) Step 1
                character = msg1.Substring(i, 1)
                asciiofchar = Asc(character)
                If asciiofchar >= 48 And asciiofchar <= 57 Then
                    permit = True
                ElseIf asciiofchar >= 65 And asciiofchar <= 90 Then
                    permit = True
                ElseIf asciiofchar = 10 Then
                    permit = True
                ElseIf asciiofchar = 13 Then
                    permit = True
                Else
                    permit = False
                    Exit For
                End If
            Next i
        ElseIf msg1.Length = 1 Then
            character = msg1
            asciiofchar = Asc(character)
            If asciiofchar >= 48 And asciiofchar <= 57 Then
                permit = True
            ElseIf asciiofchar >= 65 And asciiofchar <= 90 Then
                permit = True
            ElseIf asciiofchar = 10 Then
                permit = True
            ElseIf asciiofchar = 13 Then
                permit = True
            Else
                permit = False
            End If
        End If
        Return permit
    End Function

User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 02:47 PM
Post #11

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
i believe the default is for CD to follow carrier. the command to set this if it isn't the default is

AT&C1
User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 03:04 PM
Post #12

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
I upload the .dat logfile with the messages I get at the time of disconnect.

...

QUOTE(dbasnett @ 11 Nov, 2008 - 02:47 PM) *

i believe the default is for CD to follow carrier. the command to set this if it isn't the default is

AT&C1


Even then, the CD would become False after about half a second, time in which some messages have entered and the program and the result is the same.
even if that would help, what could I do if I get the CDHolding=False? Closing and reopening the port after some seconds would be an idea...

I am thinking to use this expression
CODE
If Allowed(comport.ReadTo(Chr(13))) then
              msg=comport.ReadTo(Chr(13))
           endif


like I foresee if the message is right

what do you think?

but I don't know if in this way, the buffer would empty or the garbae data would still remain in the buffer and I get them on the next ReadTo


Attached File(s)
Attached File  LOGERRORDISCONNECT.txt ( 4.81k ) Number of downloads: 14
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 03:11 PM
Post #13

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
redial if you lose carrier.


User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 03:14 PM
Post #14

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
the point is that the program shouldn't get stuck.
the modem cannot redial if the port is open, because there are forwarded outgoing messages that interrupt the handshake between the modems. that I realised by trial.
the program is supposed to work even (especially) if I am not there...
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Serial Port Communication-Problem With Modems
11 Nov, 2008 - 03:43 PM
Post #15

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
sure it can. if it has dropped carrier then the modem should be in command mode and just do this

atdt5551212


User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
12 Nov, 2008 - 07:06 AM
Post #16

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
QUOTE(dbasnett @ 11 Nov, 2008 - 03:43 PM) *

sure it can. if it has dropped carrier then the modem should be in command mode and just do this

atdt5551212


ok it can redial, I tested it today, but there's no point in this, since the program remains stuck. once it gets stuck it can't get into normal function
User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
12 Nov, 2008 - 07:13 AM
Post #17

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
I want to write a program that doesn't get stuck when the modem is disconnecting. Besides, now, I am the one who causes the disconnection and I can plug the line again back soon, but in real, the line can be down at every time, and the result will be the same-the program crashes and nothing works. Sure I can redial and restart the program but that's not what I want. I want the program not to crash with the modem's disconnection...
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Serial Port Communication-Problem With Modems
12 Nov, 2008 - 11:16 AM
Post #18

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
where does it crash? are you checking CD? if CD is false you should not bother reading from the serial port.
User is offlineProfile CardPM
+Quote Post

dimos8enis
RE: Serial Port Communication-Problem With Modems
12 Nov, 2008 - 11:36 AM
Post #19

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
QUOTE(dbasnett @ 12 Nov, 2008 - 11:16 AM) *

where does it crash? are you checking CD? if CD is false you should not bother reading from the serial port[size=3].


why?
it crashes when the modem disconnects (CD=off) and it sends garbage data to the serial port.
i am not checking the CD before reading from the port..
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Serial Port Communication-Problem With Modems
12 Nov, 2008 - 12:19 PM
Post #20

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
i am trying to help. it sounds like what you are doing doesn't work. i have told you many times that you need to check CD.

you probably need some sort of protocol, maybe something like this:

a byte of size -- message of size -- a byte of checksum. the checksum could be as simple as an XOR of the bytes in the message.

you could start off the communication by sending.

2 -- 170 85 -- 255

message size - 2 bytes
message - 170 85
XOR checksum - 255


User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:35PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month