Hi, all! Running the code below, I have the problem that when exiting the For loop, the received string disappear from the strTotal variable. Can anyone please guide me on how to solve this? Thank you in advance.
Imports System Imports System.IO.Ports Imports System.Threading Public Class Form1 Private serialport1 AsNew SerialPort PublicShared strtotal AsString Private Sub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim strData AsString Dim array1() AsByte Dim nr AsInteger With serialport1 .PortName = "COM1" .BaudRate = 1200 .Parity = Parity.None .DataBits = 8 .StopBits = 2 .DtrEnable = True EndWith
'Somehow the 4 lines below cannot be a part of the With block. Can anyone tell me why?
serialport1.Open() serialport1.Write("A07E") 'codes to trigger the reception of data from the serial device serialport1.Write("G")
serialport1.RtsEnable = True ReDim array1(23)' Should preferably have been ReDim array1(serialport1.BytesToRead) but this doesn't work, apparently
nr = 0 strtotal =""
For nr = 0 To array1.Length serialport1.BaseStream.Read(array1, nr, 1) strData = CStr(ChrW(array1(nr))) strtotal = strtotal + strData
Debug.Print(strtotal) 'For testing that the loop works. The string is printed for every loop as expected, one chr more per loop.EndSub
Next nr
Debug.Print(strtotal & "total") 'To test if the total string is stored. IT IS NOT???