Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modem not responding 1

Status
Not open for further replies.

Kavius

Programmer
Apr 11, 2002
322
CA
I need to query a modem for signal strength using an AT command.

Testing using "AT". Hyperterminal gives "OK". Program gives nothing (0 bytes returned).

I thought to begin communicating with the modem using the MSComm control, but it only returns echoed characters. I switched to direct serial reading and writing (using CreateFile, ReadFile, WriteFile), but am getting -1 for my pointer (that's not right is it?).

(This is mostly from MSDN, but I've lost the link)
Code:
' Obtain a handle to the COM1 serial port.
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
' Verify that the obtained handle is valid.
If hSerialPort = -1 Then
  'PROBLEM
  debug.print "PROBLEM"
End If
' Write data to COM1.
Success = WriteFile(hSerialPort, Buffer, UBound(Buffer) - 1, BytesWritten, 0)
If Success = False Then
  'PROBLEM
  debug.print "PROBLEM"
End If
' Read data from COM1.
Buffer(LBound(Buffer)) = 0
Success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, 0)
If Success Then
  Form1.text = ("BytesRead: " & BytesRead)
  For i = LBound(Buffer) To LBound(Buffer) + BytesRead - 1
    Form1.text = (i & " | " & Buffer(i)) '& " | " & chr(Buffer(i)))
  Next
Else
  'PROBLEM
End If
Success = CloseHandle(hSerialPort)
If (not Success) Then
  Form1.text = ("Unable to release handle to " & port)
End If
 
What API declaration are you using for CreateFile?

Code:
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
   (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long,_
    ByVal hTemplateFile As Long) As Long

Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type
Private Sub Command1_Click()
Dim hSerialPort As Long
Dim sa As SECURITY_ATTRIBUTES

'Create NULL Security Attributes
With sa
    .bInheritHandle = False
    .lpSecurityDescriptor = 0
    .nLength = Len(sa)
End With

hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, _
                0&, sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
' Verify that the obtained handle is valid.
If hSerialPort = -1 Then
  'PROBLEM
  
  Debug.Print "PROBLEM "; GetLastError()
  Else
  Debug.Print "valid handle "; hSerialPort
End If
CloseHandle hSerialPort
End Sub

works for me.

It is also possible that you have managed to "lock" the com port by not calling CloseHandle after a successful call to CreateFile (if you set a breakpoint and then stoped the program for example)

This happened to me while I was testing the code above. Logging out and in again release it though.

However, there is an easier way to get a handle to the com port....

The mscomm control has a property commid, which gives you a valid handle to the com port.

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Code:
[b]API:[/b]
Public Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileW" ( _
    ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    lpSecurityAttributes As Long, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long) As Long
The only difference I can see is that I refer to the security attributes as a long (to hold the pointer).

You were saying that I could use the MSComm to get the handle, but MSComm is not working correctly either. I actually switched to the API as a work around to MSComm problems.
 
I'm not sure what the difference is between our code now (I just tweeked mine to look more like yours), but mine still doesn't work. Fortunately, after cutting and pasting your code into VB, it successfully gets a filehandle (yipee!!).

Now I have successfully locked my app up by doing a readfile call. Here is my new (stripped down) version of my code:
Code:
Option Explicit

Private Const GENERIC_READ          As Long = &H80000000
Private Const GENERIC_WRITE         As Long = &H40000000
Private Const OPEN_EXISTING         As Long = 3
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Const NOPARITY              As Long = 0
Private Const ONESTOPBIT            As Long = 0

Private Type SECURITY_ATTRIBUTES
  nLength              As Long
  lpSecurityDescriptor As Long
  bInheritHandle       As Long
End Type

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
   (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long _
) As Long

Private Declare Function WriteFile Lib "kernel32" ( _
  ByVal hFile As Long, _
  ByVal lpBuffer As String, _
  ByVal nNumberOfBytesToWrite As Long, _
  ByRef lpNumberOfBytesWritten As Long, _
  ByVal lpOverlapped As Long _
) As Boolean

Private Declare Function ReadFile Lib "kernel32" ( _
  ByVal hFile As Long, _
  ByVal lpBuffer As String, _
  ByVal nNumberOfBytesToRead As Long, _
  ByRef lpNumberOfBytesRead As Long, _
  ByVal lpOverlapped As Long _
) As Boolean

Public Sub run2()
  Dim hSerialPort As Long
  Dim sa As SECURITY_ATTRIBUTES
  Dim success As Long
  Dim buffer(100) As Byte
  Dim byteswritten As Long
  Dim bytesread As Long

  'Create NULL Security Attributes
  With sa
    .bInheritHandle = False
    .lpSecurityDescriptor = 0
    .nLength = Len(sa)
  End With

  hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0&, sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  If hSerialPort = -1 Then
    Debug.Print "PROBLEM "
  Else
    Debug.Print "valid handle "; hSerialPort
  End If
  
  buffer(0) = Asc("A")
  buffer(1) = Asc("T")
  buffer(2) = 13
  buffer(3) = 0
  
  success = WriteFile(hSerialPort, buffer, 3, byteswritten, 0)
  If (Not success) Then
    Debug.Print "PROBLEM "
  End If
  
  buffer(0) = 0
  success = ReadFile(hSerialPort, buffer, byteswritten, bytesread, 0)
  If (success) Then
    Text1.text = ("BytesRead: " & bytesread)
  Else
    Debug.Print "PROBLEM "
  End If

  CloseHandle hSerialPort
End Sub
BTW: I don't do a lot of 'C' style calls from VB. I notice in your call to CreateFile that you pass a parameter(ShareMode) as 0&. Does the '&' create a pointer to the value (and if so, how would you dereference the pointer)? Mostly guessing here.
 
I forgot about this thread...

It turns out that my baudrate was way off. I couldn't have known this until they sent me the documentation (which for some reason they were reluctant to do).

It works just fine now that I have the baud rate correct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top