Results 1 to 29 of 29

Thread: VB6.0: MSComm gives me ascii character but i need single data type !!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    Unhappy 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 !???

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    Using CSng should help.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    Quote Originally Posted by dee-u View Post
    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 !

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    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 !

  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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??

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    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

  8. #8
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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??

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    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 .

  10. #10
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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??

  11. #11
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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??

  12. #12
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    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)

  13. #13
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    You know I could kick myself because dee-u posted this suggestion early in this thread. The OP said that it threw an error when he used it and when I checked the library their examples didn't show a string to single conversion. They showed examples of other numeric types converted to singles. Thus, I didn't even try it... Duh!!!!!!!
    <--- 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??

  14. #14
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    Quote Originally Posted by AamirKhan View Post
    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??

  15. #15

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    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

  16. #16

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    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 &#231;
    Serial is &#224;
    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 &#244;
    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 &#192;
    Serial is
    Serial is &#223;
    Serial is Q
    Serial is U
    Last edited by AamirKhan; Nov 22nd, 2009 at 05:33 AM.

  17. #17
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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??

  18. #18
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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??

  19. #19

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    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

  20. #20

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    i am using usb to serial converter cable

  21. #21

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!


  22. #22
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    Quote Originally Posted by AamirKhan View Post
    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??

  23. #23
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    Quote Originally Posted by AamirKhan View Post
    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??

  24. #24
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  25. #25
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    Hi Milk, LTNS. Actually, he could use CSng() if the data was sent as a string but I doubt that he can.

    I've never used the Hex conversion method that you described, as I use a method that the OP posted (link) earlier in this thread. For MSComm it's worked OK for me, but then I'm not a HEX wiz.

    Thanks for the explanation and the link of Endianness.
    Last edited by CDRIVE; Nov 22nd, 2009 at 08:29 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??

  26. #26
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  27. #27
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: VB6.0: MSComm gives me ascii character but i need single data type !!

    You can see the sort of scrambling you get when you send or receive binary data using String variables once you get above 127:
    Attached Images Attached Images  
    Attached Files Attached Files

  28. #28
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  29. #29

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    13

    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
  •  



Click Here to Expand Forum to Full Width