|
-
Nov 20th, 2009, 01:02 AM
#1
Thread Starter
New Member
VB6.0: MSComm gives me ascii character but i need single data type !!
Hello , i am new to VB 6.0 and the code i am working on works on sigle data type and i am using serial communication that send me ascii numbers and i need to convert the ascii to single. The Code is :
Private Sub WithoutGE_Click()
Dim serialflag As Boolean
Dim serportname As String
If MSComm1.PortOpen Then
MSComm1.PortOpen = False
End If
MSComm1.CommPort = 5 'Defined COM port available on PC
MSComm1.InputLen = 1
MSComm1.Settings = "115200, N, 8, 1"
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() 'on serial event
If (MSComm1.CommEvent = comEvReceive) Then ' if something received
'serialData = MSComm1.Input shows me data type is byte i have mismatch error
'serialdata is variant error is not observed
serialByteData = MSComm1.Input
End If
End Sub
What should i do need help . Ascii to Byte and Byte to single ! but how !???
-
Nov 20th, 2009, 01:28 AM
#2
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
-
Nov 20th, 2009, 01:33 AM
#3
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
 Originally Posted by dee-u
Using CSng should help.
yea i did but it gives me "Data Type Mis-match error"
in the code
serialdata can be a variant or a string but it cant be a byte.
and when i use
Dim serialSigle as Single
and then in the OnComm
if i place
serialSingle = Csng(serialData)
its gives me Data type mis match error !
-
Nov 20th, 2009, 01:59 AM
#4
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Have a look at this Microsoft article on MSComm, it looks like you need to get the value in comEvReceive event.
You can post a sample value of InBuff after that.
-
Nov 20th, 2009, 06:58 AM
#5
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
thanks for ur reply , but can you give me a code , i am new to visual basic coding and confused too !
-
Nov 20th, 2009, 09:50 AM
#6
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Forgive me if I misunderstand your question. If MSComm is receiving ASCII chrs then can't you use the Asc() function? Since it only returns the first chr in a string you will have to do some string concatenation. When I get home from work I'll play with MSComm to see if I can parse it in there. In the interim, someone may give you a better solution.
Code:
Option Explicit
Private Sub Command1_Click()
Dim intMyNum As Integer
Dim myStr As String
myStr = "2" ' ASCII val
intMyNum = Asc(myStr) ' Returns 50.
MsgBox (intMyNum)
End Sub
From the Library:
Asc Function
Returns an Integer representing the character code corresponding to the first letter in a string.
Syntax
Asc(string)
Last edited by CDRIVE; Nov 22nd, 2009 at 11:20 PM.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 20th, 2009, 11:33 PM
#7
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Thanks , for ur reply.
and how can i use them to make them single .
if my serial data that came from another software/hardware was -0.02934 or 2987.98
then how can i get them back ! in visual studio 2005 it is kinda lot easier but as i am doing my work in vb6 its kinda confusing.
and one thing more in vb6 can i work in bit level i mean i make up my own function that concatenates it. or can i work in addressing level , for example i make up a pointer and that pointer points to that port input and from there it takes the single value. (just a noob question)
Aamir
-
Nov 21st, 2009, 12:46 AM
#8
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
I should have asked you this before because we don't want to be chasing a red Herring.
(1) What device is sending the data to your PC?
If it's a microcontroller it will be sending ASCII or Byte data.
(2) Is the data numeric ASCII chars or letter ASCII chars or both?
As far as your question regarding converting to a Single is concerned, I declared intMyNum as an Integer because the Asc(String) function won't return a value that requires a Single. Please answer the above questions and then we can work on converting your data to a Long, if we need to.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 21st, 2009, 03:46 AM
#9
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Actually its matlab for now and then in further days it would be a radio link hardware: in complete the project is as follows ;
Matlab is being for simulation purposes applying 6-DOF equation in Aerosim that send me data in ascii , and i can see the data in vb 6 by using debug.print command. the data contains latitude ,longitude and some other things. the problem i am faing that i need it in single (4 BYte floating) and data Matlab (Simulink) Com3 is sending to com5 is in ascii. so , thats the whole situation. The code was applied in Visual Studio 2005 and now its is totally applied in vb6.0 because of a reason (vb6 is easier ) but i am stuck in this situation for weeks , searched all net but could find the answer, even used AscB(MScomm.Input) and than converted it into single but that didnt help me. I would be much grateful .
-
Nov 21st, 2009, 12:51 PM
#10
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
A VB guy that I'm acquainted with uses this in his signature...
"I know just enough to be dangerous!"
Well, this solution was derived by the pick and poke method but by golly it seems to work. It seems to work by coercing VB into changing a string into a numeric value by multiplying it by 1. I'm sure that someone will surely post "Hey, there's a function for that.!", but I haven't found it.
BTW, I didn't involve MSComm in this solution because I didn't think it was relevant to your conversion problem.
Code:
Option Explicit
Dim strNum As String
Dim intNum As Integer
Dim sngResult As Single
Private Sub Command1_Click()
strNum = "-0.02934"
Me.Print "strNum is a " & TypeName(strNum)
intNum = 1
sngResult = strNum * intNum ' multiply the string by 1
Me.Print sngResult
Me.Print "sngResult is a " & TypeName(sngResult)
End Sub
Private Sub Command2_Click()
strNum = "2987.98"
Me.Print "strNum is a " & TypeName(strNum)
intNum = 1
sngResult = strNum * intNum
Me.Print sngResult
Me.Print "sngResult is a " & TypeName(sngResult)
End Sub
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 21st, 2009, 01:39 PM
#11
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
... i can see the data in vb 6 by using debug.print command. the data contains latitude ,longitude and some other things. the problem i am faing that i need it in single (4 BYte floating) and data Matlab (Simulink) Com3 is sending to com5 is in ascii. so , thats the whole situation.
Wow.
All of this is difficult to understand.
It sounds like you don't have much control over what Matlab is sending, but one or more data items it sends is required as Single data in your receiving (VB6?) program.
It sounds as if the data is arriving in one of two ways and I'm not clear which one you mean when you use phrasing like sending in ascii. Do you mean:
- Sending as a formatted text value, i.e.: -1.234
- Sending binary data that comes out scrambled in the receiving program: &D-@
- Something else???
The first case should be easy. Just parse the arriving data by spaces, commas, or whatever you have pulling out these values, then use CSng() on the "number" strings.
The second case means you need to be receiving the data in binary mode instead of text mode, using Byte arrays instead of Strings. If you use a control like MSComm with Strings you will run afoul of ANSI to Unicode translation, which is going to scramble the bit patterns of some bytes.
In the 3rd case.. well, we'd need further explanation.
Can you show what the received stuff looks like when you Debug.Print it??
-
Nov 21st, 2009, 01:42 PM
#12
Fanatic Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
I have variant conversion. IMO, CSng(strNum) is what you should use. Though I may have misunderstood some part of the discussion
Naturally, one must make sure that ALL data that comprise the number have been received in the OnComm event, before it is converted to the Single representation.
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Nov 21st, 2009, 02:24 PM
#13
-
Nov 21st, 2009, 02:55 PM
#14
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
 Originally Posted by AamirKhan
