Opening a serial port and print something in it

M

Mota

Hi;
How to open a COM,such as com3 that my modem is connected to,to print and
read something from it.I use this syntax to send a Hayes command to modem
and get its reply but it doesnt work:

Open "COM3:" For Output As #1
Print #1, "ATI" 'ATI is a standard Hayes command to return modem
information
Close #1
Open "COM3:" For Input As #1
Input #1, strResponse
Msgbox StrResponse
Close #1

When i use this syntax,system hangs.Deleting the line: Input #1,StrResponse
solves the hanging problem,but obviously does nothing for me.In the other
hand,Opening it for Random,rather than reopening and closing it, and trying
to read from it causes Run time Error 54 "Bad File Mode".I dont know
why.So,How to send a Hayes command to modem and get its response?
Are serial ports UnReadable?!!!Can anyone please help me?
Thank you in advance for ur help.
 
A

Adrian Jansen

Open the file for Random, but you have to use Get and Put for the I/O
statements.

You might find it easier to use the MsComm control, available if you have
VB4,5,or 6.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
M

Mota

All is about a code snippet u suggested.It didnt work and im searching for
its cause.Can u plz give me a code part to use a Hayes command?Appreciate
you.
Thank you so so much in advance for ur help.
 
A

Adrian Jansen

I had this working, just as a test. On a form with a text box called
txtMsg, and two command buttons, one to start the comms, and one to stop it.
My modem is on Com3.


Private Sub cmdComm_Click()
Dim li As String * 20

On Error GoTo Err_cmdComm_Click


Open "com3:4800,n,8,1" For Binary As #1

Do
Put #1, , "ATI"
DoEvents

Get #1, , li

Me.txtMsg = li
Loop

Close #1

Exit_cmdComm_Click:
Exit Sub

Err_cmdComm_Click:
MsgBox Err.Description
Resume Exit_cmdComm_Click

End Sub
Private Sub cmdStop_Click()
On Error GoTo Err_cmdStop_Click

Close #1
MsgBox "Comm closed"


Exit_cmdStop_Click:
Exit Sub

Err_cmdStop_Click:
MsgBox Err.Description
Resume Exit_cmdStop_Click

End Sub


--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
M

Mota

Thanx,
My modem is on com3 too.But i tried this code with all 4 coms.In any case I
got the error "file not found" for the Open Com statement.Who knows why!!!
Anyway,thank you so much for all ur helps.
THX.
 
A

Adrian Jansen

Are you certain your syntax is exactly as

Open "com3:4800,n,8,1" For Binary As #1

It sounds like the command is trying to open a file, rather than a com port.

One other thing to check, maybe the filenumber #1 is already taken on your
system. Technically you should do something like

lngFreeHandle = freefile 'get the next available file handle

then

Open "com3:4800,n,8,1" For Binary As #lngFreeHandle

I have never found this necessary, but it will depend on what other
processes are using files on your machine.


--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top