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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial Communication using VB6

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Does anyone know how to configure VB6 so that is reads binary data from the serial port as 0's and 1' rather than just 1's. Also, can you do bit manipulation in VB6
 
The MSComm Object has an InputMode Property that can be set to string or binary. Yes you can do Bitwise data manipulation.
 
The project i am working on requires sending a stream of binary data into the serial port(ex. ID number). When I configure the input mode of VB to binary, somehow the data stream is reduced to only 1's, leaving the ouput as ?????. If the data entering the serial port is coming in at 1200 bps with 5 data bits and odd parity, how can I configure VB to recognize the zeros and ones? Can the input buffer receive 0's and 1's without converting them to ASCI? Can I display the received serial transmission in a text box? How do you do bit manipulation?
 
Is there a particular reason you are using 5 data bits? The reason I ask is that is will be easier to explain what is going on if you were using 8 bits. If not I will try to explain it as best I can. Let me know.
 
Yes, there are only 5 data bits used in the transmission. Could I store the complete data transmission into a binary file and then use bit manipulation to packet it into the 5-bit BCD code I am after?
 
OK Here goes. When you set the MScomm Object to Binary it tranmits and recieves data as type Byte this implies 8 bits of data. For example if you send a 1 (which is type string) to the MScomm.output property it gets converted from the ASCII code for the String "1" which is 49 Decimal, 31 Hex or 00110001 binary by VB. When you set the Data bits for the MScomm object to 5 you are actually putting 10001 in the output property. The MScomm object pads this data to make it a full byte which then amounts to 00010001. This is what you are actually sending out of the port. The same thing applies to the input property in the reverse order. The data is then pulled from the input property in an array of Bytes. The problem with this is that some of the data was trimed(00110001 is replaced with 00010001) and the MScomm object puts this data into a Byte array containing the value 11 which is the Hex value for 00010001 which is the trimed value. The ????? value is recieved because(I'm assuming you are trying to recieve '11111')the default ParityReplace property for the MScomm object is the '?'. This is the string character that is read when the MScomm object cannot interpret the data. Clear as Mud??

If you are looking to use just 5 bit you will need to use bitwise manipulation for you Input and Output strings.
If you can give me a little more detail as to if you are sending or recieving or both I can give you a better direction as to how to manipulate your data stings.
 
Great explanation. There are input pulses coming into the serial port of the computer. The output signal will be completly different from the input. Meaning, that the output will consist of either a 1 or 0(on/off). Right now I am only concerend with converting the input pulses into a 5-bit BCD code with odd parity. I had an idea of using bit manipulation, but did not know how to approach the problem.
 
Try using this code to read from the com port.

'Where cmdRead is the name of a command button on the form
Private Sub cmdRead_Click()
Dim bytInput() As Byte
Dim bytElement As Byte
Dim iX As Long
Dim iY As Long
Dim iL As Long
Dim sResult As String
Dim sSpace As String

'Where comSerial is the name of your MSComm Control

If comSerial.PortOpen = False Then
comSerial.PortOpen = True
End If

'bytInput will be an Array of bytes

bytInput = comSerial.Input

'iX is the number of array elements

iX = UBound(bytInput(), 1)

'Cycle thru each element in the bytInput Array

For iY = 0 To iX
bytElement = bytInput(iY)

'Each bytElement will have 8 bits check each one

For iL = 1 To 8

'Add a comma to display each digit

If iL = 4 Then
sSpace = ", "
Else
sSpace = " "
End If

'Create an Output String

sResult = sSpace & Abs(CInt(BitOn(CLng(bytElement), iL))) & sResult
Next
Next

'txtRead is the name of a textbox control on the form

txtRead.Text = sResult

End Sub



'This Function Does Bitwise manipulation of 'Number'
'Return True if 'Bit' is On, False if 'Bit' is off
Function BitOn(Number As Long, Bit As Long) As Boolean
Dim iX As Long
Dim iY As Long

iY = 1
For iX = 1 To Bit - 1
iY = iY * 2
Next
If Number And iY Then BitOn = True Else BitOn = False
End Function

If you place this code in your form you will see the result of the data read off the input of the port in the form of two digit byte ie 0 0 0 1 , 1 0 1 1
Give this a try and see how thing turn out
 
The code worked out great except for one detail. The bits were organized in a set of 8 rather than 4. However, I have another problem. The data seems to be differnet every time it comes in. I am thinking that it has to do with the clock on the external device. Is there any way to emulate a clock in VB (maybe not). Here is a better explanation of what is going on.

I have a stream of data coming into the serial port. It is about 13 characters when decoded in 5-bit BCD format. I know this because I used a digital oscilloscope to view and decode the entire stream of data by using the device's external clock. When this information is transmitted from the device to the computer, it goes through a communication chip to bring the signal up to RS-232 standards. Ok, there is a clock on the transmitting device that is used to determine the correct data that is to be sent. However, the communication chip only allows one signal to be converted to RS-232 levels, which means the data is sent in without the clock. Would VB recognize the difference in pulse widths or can a program be written such that when the first start bit is detected, the pulse width is recorded and then used to determine the rest of the data scheme.
 
Will the communication chip allow handshaking or do you not want to get into that? What did you mean when you said "the bits were organized in a set of 8 rather than 4"? Are you refering to my code or the actual data on the input property? What is the clock pulse frequency? I don't know of any way to check the pulse width specifically but thats not to say that there is not a way to work around the situation. Anything is possible, the problem is I only have one lifetime.
 
No, the communication chip will not allow handshaking. The frequency of the clock is always different for each transmission. Going back to what you said about bitwise manipulation, if I set the Comm port to recieve with the parameters of (1200,n,8,1) then I should be able to see the complete data transmission without having VB alter it, correct? Then, I can use bitwise manipulation to packet the data in 5-bits. Is that what you are implying?
 
That is correct. The data in and out of the port will always be in 'chunks' of 8, all the you achive by setting the data bits to a setting other that 8 is 'zero out' the number of undesired bits. ie Data 11110101 at 7 bits would be 01110101, at 6 bits would be 00110101 etc. Try doing this it might clear things up some. Place a debug break point at the line(from the code above, or if you modified it)

bytInput = comSerial.Input

When the break occurs single step('F8') and look at the 'bytInput' Array() in the local debug window by drilling down on the tree. Each array element will be a single byte and its value will be a decimal value. If you do this with 8 data bits and then with 5 data bit you will get an idea of what is going on. Anything is possible, the problem is I only have one lifetime.
 
Since I have no way of synchronizing the data with the clock through the serial port, how difficult would it be to acess the parallel port(LPT1) using VB.
 
No Problem. I have never tried to anything else with the parallel port other then output to a printer. If I had to guess though I would say you would not be able to get what you are looking for from a parallel port. Anything is possible, the problem is I only have one lifetime.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top