How to find specific characters(items) in string array(or list) and then merge them?
I'm having a problem recently, a logical issue so hard to determine various examples.
I need the text that is inserted in textbox, which has been altered or modified or twisted to have a proper format through line accordance and constant white spaces etc. And some cases I stuck at defining the If statement within the loop. Here's an example of a text I'm trying to reformat
And here's the code that I filter the message in the textbox.
Code:
Dim fjalet As String = txtReceive.Text
Dim status As Boolean
Dim strfilter As String = ""
Dim oldfilter As String = ""
For Each b As Char In fjalet
If status = True Then
If b <> oldfilter Then
status = False
strfilter = strfilter + b
End If
Else
strfilter = strfilter + b
End If
If b = " " Then
status = True
End If
oldfilter = b
Next
'Since the bytes that are converted after they're received in the Data Received event have white spaces
'and when trying to split them into smaller parts of that string, resulsts some logical errors by
'inserting each of the characters into designated columns of the database table thus to prevent that
'a replacement of every white space next to randomly selected character with the sole character is a must.
strfilter = strfilter.Replace(": ", ":")
strfilter = strfilter.Replace("/ ", "/")
strfilter = strfilter.Replace(" /", "/")
strfilter = strfilter.Replace("< ", "<")
strfilter = strfilter.Replace("A ", "A")
strfilter = strfilter.Replace(" i", "i")
strfilter = strfilter.Replace(" >", ">")
strfilter = strfilter.Replace(". 0", ".0")
strfilter = strfilter.Replace(". .", "..")
strfilter = strfilter.Replace("....", ".... ")
strfilter = strfilter.Replace(vbCrLf + "/", "/")
strfilter = strfilter.Replace("* ", "*")
strfilter = strfilter.Replace("-", "")
strfilter = strfilter.Replace("P ", "P")
strfilter = strfilter.Replace(" M", "M")
strfilter = strfilter.Replace("Date", "")
strfilter = strfilter.Replace("D ate", "")
strfilter = strfilter.Replace("Time", "")
strfilter = strfilter.Replace("Ext.", "")
strfilter = strfilter.Replace("CO", "")
strfilter = strfilter.Replace("Dial", "")
strfilter = strfilter.Replace("number", "")
strfilter = strfilter.Replace("Duration", "")
strfilter = strfilter.Replace("Code", "")
Dim split2 As String() = strfilter.Split(New [Char]() {" "c, CChar(vbTab), CChar(vbCrLf), CChar(vbCr), CChar(vbLf)})
Dim listfilter As New List(Of String)(split2)
Dim i As Integer = 0
Dim itemnew As String = ""
For Each item As String In split2
If item.Trim = "" Then
listfilter.RemoveAt(i)
i -= 1
End If
i += 1
Next
I'm using Microsoft Visual Studio 2010 Ultimate version with Microsoft SQL Server Management Studio. This code is being fired on mouse click event of a button control. This is a snippet of my code there are more filtering after but here is what should be changed.
Re: How to find specific characters(items) in string array(or list) and then merge th
Could you post the data received event from your comm control. It looks like the issue may be with how you are getting the data in. I doubt the smdr is coping across like you posted above, so I am thinking you might be getting some extra data in your receive event.
Re: How to find specific characters(items) in string array(or list) and then merge th
Here it is.
Code:
Dim n As Integer = srpPBX.BytesToRead() 'Declare n as Integer, system-defined class type variable and set it's value to the bytes-to-be-read from the serial port srtPBX.
Dim comBuffer(n - 1) As Byte 'Declare comBuffer as Byte, system-defined class type array set to the sum of bytes-read minus one, since bytes don't start to count from 0.
Dim BytesReceived As Integer = 0 'Declare BytesReceived as Integer, system-defined class type variable and set to 0. This is a counter for the bytes-received.
Try
BytesReceived = srpPBX.Read(comBuffer, 0, n) 'Set BytesReceived to bytes read from n , starting from 0 and storing them into comBuffer.
If BytesReceived > 0 Then 'If BytesReceived is bigger than 0 then...
x = System.Text.Encoding.ASCII.GetString(comBuffer) 'Convert the received bytes by ASCII format-encoding and set it to x string.
End If
Me.Invoke(New MethodInvoker(AddressOf ReceivedText)) 'Since the control that is needed to operate is declared in another thread an invoke is necessary here with address to ReceivedText.
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error) 'Catch exception/s that might occurr anytime by any reason into a simplified message box followed by a critical sign within.
End Try
Re: How to find specific characters(items) in string array(or list) and then merge th
i'm willing to bet.... The sub here is receiving small chunks of the data and further up stream you accidentally adding in the additional line breaks between each chunk of data....
Re: How to find specific characters(items) in string array(or list) and then merge th
Originally Posted by GremlinSA
i'm willing to bet.... The sub here is receiving small chunks of the data and further up stream you accidentally adding in the additional line breaks between each chunk of data....
Agreed. Do you do anything to the data in the "ReceivedText" function? Or is it just for putting the text in the textbox. Could you post that function as well.
Also, if you do do any formatting, could you remove that formatting, and then post the text that comes out of the SMDR.
Re: How to find specific characters(items) in string array(or list) and then merge th
Actually I do format the text that comes from the DataReceviedEvent.
I can put the function but its a bit long in case you would spend time reading and analyzing ill post it in here afterwards but im uploading the text file that I didnt touch anything just inserted the message from the DataReceivedEvent directly to the textbox and then saved it to the txt file. I can 100% assure you it's malware-free. I don't have intentions to infect somebody's PC or platform. =)
Re: How to find specific characters(items) in string array(or list) and then merge th
A very bizarre text file you have there. Either the programmers sending out the smdr are smoking some really good stuff, or you are not getting the data properly. So where is it going wrong? It could be you have the comm port set up improperly, or it is happening in the byte to ASCII conversion. TextBoxes do not play nice with a lot of ASCII characters, so I would say, our first step to diagnose this is to stop writing out to a textbox and just start building a large character array, and doing the processing from there. Would it be possible for you to write the data directly to a file instead of trying to put it into a textbox? So open a file before you open the comm port, then instead of trying to put anything in the textbox, just write the data in the "commBuffer" to the file. Then post that file so we can see it.
If that file is still wacky looking, then it probably has something to do with the comm port setup and not the Ascii conversion.
Re: How to find specific characters(items) in string array(or list) and then merge th
So you mean forget about ASCII conversion just make a WriteToFile directly after receiving those bytes and store them directly to that file? Ok that is possible for me to do but gotta wait for tomorrow since the PBX machine is located on my work PC and beside it's Sunday today that means no call made today to or from my workplace which results no record in the PBX itself.
Re: How to find specific characters(items) in string array(or list) and then merge th
Well, it is very difficult to try and figure out across the web However, yes, I think it might be in the ASCII conversion or how the COMM port is set up. No programmer in their right mind would output text in the format you have right now. I have connected to a PBX before and once you get the comm setup properly, it should send the data to you in a nice format like you wanted to see it in your first post. Now it might not come through all at once like that. So for example, you might get a sequence like this.
However, once you put it all together (as long as your don't do any thing to the data) it will look like this...
3/7/12 3:50PM 108 02 6060001108 00:00'48" ....
3/7/12 3:51PM 102 01 00491734973369 00:00'33" ....
3/7/12 3:51PM 102 02 00491734973369 00:00'43" ....
3/7/12 3:54PM 102 02 00491734973369 00:00'21" ....
3/7/12 3:55PM 102 02 044659912 00:00'24" ....
When I was doing my checking, I would build the string out, check if I had a complete line, then process that complete line. I didn't do any processing until I had the complete line, and a lot of the time, there were left over characters that I had to wait for the next comm event to get the rest of the line. I think you may be in the same situation.
Re: How to find specific characters(items) in string array(or list) and then merge th
That's completely right what you just said. I've already noticed that the data doesn't come as complete line so I stepped over debugging with breakpoints and I put one on the x(the string that receives the bytes and converts to ASCII) and I see that the message comes incomplete (Date/Time/Ext/CO/Number) and then after it comes the 2nd part (Call Duration/Code). And what I do is take the first message filter it with replacements splits and store it into another string and emptying the X string so the 2nd part comes standalone to that string and again goes through filtering and concatenates with the previously stored other string and in the end after having 7 strings in the string array after split it stores to the table of my database, to the correspondent columns.
But yeah what I doubt is that the setup of the COM port is varying. I've read somewhere that the default setup for Panasonic KX-TEM824 is baudrate:9600/databits:8/stopbits:1/parity:none/HandshakeFlow:none/ and I set DTR and RTS enabled.
Tomorrow I'll post new results and the some other code of my project. =)
Last edited by Venn; March 12th, 2012 at 04:04 AM.
Re: How to find specific characters(items) in string array(or list) and then merge th
I'm a bit confused now! It happened exactly what you said. Somehow the textbox(or afterwards filtering may corrupt it). I found out this by directly writing to a text file all the message/s that were coming through DataReceivedEvent right after the x string was getting the ASCII conversion of bytes-array. And the output message I've got was this :
No extra vbCrLf(carrieg-returns) or a twisted message. Now how can this be possible to be written in a text file and not get inserted same to a textbox? While considering e.g. first row of the message was coming in two separate parts where the first part is "3/12/12 1:49PM 101 02 < DISA incoming >" and the second part is "00:00'16" .... 0"?
What should I be aware of? What should I add or remove from my code?
Re: How to find specific characters(items) in string array(or list) and then merge th
Since we know now that it is not the PBX, I guess what is next is to figure out what exactly you are trying to do. If you want it to be displayed in a textbox, well, change the receivedText funtion that is mentioned here.
Code:
Me.Invoke(New MethodInvoker(AddressOf ReceivedText)) 'Since the control that is needed to operate is declared in another thread an invoke is necessary here with address to ReceivedText.
To just be ....
Code:
textbox1.text += x
If you need to do further processing, then in your received function, check for a line feed.
Code:
if (x.indexof(vbcrlf) > 0 then
textbox1.text += x
......
end if
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.