Sending binary data through serial port 
Author Message
 Sending binary data through serial port

Hi,

I am trying to write an app which would send bytes through the serial port
using mscomm32.
In the documentation it says that in mscomm.output = byte , byte needs to be
a variant containing a byte array.
I tried :

Private Sub Command2_Click()
Dim bit As Byte
Dim vbit
Dim fichier As String

MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
MSComm1.InputLen = 0

fichier = "C:\TestFile.bin"
Open fichier For Binary As #1
Do While Not EOF(1)

Get #1, , bit
vbit = bit

MSComm1.Output = vbit

Loop
Close #1

MSComm1.PortOpen = False

End Sub

but it stops at Mscomm1.output = vbit, saying "Runtime error 380   Invalid
property value".
Anyone knows why?
Thanks for your help
PH



Mon, 03 Jan 2005 20:30:08 GMT  
 Sending binary data through serial port
Hi,

In the documentation it says that in mscomm.output = byte , byte needs to be
a variant containing a byte array.
<<

Well, the documentation for VB6 was wrong.  And, you cannot use Variants in
.NET, anyway.

I have lots of information, and working example code that covers these
things in my book (see below).  You should use an array of type Byte to
buffer transmit and receive data.  Here is a working code fragment:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim Buffer(2) As Byte

Buffer(0) = 0

Buffer(1) = &HFF

Buffer(2) = 1

With AxMSComm1

.InputMode = MSCommLib.InputModeConstants.comInputModeBinary

.PortOpen = True

.Output = Buffer

End With

End Sub

--
Richard Grier  (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.



Mon, 03 Jan 2005 23:43:21 GMT  
 Sending binary data through serial port
Thank you Richard for your help, it works fine fine now when I use .output =
...

But when I tried:

dim buffer(2) as byte
[...]
buffer = mscomm1.input
...

I get "Can't write to array". So I tried doing it value by value:

for x = 0 to 2
buffer(x) = mscomm1.input
next x
...

But I get a mismatch at the second line(Runtime error 13).
I really don't understand.
Anyone can help me?
Thanks
PH


Quote:
> Hi,

> In the documentation it says that in mscomm.output = byte , byte needs to
be
> a variant containing a byte array.
> <<

> Well, the documentation for VB6 was wrong.  And, you cannot use Variants
in
> .NET, anyway.

> I have lots of information, and working example code that covers these
> things in my book (see below).  You should use an array of type Byte to
> buffer transmit and receive data.  Here is a working code fragment:

> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load

> Dim Buffer(2) As Byte

> Buffer(0) = 0

> Buffer(1) = &HFF

> Buffer(2) = 1

> With AxMSComm1

> .InputMode = MSCommLib.InputModeConstants.comInputModeBinary

> .PortOpen = True

> .Output = Buffer

> End With

> End Sub

> --
> Richard Grier  (Microsoft Visual Basic MVP)

> See www.hardandsoftware.net for contact information.

> Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
> Edition ISBN 1-890422-27-4 (391 pages) published February 2002.



Wed, 05 Jan 2005 00:55:51 GMT  
 Sending binary data through serial port
Hi,

dim buffer(2) as byte
[...]
buffer = mscomm1.input
<<

No, you have to dimension to the actual size...

ReDim Buffer(MSComm1.InBufferCount -1) As Byte
Buffer = MSComm1.Input

--
Richard Grier  (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.



Wed, 05 Jan 2005 01:50:16 GMT  
 Sending binary data through serial port
Also, remember:

MSComm1.InputMode = comInputModeBinary

You cannot use arrays of type Byte when in Text mode (for receive data).

--
Richard Grier  (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.



Wed, 05 Jan 2005 01:52:59 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Receiving binary data from a serial port

2. BINARY DATA OVER RS232C SERIAL PORT?

3. Binary Data to Serial Port

4. Sending serial binary data

5. send data stream via serial port?

6. Sending Data to a Serial Port to control a Digital camera

7. Sending Data to a Serial Port to control a Digital camera

8. How do you send binary data to the Com port

9. Accessing BINARY files in QBASIC and Serial Port Tx/Rx

10. I/O port, Parallel port, Serial port

11. Need to send a string to the serial COM3: port using VBA

12. Send text to serial port

 

 
Powered by phpBB® Forum Software