PDA

View Full Version : VB Binary Serial Help for someone



air1kdf
- 15th December 2005, 02:37
I have been reading a lot of posts on here absorbing information like a sponge, so I thought that I could give back a little. I wanted to send serial data via hserout. All is well. I'm using a 4 Mhz oscillator with a 16F876A chip. The serial data thing was suprpising for me when sending a word. I wanted to save some bandwidth, so I'm sending binary data. If you specify.
hserout [MyWord]
It will only send the LowByte. So you will need to send it as...
hserout [MyWord.lowbyte, MyWord.highbyte]
or
hserout [MyWord.Byte0, MyWord.Byte1]

First the connect string needs to be a little different in VB. I use this.
MSComm1.Settings = "19200,n,8,1"
MSComm1.DTREnable = False
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.CommPort = 4
MSComm1.PortOpen = True


Private Sub MSComm1_OnComm()
Dim ByteArray() As Byte
Select Case MSComm1.CommEvent
Case comEvReceive
ByteArray = MSComm1.Input
...

This is the VB function that I wrote.
'Convert 2 bytes into a 4 byte vb long
Private Function MakeWord(LowByte As Byte, HighByte As Byte) As Long
MakeWord = (HighByte * &H100&) Or LowByte
End Function

Now I am new to PIC programming, but am a professional programmer. There may be a register to set or something to send the whole word in one shot. But I noticed that every example that I saw here used DEC MyWord, #MyWord, or something similiar, which kills bandwidth.

This information would have saved me a couple of days. Enjoy!

Regards,
Kurt

Luciano
- 15th December 2005, 08:07
Hi,

The RS-232C standard describes a communication method where information is sent bit by bit on a physical channel. The information must be broken up in data words. The length of a data word is variable. On PC's a length between 5 and 8 bits can be selected. This length is the netto information length of each word. For proper transfer additional bits are added for synchronisation and error checking purposes.

To send your 16-bit value over RS232 you will need two data words.

1 START BIT + 8 DATA BITS + 1 STOP BIT (total 10 bits)
1 START BIT + 8 DATA BITS + 1 STOP BIT (total 10 bits)

You will also need to ensure the integrity and correctness of the transmitted data.

Example of a simple ASCII message-based protocol suitable for use on serial lines:
http://www.embedded.com/1999/9911/9911feat3.htm


Best regards,

Luciano

air1kdf
- 15th December 2005, 16:04
I realize that it is 2 bytes. I just didn't expect the compiler to see it as one byte.

hserout [MyWord]
will only send the lowbyte

hserout [#MyWord]
Will send upto 5 bytes depending on value

hserout ["Hello"]
Will send 5 bytes.

The first line isn't so obvious, and one that I've seen many people try, and fail on this message board. It always results in them sending text instead.