PDA

Click to See Complete Forum and Search --> : string problems with winsock


jeroen684
Nov 20th, 2007, 10:48 AM
Hi all,

I´m sending data to the PC from an embedded application. The data contains a string (node name, MAC address) and some user data. The string contains only ascii characters, but the data can be anything from 0 to 0xFF (255). I have been converting the string to a variable (int) in VB by doing Asc(mid(strData, 19,1)) which works fine in Germany. But, if the codepage is different it doesn´t work anymore, all sorts of strange things happen. I even get the wrong value from Len(strData) somehow. e.g. in:

Private Sub wsRunning_DataArrival(ByVal bytesTotal As Long)
On Error GoTo error
wsRunning.GetData strData, vbString

Now bytesTotal is 43 (which is the amount of bytes sent, so correct), but Len(strData) returns 42..... possibly because of illegal characters?

Anyway, I am freaked by this... I tried CInt(Mid(strData,18,1)) but only get type mismatch errors..... help!!!!

thanks... any suggestions would be greatly appreciated!

CVMichael
Nov 20th, 2007, 12:18 PM
Try something like this:

Private Sub wsRunning_DataArrival(ByVal bytesTotal As Long)
Dim DataArr() As Byte
Dim strData As String

On Error GoTo Error
wsRunning.GetData DataArr, vbArray + vbByte, bytesTotal

strData = StrConv(DataArr, vbUnicode)
End Sub

jeroen684
Nov 23rd, 2007, 06:01 AM
Problem solved!
Now it is:

ReDim wsRunningData(48)
wsRunning.GetData wsRunningData, vbArray + vbByte, 48

Since I know the expected data string is either 43 or 48 bytes, I limited the max amount of data to this.

Then I can convert one part of the array to a string:

For i = 0 To 2
strDataVisual = strDataVisual & Chr(wsRunningData(i))
mask = mask + Chr(wsRunningData(i))
Next

The rest is just used as an array of bytes.

Thanks for your help!

jeroen