Reading data from serial port

I have a OBDII (D-CAN) to USB cable that I have been using in Windows without any issues.
Following XOJO tutorial, in Serial1’s DataAvailable, I added this code:

MsgBox "Getting Data … "
TextArea1.AppendText(me.ReadAll)

I connected the cable to the car and executed the code. USB Serial port was listed and I hit the connect button. Nothing seems to happen after that. Since I am not seeing message box "Getting Data … ", I am assuming nothing is coming in because DataAvailable is not firing. Serial Port settings are correct (Bits per second 9600, Data bits 8, Parity None, Stop bits 1). I am a newbie. Please let me know if I am missing anything here. Appreciate your help.

Make your data readable instead of reading with a nil encoding (readAll default encoding)

TextArea1.Append( EncodeHex( me.ReadAll(Nil), True ) )

Also make sure you send the right data to it using the right data type (mostly will be binary -nil encoded string-)

And check the LastErrorCode of the serial port

[quote=433465:@Derk Jochems]Make your data readable instead of reading with a nil encoding (readAll default encoding)

TextArea1.Append( EncodeHex( me.ReadAll(Nil), True ) )

Also make sure you send the right data to it using the right data type (mostly will be binary -nil encoded string-)

And check the LastErrorCode of the serial port[/quote]

Thank you. I added this code in Connect/Disconnect button.
Dim svalue as Integer
svalue = Serial1.LastErrorCode
if svalue <> 0 then
MsgBox "Serial Port Error Code is " + Chr(svalue)
end if

MsgBox is not displayed. So I think it is ok. But DataAvailable event for Serial is not firing. I have MsgBox as the first statement for this event. It doesn’t show up.

Anybody ?

Can you elaborate what you mean by that statement?

Do you mean you have been using it with Xojo without issues and only had issues when you moved to using DataAvailable (I would presume from polling)? Do you mean you have been using it with other Serial Terminals (e.g. Teraterm, Coolterm) but can’t seem to receive anything when moving the serial interface to Xojo?

[quote=433515:@LangueR]Can you elaborate what you mean by that statement?

Do you mean you have been using it with Xojo without issues and only had issues when you moved to using DataAvailable (I would presume from polling)? Do you mean you have been using it with other Serial Terminals (e.g. Teraterm, Coolterm) but can’t seem to receive anything when moving the serial interface to Xojo?[/quote]

Sure. I have been using OBDII (D-CAN) to USB cable with other Windows software. But when I tried to use the same cable with Xojo (MAC), DataAvailable event is not getting fired.
I followed serial port tutorial. When I run xojo application, it shows up in the list of serial ports. But I don’t see any data coming in.
I used this code to check and it is always zero.

Dim sbytes as Integer
sbytes = serial1.BytesAvailable
if sbytes > 0 then
MsgBox "Serial Port Bytes " + str(sbytes)
end if

Am I missing anything?

Usually you need to send a command to the OBD (on board diagnostics) computer to get it to respond with some data.
Depending on what sftware you have been using this cable with that software may already be sending an initial command to the OBD system.
OBD can be using any one of 5 different protocols so determining which one is in use is probably the first thing to figure out.

There are lots of online resources available about programming OBD II

This one seems to be a decent starting point
https://learn.sparkfun.com/tutorials/getting-started-with-obd-ii/all#resources-and-going-further

[quote=433589:@Norman Palardy]Usually you need to send a command to the OBD (on board diagnostics) computer to get it to respond with some data.
Depending on what sftware you have been using this cable with that software may already be sending an initial command to the OBD system.
OBD can be using any one of 5 different protocols so determining which one is in use is probably the first thing to figure out.

There are lots of online resources available about programming OBD II

This one seems to be a decent starting point
https://learn.sparkfun.com/tutorials/getting-started-with-obd-ii/all#resources-and-going-further[/quote]

Thanks! Protocol is ISO 15765-4 CAN. Protocol can be set to auto by “AT SP 0”. How do I send these commands from xojo application? Any example for sending this command and get basic data like coolant temperature (0105) ?

Have you tried writing to the serial port ?
http://documentation.xojo.com/api/deprecated/serial.html

Serial.Write should do

I sent command using Serial1.write(“AT DP”) (display protocol).
DataAvailable event fires up and TextArea1.AppendText(me.ReadAll) displays ‘AT DP’.
Am I missing any step?

Sounds like the ODP is configured to echo everything

I’d try another command and see what results you get

But the point is that the ODP doesnt just send data without being told what to do by sending it a command
You send a command & it replies
You send another & it replies
and so on

I tried several commands … the same are echo’d back.

You will of course have to send OBD commands:
https://learn.sparkfun.com/tutorials/obd-ii-uart-hookup-guide/obd-commands

[quote=433645:@Ulrich Bogun]You will of course have to send OBD commands:
https://learn.sparkfun.com/tutorials/obd-ii-uart-hookup-guide/obd-commands[/quote]

I sent OBD command like this:
Serial1.Write(“010C”)

DataAvailable event fired up and getting back 010C which is only an echo of the command sent.

Do you finish the command with an EndOfline.Windows (CR/LF)?
If not, the diagnostic will only echo the command but not react on it.

I’m willing to bet you need a terminating character(s) at the end of your command.

Try adding a CR+LF to the command, something like…

dim cmd as String = “010C” + chr(10) + chr(13)

serial1.Write(cmd)

…syntax may be off, just shooting from the hip.

I am on Mac. I tried
dim cmd as String = “010C” + chr(10) + chr(13)
serial1.Write(cmd)

No response :frowning:

According to https://learn.sparkfun.com/tutorials/obd-ii-uart-hookup-guide/first-communcation, you should start with ATZ (+CR/LF). Does that change anything?

Tried ATZ also. No response :frowning:
In Windows, using another software, I verified cable is working fine and receiving data.
For some reason xojo application is not receiving any data :frowning:

I tried in Windows also. Added endofline.Windows to end of every command. Except an echo, nothing is coming into xojo application.