Sub AStream_NewData (Buffer() As Byte)
Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Dim i As Int = 0
Dim length As Int
length = s.Length
Dim temp As String
Select s.CharAt(i)
Case "X"
Do While (s.CharAt(i+1)<>";")
i=i+1
temp = temp & s.CharAt(i)
Loop
lblXval.Text=temp
'add extra cases
End Select
Log(length)
'LogMessage("You", temp)
End Sub
I agree, except that the original post stated "again no CRLF needed".PS. It looks to me that each packet is actually terminated by CRLF and not by semi-colon.
"10X1946;Y0;Z-38;A0;CRLF"
Was a doddle in C just reading one byte at a time, just built up required packets as I went. ( A LOT of other stuff is not so easy in C!!!!)
Sub AStream_NewText(Text As String)
Dim m As Matcher = Regex.Matcher("([A-Z])(.*?);", Text) 'fields start with uppercase letter, terminated by semicolon
Do While m.Find
HandleField(m.Group(1), m.Group(2)) 'letter, value
Loop
End Sub
Sub AStream_NewText(Text As String)
Dim FieldPattern As String = "([A-Z])(.*?);" 'fields start with uppercase letter, terminated by semicolon
Dim m As Matcher = Regex.Matcher(FieldPattern, Text)
Do While m.Find
Dim FieldLetter As String = m.Group(1)
Dim FieldValue As String = m.Group(2)
If IsNumber(FieldValue) Then 'if value is supposed to be numeric
HandleField(FieldLetter, FieldValue)
End If
Loop
End Sub
No worries.sorry posted my previous reply without looking at yours.
Surely not.Had enough of this for one day.
Will try again tomorrow
No. It's all going to work perfectly, we just need to find out where this extraneous "10" is coming from.as a read is not guaranteed to start or finish with a letter or a ; I'm going to miss data arent I?
Sub Process_Globals
Dim SerialBuffer As String 'agglomerate incoming serial data
End Sub
Sub AStream_NewData (Buffer() As Byte)
Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
SerialBuffer = SerialBuffer & s
'''Log("SerialBuffer = [" & SerialBuffer & "]")
Dim P As Int = SerialBuffer.LastIndexOf(";")
If P >= 0 Then 'found semicolon, -1 if not found
Dim FieldPattern As String = "([A-Z])(.*?);" 'fields start with uppercase letter, terminated by semicolon
Dim m As Matcher = Regex.Matcher(FieldPattern, Text)
Do While m.Find
Dim FieldLetter As String = m.Group(1)
Dim FieldValue As String = m.Group(2)
'''HandleField(FieldLetter, FieldValue)
Log("Letter = [" & FieldLetter & "], value = [" & FieldValue & "]")
End If
SerialBuffer = SerialBuffer.SubString(P + 1)
End If
End Sub
Not random bytes. Is ASCII text, eg: "X1251;Y0;Z-90;A0;". I think they're readings from or commands for CNC machinery.Why aren't you using B4XBytesBuilder? Converting random bytes to string is a big mistake.
The 0x10 looks like the byte count of your packet. Is that clue?
If you are dealing with a binary protocol, then Byte Arrays are better.Why is Converting random bytes to string a big mistake?
Close. The length is a 32-bit number, ie 4 bytes, not just one. Given the networking origin of Java, the byte order is probably big-endian, ie most significant byte first.Does prefix mode send the first byte as the length of data
After receiving the length, it then builds up the specified number of bytes in a buffer, and presents it to your program when complete.or does it make sure that the buffer has the right amount of data.
Difficult to get much simpler than SerialBuffer = SerialBuffer & s. I used to worry about efficiency, but when I actually tested the speed of Java string operations, I was surprised. They are amazingly fast. Erel was correct, asIt would be so much simpler to build strings byte by byte
Don't worry about StringBuilder for the time being. The code in this post should get you going, with or without the spurious "10"s or make-it-readable CR's or LF's.But will carry on as you suggest Emexes with an sb.
*
srx 1592908976001 5 : 88 45 49 59 49
SerialBuffer = [X-1;1]
srx 1592908976017 1 : 48
SerialBuffer = [10]
*
srx 1592908976514 6 : 88 45 49 59 49 48
SerialBuffer = [10X-1;10]
ERRRRRRRORR10X-1
*
Lol. It definitely has a quantum theory observation-affects-reality feel about it.maybe Teraterm ignores the '10'
1592923933799 : X16;Y2;Z0;A0;
1592923934799 : X17;Y2;Z0;A0;
1592923935799 : X18;Y2;Z0;A0;
1592923936799 : X19;Y2;Z0;A0;
1592923937799 : X20;Y2;Z0;A0;
1592923938802 : X21;Y2;Z0;A0;
1592923939794 : X
1592923939795 : 22
1592923939796 : ;Y2;Z0;A0;
1592923940798 : X23;Y2;Z0;A0;
1592923941799 : X24;Y2;Z0;A0;
1592923942801 : X25;Y2;Z0;A0;
1592923943796 : X26;Y2;Z0;A0;
1592923944797 : X27;Y2;Z0;A0;
1592923945796 : X28;Y2;Z0;A0;
1592923946797 : X29;Y2;Z0;A0;
1592923947798 : X30;Y2;Z0;A0;
1592923948801 : X11;Y3;Z0;A0;
1592923949801 : X12;Y3;Z0;A0;
1592923950797 : X13;Y3;Z0;A0;
1592923951797 : X14;Y3;Z0;A0;
1592923952798 : X15;Y3;Z0;A0;
1592923953798 : X16;Y3;Z0;A0;
1592923954798 : X17;Y3;Z0;A0;
1592923955798 : X18;Y3;Z0;A0;
1592923956803 : X19;Y3;Z0;A0;
1592923957793 : X
1592923957803 : 20;Y3;Z0;A0;
1592923958793 : X
1592923958803 : 21;Y3;Z0;A0;
1592923959794 : X22;Y3;Z0;A0;
1592923959804 :
1592923960797 : X23;Y3;Z0;A0;
1592923961798 : X24;Y3;Z0;A0;
1592923962802 : X25;Y3;Z0;A0;
1592923963802 : X26;Y3;Z0;A0;
1592923964799 : X27;Y3;Z0;A0;
1592923965799 : X28;Y3;Z0;A0;
1592923966802 : X29;Y3;Z0;A0;
1592923967802 : X30;Y3;Z0;A0;
1592923968802 : X11;Y4;Z0;A0;
1592923969802 : X12;Y4;Z0;A0;
1592923970795 : X13;Y4;Z0;A0;
1592923971799 : X14;Y4;Z0;A0;
1592923972799 : X15;Y4;Z0;A0;
1592923973800 : X16;Y4;Z0;A0;
1592923974793 : X17;Y4;Z0
1592923974803 : ;A0;
1592923975798 : X18;Y4;Z0;A0;
1592923976793 : X19;Y4;Z0;
1592923976803 : A0;
1592923977793 : X20;Y4;Z0;A
1592923977803 : 0;
1592923978794 : X21;Y4;Z0;A
1592923978804 : 0;
1592923979796 : X22;Y4;Z0;A0;
1592923980798 : X23;Y4;Z0;A0;
1592923981798 : X24;Y4;Z0;A0;
1592923982799 : X25;Y4;Z0;A0;
1592923983799 : X26;Y4;Z0;A0;
1592923984793 : X27
1592923984793 : ;Y4;Z0;A0;
1592923984803 :
1592923985793 : X28;Y4;Z0;A0;
1592923986794 : X29;Y4;Z0;A0;
1592923987797 : X30;Y4;Z0;A0;
1592923988797 : X11;Y5;Z0;A0;
1592923989797 : X12;Y5;Z0;A0;
1592923990798 : X13;Y5;Z0;A0;
1592923991799 : X14;Y5;Z0;A0;
1592923992799 : X15;Y5;Z0;A0;
1592923993799 : X16;Y5;Z0;A0;
1592923994791 : X
1592923994801 : 17;Y5;Z0;A0;
1592923995796 : X18;Y5;Z0;A0;
1592923996798 : X19;Y5;Z0;A0;
1592923997799 : X20;Y5;Z0;A0;
1592923998800 : X21;Y5;Z0;A0;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?