1
votes

I have a USB modem connected and it is working fine at port COM1 with putty.

The modem is able to response to commands like

AT
OK
ATZ
OK

I tried to do the same thing under .NET 2.0 framework using SerialPort class but no "OK" is ever received.

Code is updated for further testing

Initialize port

Dim ModemPort As SerialPort

ModemPort.DtrEnable = True
ModemPort.Handshake = Handshake.XOnXOff
ModemPort.NewLine = "\r\n"               '(Added but the problem still exist)
'ModemPort.ReadTimeout = 3000             (Removed for testing)
SendSerialData(ModemPort, "ATZ")

Method SendSerialData

Function SendSerialData(ByRef modemPort As SerialPort, ByVal data As String)    
    Dim returnStr As String = ""

    If modemPort.IsOpen = False Then
        modemPort.Open()
    End If

    'Updated
    modemPort.WriteLine(data) 'ATZ
    System.Threading.Thread.Sleep(1000)

    Try
        Do
            Dim Incoming As String = modemPort.ReadExisting()
            If Incoming = "OK" Then
                Exit Do
            End If
            If Incoming = "" Then
                modemPort.WriteLine(data)
                System.Threading.Thread.Sleep(1000) 'Modem will send "Error" sometime
            Else
                returnStr &= Incoming
            End If
        Loop
    Catch ex As Exception
    End Try

    Return returnStr
End Function

However, all I revived is echo

Dim Incoming As String = modemPort.ReadExisting()

is

ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ ATZ AT ERROR Z ATZ

(Split by newlines)

w̶h̶i̶c̶h̶ ̶i̶s̶ ̶j̶u̶s̶t̶ ̶w̶h̶a̶t̶ ̶I̶ ̶s̶e̶n̶t̶ ̶t̶h̶r̶o̶u̶g̶h̶ ̶W̶r̶i̶t̶e̶L̶i̶n̶e̶ (This is actually an echo)

The modem is able to echo my commands but the modem will not send an "OK" but sometime "ERROR".

This is the modemPort variable at runtime just before ReadLine()

A local test done by PassMark shows the modem is functional

M̶a̶y̶b̶e̶ ̶t̶h̶e̶ ̶m̶o̶d̶e̶m̶ ̶n̶o̶t̶ ̶r̶e̶a̶d̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶t̶r̶e̶a̶m̶?̶

Is there any configuration needed to be done?

I did some digging online but it seems that no one has similar problems.

Thanks in advance.

1
So it actually works just fine and you don't have a real problem. You are just forgetting to keep calling ReadLine() until you get the OK response. You can send "ATE0" to disable the echo.Hans Passant
Thanks :) After further testing, it is indeed the modem echo. Now I just need to figure out why is it not responding "OK".HardcoreGamer
I think I solve it, your help is a huge step for the right direction for me Thanks ; ) Writing the answer now.HardcoreGamer

1 Answers

0
votes

Turns out I just need to dig harder.

I come across this page and find the solution

What I did wrong:

  1. The port initialized is not using the right encoding.
  2. The NewLine characters \r\n is not properly escaped.

The correct way

ModemPort.DtrEnable = True
ModemPort.Handshake = Handshake.XOnXOff
ModemPort.NewLine = Chr(13) + Chr(10)
ModemPort.Encoding = System.Text.Encoding.ASCII

The modem is able to reponse

ATZ

OK

If you do not want the "ATZ" echo, use the "ATE0" with "AT" command. "ATZ" will reset ATE0.