PDA

Click to See Complete Forum and Search --> : MSComm in class???


Rob Brown
Jul 26th, 2001, 02:58 PM
Hi,

I'm building an object oriented solution for work at the moment.

I need to send and recieve information via the com port. I can't get a comm control into my class without adding a form to my dll project (which I hate doing).

Does anyone know of a way to re-use the comm control in code or else another means of sending and recieving data through the comm port.

Any suggestions would be much appreciated.

Best regards,

Rob Brown.
:) :( :o :D ;) :p :cool: :rolleyes: :mad: :eek: :confused:

Keith Bradley
Jul 28th, 2001, 05:21 AM
Berite there may or may not be something in the attached file to interest you :) It does not use MScomm.

See ya in work on Monday :)

K

Text Included with the example :

VB Port Test, Visual Basic 6 Sample

This program will try to open a specified range of ports and
then transfer packets of 128 Bytes each.

The transfers are not overlapped (or multi-threaded) therefore if one port is not
working and is waiting for the timeout, then the whole program
waits.

For some undertmined (at this time) reason, the program does
not want to compile and run correctly unless you compile it to
P-Code.

Please be advised that this program is for reference only and
has in no way been written and/or tested for daily usage.

Keith Bradley
Jul 28th, 2001, 05:33 AM
The most reliable method in 32 bit Windows is to use the CreateFile, WriteFile, ReadFile and CloseHandle API calls. Do not try to use MSComm.ocx. It is very unreliable.

Here is a VB code snippet to initialise com1. It was written to output only
and share the port but it should give you the basics.



Dim CommDCB As DCB
Outfile = -1
Picture1.Cls
du = BuildCommDCB("9600,n,8,1", CommDCB) 'create device control block
for com1
CommDCB.fBitFields = fBinary + fOutxCtsFlow + fOutX 'file attributes
If du <> 0 Then
Outfile = CreateFile("com1:", GENERIC_READ_WRITE, FILE_SHARE_READ +
FILE_SHARE_WRITE, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0)
End If
If Outfile = -1 Then
MsgBox "Could not open com1"
Else
du = SetCommState(Outfile, CommDCB) 'initialise port
If du = 0 Then
Outfile = -1
MsgBox "Unable to set comm state"
Else
LineTrack = 0
Status.Caption = "Transmitting data"
Status.Refresh
End If
End If



To write data to the port use the WriteFile API call
To read from the port use the ReadFile API call
To close the port use the CloseHandle API call

Chris
Jul 31st, 2001, 05:18 AM
check out this thread (http://www.vbforums.com/showthread.php?s=&postid=457148#post457148) hope it does help you :)

regards,