B4R Question Cannot receive serial data from B4R to .Net program

Jakes72

Member
Licensed User
Hi Guys,

I am having a problem trying to receive serial data from B4R to my VB.Net program.
I either get nothing or something like 'Recieved???' or 0's.

But when I use the normal Arduino IDE and use:
Serial.println("Test');
It works 100% in my .Net program. In my .Net code I basically just do a Port.ReadLine call, which works for normal Serial.println. But even if I try Port.Read or Port.ReadByte for B4R I still get rubbish.

I use the following simple code in B4R:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private SerialStream As AsyncStreams
    Private Timer1 As Timer
End Sub


Private Sub AppStart
    Serial1.Initialize(9600)
    SerialStream.Initialize(Serial1.Stream, "SerialStream_DataReceived", "SerialStream_Error")
    Timer1.Initialize("Timer1_Tick", 1000) 
    Timer1.Enabled = True
End Sub


Sub Timer1_Tick    
    SerialStream.Write(Array As Byte("Test")) 
End Sub


Sub SerialStream_DataReceived (Buffer() As Byte)
    Log("Received: ", Buffer)
End Sub


Sub SerialStream_Error
    Log("error")
End Sub
What am I doing wrong here?
 

Jakes72

Member
Licensed User
OK so eureka! I solved my own problem! I post the solution here, for others who might be battling with this.

So what I did was change this line of code:
B4X:
SerialStream.Write(Array As Byte("Test"))
...to this....

B4X:
SerialStream.Write("Test".GetBytes)
Now why the one works and not the other is a mystery to me, as they look the same??
I also added this to signify End of line:

B4X:
SerialStream.Write(Array As Byte(10))
On .Net side I now just use:
Port1.NewLine = Chr(10)
Port.Readline
 

Jakes72

Member
Licensed User
Thanks Peter I appreciate it! Yeah it took me quite a bit of head scratching! :) Your code looks great too, I can learn something from it, and no I did not see it. In my case the bugger was not so much sending the data form the .Net program but rather receiving it! Strange that the one SerialStream.Write method works and the other not?? Well anyway, hopefully my code, as yours, can help someone else. Happy coding!!!
 

Jakes72

Member
Licensed User
Hi tigrot. Yeah that may possibly be another solution, thanks! Always more than 1 way to skin a cat! Will try it some time.
 
Top