Hello , i am new to VB 6.0 and the code i am working on works on sigle data type and i am using serial communication that send me ascii numbers and i need to convert the ascii to single. The Code is :
Private Sub WithoutGE_Click()
Dim serialflag As Boolean
Dim serportname As String
If MSComm1.PortOpen Then
MSComm1.PortOpen = False
End If
MSComm1.CommPort = 5 'Defined COM port available on PC
MSComm1.InputLen = 1
MSComm1.Settings = "115200, N, 8, 1"
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() 'on serial event
If (MSComm1.CommEvent = comEvReceive) Then ' if something received
'serialData = MSComm1.Input shows me data type is byte i have mismatch error
'serialdata is variant error is not observed
serialByteData = MSComm1.Input
End If
End Sub
What should i do need help . Ascii to Byte and Byte to single ! but how !??? 
AamirKhan, reading back over this code begs the question: Is this code exactly the code in your app? If it is; serialBytedata will be read and flushed upon receipt of each char! You will never retrieve the whole number.
Also, why are you naming the input "serialBytedata" when, as you say, you're receiving string data? Reading over this post can be very confusing.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 22nd, 2009, 05:02 AM
#15
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
yea the code is same in my app . before knowing that i am getting the ascii i initialized that ways sorry if it is confusing.
Dim serialBytedata As String
-
Nov 22nd, 2009, 05:26 AM
#16
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Can you show what the received stuff looks like when you Debug.Print it??
okay . the code in my app is below and output data looks like this :
Dim serialBytedata As String
Private Sub WithoutGE_Click()
If MSComm1.PortOpen Then
MSComm1.PortOpen = False
End If
MSComm1.CommPort = 5 'Define COM port available on PC
MSComm1.InputLen = 1 'Read 1 character/byte whenever Input property is used.
'''for 0 read whole buffer
'''MSComm1.InputMode = comInputModeBinary 'Input will retrieve data in array of bytes.
MSComm1.InputMode = comInputModeText
MSComm1.Settings = "115200, N, 8, 1" '115200 Baudrate, No Parity, 8 bits data, 1 Stopbit.
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() 'on serial event
If (MSComm1.CommEvent = comEvReceive) Then ' if something received
serialBytedata = MSComm1.Input
Debug.Print ("Serial is " & serialBytedata)
End If
End Sub
Output is :
Serial is ç
Serial is à
Serial is
Serial is B
Serial is $
Serial is F
Serial is Q
Serial is U
Serial is U
Serial is Q
Serial is U
Serial is U
Serial is U
Serial is
Serial is €
Serial is ?
Serial is D
Serial is \
Serial is ô
Serial is
Serial is B
Serial is $
Serial is F
Serial is Q
Serial is U
Serial is U
Serial is U
Serial is Q
Serial is U
Serial is U
Serial is U
Serial is
Serial is Q
Serial is U
Serial is U
Serial is U
Serial is
Serial is Q
Serial is U
Serial is U
Serial is U
Serial is
Serial is €
Serial is @
Serial is D
Serial is
Serial is
Serial is
Serial is B
Serial is #
Serial is F
Serial is
Serial is À
Serial is
Serial is ß
Serial is Q
Serial is U
Last edited by AamirKhan; Nov 22nd, 2009 at 05:33 AM.
-
Nov 22nd, 2009, 12:45 PM
#17
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Geezz, I thought I was confused before! This makes absolutely no sense at all. How can you possibly be sending an Ascii string ( MSComm1.Output = "-0.02934" ) from Matlab?? Your InputMode = Text, so it should read the characters sent from Matlab as same.
(1) Are you sure that you're not sending Binary data from Matlab?
(2) Are you certain that your Comm Settings in Matlab & MSComm are identical?
Either one of these issues can result in gibberish being received. If the Settings don't agree the data received will be out of sync with the transmitter (Matlab). This will result in the received data being totally random.
Let's talk about InputLen.... The experiments that I've made with this property tells me two things.
(1) The Library description is misleading. This is what it says:
Sets and returns the number of characters the Input property reads from the receive buffer.
This statement might lead one to believe that if InputLen = 8 then it would only read the buffer if there were 8 chars in the InBuffer. The truth is the InputLen property sets the Maximum number of characters that will be read. If the InBuffer contains less than InputLen it will still read them, even if it's 1 char. If the InBuffer contained 12 chars then only the first 8 received chars would be read. Mscomm provides the InBufferCount property that can be used in an If statement to manipulate and action. With it you can specify =, <, >, >=, <= or <> then do something.
(2) I do not find the InputLen property useful in the On_Comm Event where RThreshold rules. Personally, I find it far more useful when used in Polling the InBuffer. Evidently MSDN must have agreed because the example they give is in a Command_Click event, not On_Comm. Polling is more often utilized in a TimerControl though.
FYI: I taught myself, (and I'm still teaching myself) MSComm with the use of Loopback testers and Nullmodems. There is absolutely no better way to learn the quirks of MSComm. If you plan on doing much work with the Serial Port I would highly recommend arming yourself with them. Both can be purchased on line cheap, complete with LED indicators for all your RS232 nodes.
Last edited by CDRIVE; Nov 22nd, 2009 at 06:24 PM.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 22nd, 2009, 12:55 PM
#18
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 22nd, 2009, 01:07 PM
#19
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Looking at Matlab (Simulink) com3 configuration :
Communication Port :COM3
Baud Rate : 115200
Data Bits: 8
Parity:none
Stop Bits:1
Byte Order: LittleEndian
Flow Control:none
Time Out:10
Now as i mentioned above Com3 and Com5 are virtually paired by the software
com3 is receiving the same data as com5
com3 receiving configuration in simulink are:
Communication Port: COM3
Header:none
Terminator:none
Data size:[4 1] it means an array of 4 * 1 matrix
Data Type:Single
-
Nov 22nd, 2009, 01:10 PM
#20
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
i am using usb to serial converter cable
-
Nov 22nd, 2009, 01:15 PM
#21
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
-
Nov 22nd, 2009, 04:39 PM
#22
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
 Originally Posted by AamirKhan
i am using usb to serial converter cable
USB/Serial coverters work just fine. No issue there.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 22nd, 2009, 05:02 PM
#23
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
 Originally Posted by AamirKhan
Looking at Matlab (Simulink) com3 configuration :
Byte Order: LittleEndian ' *********
Data size:[4 1] it means an array of 4 * 1 matrix
Data Type:Single
According to this, Matlab is not sending a string. Matlab is sending a Single. This is something that MSComm can't do. MSComm can only send a numeric data type as Byte data in a Byte Array. When receiving Byte data MSComm must also receive it in a Byte Array. As I see it, your options are to send the Matlab data as String and use the CSng(String) when received. Your other option is to send your Matlab data as Byte data and receive it into an array.
********* I have no idea what Byte Order is.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Nov 22nd, 2009, 07:07 PM
#24
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
If I understand this right cSng is not going to work. If MSComm can receive byte data then I think that is what should be done here.
A single is made up of four bytes. x86 architecture is little-endian so it should be just a case of copying four bytes into the memory space of a single (as opposed to reversing the order of the four bytes before copying as with big-endian)
To convert the byte array to a single array one could use RtlMoveMemory (aka CopyMemory) API or Lset with some custom UDTs
"QUUU" becomes 1.466015E+13
Looking at the posted data it might not be so simple, although it looks like binary it also looks like there might be more than just 4 byte singles in there. If it's possible to send a known sequence of singles then at least you would know what data is expected.
-
Nov 22nd, 2009, 08:25 PM
#25
-
Nov 22nd, 2009, 08:58 PM
#26
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Milk is on top of it, but the data may be scrambled as I said earlier by accepting it as text (String) data.
The easiest way to receive and accumulate the data is to accept it in binary into a Byte array then .Write it to an ADODB.Stream object of Type=adBinary. You can check the .Position property to see when you have your 4 bytes or 16 bytes or whatever accumulated. Then .Read(4) or 16 or whatever to bring back a whole Single (or 4) into a Byte array.
From there convert to Single as Milk suggests (Byte array in a UDT and LSet, or use CopyMemory). Pretty basic stuff.
The data just isn't going to arrive in chunks of the desired size reliably. The trick is to use some sort of binary stream object for accumulation, and the one ADODB provides is pretty handy. I'm amazed people aren't doing this routinely in MSComm programs. The logic is less clumsy than moving individual bytes around with loops.
-
Nov 22nd, 2009, 09:21 PM
#27
-
Nov 22nd, 2009, 09:39 PM
#28
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Oh one more thing:
Using Asc() on a String variable will attempt to "undo" the scrambling. The downside is that it uses the current locale's codepage to do this.
Within one machine this isn't a problem. But if one end sends binary using Strings and is using one set of locale settings and the other end receives it and has different locale settings... some bytes are going to get scrambled even worse.
We see this more often in people's Winsock programs, where it is more likely the two ends might be in different countries.
-
Nov 23rd, 2009, 03:27 AM
#29
Thread Starter
New Member
Re: VB6.0: MSComm gives me ascii character but i need single data type !!
Microsoft Visual Studio 2005 code is given below. I am not using Visual Studio because of some circumstances (as i mentioned before):
(Only serial code is included not whole)
The code below might help in converting it to VB6 (the difference is that it uses polling and i had done interrupt as i have to include some other code in future too.)
receive(i, j) = serialportx.ReadByte() ' Reads the byte
singlearray(k) = BitConverter.ToSingle(temp, 4 * k) ' converts the cosecutive bytes to single
(actually need to translate ReadByte() and ToSingle command in vb6)
'Code in Visual studio 2005 is
Dim receive(,) As Byte = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
Dim singlearray() As Single = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim temp() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
'--------Loading Serial Port Form--------
If serialportx.IsOpen Then
serialportx.Close()
End If
Try
With serialportx 'setting serial comm. parameters
.PortName = "COM4"
.BaudRate = 115200
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
serialportx.Open() 'opening serial port
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub serialPortX_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialportx.DataReceived
Dim i As Integer
Dim j As Integer
Dim k As Integer
If serialportx.BytesToRead > 0 Then
For i = 0 To 15
For j = 0 To 3
receive(i, j) = serialportx.ReadByte()
temp(j + i * 4) = receive(i, j)
Next
Next
For k = 0 To 15
singlearray(k) = BitConverter.ToSingle(temp, 4 * k)
Next
End If
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|