Wanxi

Hi everyone,

My Problem:

How to get only specific data from a whole line

eg. I got --> "00 1F 00 5D 25 4E 3C 23..." but i only want 00 1F, the next time i will only wan 00 5D...

Anyone got this idea

p/s:

The data "00 1F 00 5D 25 4E 3C 23.." will keep adding on (update)..

So once it update, i will only take the updated data only..

Thanks




Re: Visual Basic Express Edition How to get specific data from a line?

ReneeC

You have not mentioned your datatypes.




Re: Visual Basic Express Edition How to get specific data from a line?

Wanxi

Hi,

The "00 1F 00 5D 25 4E 3C 23" are Hex data.. i need to convert 00 1F to decimal than save into excel..






Re: Visual Basic Express Edition How to get specific data from a line?

Solitaire

I prepared a mock-up of your program. Textbox1 represents the current data.

Button1 displays the first unread data item in Textbox2 and removes it from TextBox1. It converts that value into hexadecimal and displays that in TextBox3.

Textbox4 is for new incoming data, which is added on to TextBox1 one at a time. The mock-up enables you to manually add the data one at a time with Button2.


Code Block

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

TextBox1.Text = "00 1F 00 5D 25 4E 3C 23 "

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim oldat As String

Dim newdat As String = ""

Dim decnum As Double

TextBox4.Clear()

TextBox1.Text = Trim(TextBox1.Text) & " "

oldat = TextBox1.Text

If oldat = "" Then newdat = ""

If oldat.Length >= 4 Then

newdat = oldat.Substring(0, 5) '& oldat.Substring(3, 2)

End If

TextBox2.Text = newdat

If oldat.Length > 5 Then

oldat = oldat.Substring(6)

Else

oldat = ""

End If

TextBox1.Text = oldat

newdat = TextBox2.Text

If newdat <> "" Then

decnum = Val("&H" & newdat & "&")

TextBox3.Text = decnum.ToString

Else

TextBox3.Clear()

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

If TextBox4.Text.Length = 5 Then

TextBox1.Text &= TextBox4.Text & " "

End If

'text must include 2 hex digits, a space, 2 hex digits

End Sub






Re: Visual Basic Express Edition How to get specific data from a line?

Carsten Kanstrup

Wanxi

Why do you convert the data from your serial port to a hex string if you want to put them in Excel In that case, it would be much easier to merge the two bytes together to an integer when you receive them and then send this integer to Excel. Something like this should do it:

Dim ExcelValue As Integer

ExcelValue = HighByte

ExcelValue = (ExcelValue << 8) + LowByte

Note

ExcelValue = (HighByte << 8) + LowByte

may also work, but it depends on when the compiler converts HighByte to an integer (if the shift operation is done before the integer conversion it will not work because a byte shifted left 8 places is always 0).

There is nothing like a decimal integer. Everything in a computer is binary. It is only at display time you convert the binary values to a more user friendly format like Hex or decimal. If you group the data bits together in 4-bit nibbles it is Hexadecimal, if you group them together in 3-bit groups it is Octal - ReneeC will cry a little tear :-)





Re: Visual Basic Express Edition How to get specific data from a line?

JohnWein

What is the format of the data Is it bytes or is it text Is it a string like this:

S = "0" & "0" & " " & "1" & "F" & . . .

or is it Big Endian byte data

If it is a string then it needs to be split on the spaces to get bytes.

Where is the data coming from

More information would help us help you more.





Re: Visual Basic Express Edition How to get specific data from a line?

Wanxi

Hi,

-------------------------------------------------------------------------------------------------------------------------------------------

You said:

Why do you convert the data from your serial port to a hex string if you want to put them in Excel In that case, it would be much easier to merge the two bytes together to an integer when you receive them and then send this integer to Excel.

--------------------------------------------------------------------------------------------------------------------------------------------

Ya, I'm going to do something as you said..

The progress is like this:

Hardware(Hex data) send to vb >> VB TextBox1(Show in Hex) save to >> Excel document (In Decimal) >> Zedgraph(last)

p/s:

My Questions:

1) How to save in Excel ... Last time i save it in text doc, so i use ".txt".... what about excel ".excel "

2) Now when i receive 30 41 the decimal i saved is 12353.... is this correct (I just not confident >.<)

Thank you






Re: Visual Basic Express Edition How to get specific data from a line?

Wanxi

Hi Solitaire,

Thanks for your code..

But i cannot have so many textbox inside my application..

I tried to figure out the meaning of the code..

It seems abit hard for me..

Can you please put some comment at behind the code

And is there anyway not to take off the old code

eg. 00 1F 00 5D 25 4E 3C 23

After click the button it will still the same --> 00 1F 00 5D 25 4E 3C 23

Thank you






Re: Visual Basic Express Edition How to get specific data from a line?

Carsten Kanstrup

Wanxi

Your two questions:

1) I don't know anything about Excel input, can't help you on that.

2) Yes. It is easy to verify by means of the Windows calculator. Be sure it is scientific (show menu), switch to Hex, type in 3041 and then switch to Dec (decimal).





Re: Visual Basic Express Edition How to get specific data from a line?

Solitaire

The code I posted was a mock-up, not intended for you to actually use as it is. You were supposed to study the code and see how it works. The textboxes were only included as a means of input and output. Your program provides its own means of input and output, which you did not specify.

Sorry, I am unable to provide any further assistance.






Re: Visual Basic Express Edition How to get specific data from a line?

Wanxi

Hi Kanstrup,

For the saving in Excel part i do in this way:

I not sure ist this is the correct way to save into excel file..

Bcos i got some error in the Excel when i open it =(

Although it success, but when i open the excel.. (but is not normal open, bcos i got the "space")
The data will come out with some weird things: "i 17:48:55:50 12337"
But if i open with text document, the "i " will not be there..
What i think is even i save in excel, but the data still in text format..
How to change it to real excel format.. and how to get rid of the "i "
And how to make excel recognize the "space" between the TIME and the DEC DATA
So that i will save it into 2 column --> TIME and DEC DATA

Thanks

Code Block

Dim FilePath As String = "..\MyFile"

Dim FileName As String = FilePath & "\" & Now.ToString("ddMMyyyy") & ".xls"

If My.Computer.FileSystem.DirectoryExists(FilePath) = False Then ' If there is no this file(Path: ddMMyyyy.txt)

My.Computer.FileSystem.CreateDirectory(FilePath) ' Create the file (Path: ddMMyyyy.txt)

End If

Dim sw As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(FileName, True) ' True to make it not overwrite the previous text

Dim HexValue As String

Dim DecimalValue As Integer

HexValue = TextBox1.Text

DecimalValue = CDec(Val("&H" & HexValue)) ' &H means declare the value is in Hex

sw.Write(Now.ToString("HH:mm:ss:ff") & " " & DecimalValue)

sw.Close()






Re: Visual Basic Express Edition How to get specific data from a line?

Wanxi

Hi Solitaire ^^

Thanks for your mock-up code..

Below is my program ^^ and it works.. haha..

Code Block

Dim oldat As String

Dim newdat As String = ""

Dim HexValue As String

Dim DecimalValue As Integer

TextBox1.AppendText(Buffer)

TextBox1.Text = Trim(TextBox1.Text) & " "

oldat = TextBox1.Text ' oldat is the whole received data string

If oldat.Length >= 4 Then

newdat = oldat.Substring(0, 5) '& oldat.Substring(3, 2)

End If

HexValue = newdat ' newdat is the 2byte hex only, show out at tb2

Dim FilePath As String = "..\MyFile"

Dim FileName As String = FilePath & "\" & Now.ToString("ddMMyyyy") & ".txt"

If My.Computer.FileSystem.DirectoryExists(FilePath) = False Then ' If there is no this file(Path: ddMMyyyy.txt)

My.Computer.FileSystem.CreateDirectory(FilePath) ' Create the file (Path: ddMMyyyy.txt)

End If

Dim sw As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(FileName, True) ' True to make it not overwrite the previous text

DecimalValue = CDec(Val("&H" & HexValue)) ' &H means declare the value is in Hex

sw.WriteLine(Now.ToString("HH:mm:ss:ff") & " " & DecimalValue)

sw.Close()