PDA

View Full Version : basic stamp with RS-232 link



Robban
02-08-2007, 02:27 AM
hi!
trying to connect· a elm327 (see link 1) to my basic stamp 2 and found out that i would need a max323 rs-link (see link 2) so i bought myself a MAX323 and hooked it up like...

Max in--->Pin 14
Max Out-->Pin15
Max 5v--->vdd
Max gnd-->Vss

and wrote this program

rpm var word
·start:
· SEROUT 15,84,["01O0"]
· SERIN 14,84,[rpm]
··debug·hex rpm
··pause 200
GOTO start

when i hook up the computer· directly to the elm327 and send the "0100" i get back
41 BE 1F B8 10

just as i should...
but when i tried it with my BS2 nothing happends..


link 1
http://www.elmelectronics.com/DSheets/ELM327DS.pdf

link 2
http://www.embedinc.com/products/ser/index.htm

Any ideas?

Regards
Robert

steve_b
02-08-2007, 02:31 AM
The MAX323 is the wrong IC to use.
It's the MAX232 you should use.

I still have to install the PBasic program on this laptop (just got it replaced).
OK, just installed it.
The "84" in your serin/serout lines refers to the serial data going out in TRUE format. Meaning a logic 1 is a +5V level....basically TTL levels.
RS232 doesn't work that way. A Logic 1 in RS232 is actually a negative voltage (~-5/-8/-12volts depending). and alogic 0 would be the positive of those values.
The stamps don't produce a negative voltage, but instead just drop the level to 0volts at the output which is usually good enough for most devices.

So, if you are connecting your BS2 to the MAX232 RS232 in pin, you have to set your serout baudmodes to 16468 -- inverted mode.(this is all in the pbasic helpfile).

To be honest, you should be able to get away without using a MAX232 as you can set the BS2 to use the inverted mode and connect it straight to your ELM. NOTE though, that you need a series current limiting resistor on your serin line to avoid damaging that pin (this too is in the help file).

Hope that straightens some things out.

cheers

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·

Steve

"Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."

Robban
02-08-2007, 02:39 AM
i have tried this with 16468 but with no luck....is my program correct?
can i use that 16468 in any pin on the stamp?

tried also to hook up a display to my pin 0 and replaced the debug line with "serout 0,84,[hex rpm]" but also this was of no luck

Unsoundcode
02-08-2007, 03:38 AM
Hi Robban, scanning the datasheet real quick I see that the elm327 expects a carriage return at the end of sent data, it may not be the cause of your communication problem but you might try it and see if the response differs. Even without the <CR> it should return "?" after a 20 sec timeout

SEROUT 15,84,["0100",13]

Jeff T.

Chris Savage
02-08-2007, 05:04 AM
steve_b said...(trimmed)
The "84" in your serin/serout lines refers to the serial data going out in TRUE format. Meaning a logic 1 is a +5V level....basically TTL levels.
RS232 doesn't work that way. A Logic 1 in RS232 is actually a negative voltage (~-5/-8/-12volts depending). and alogic 0 would be the positive of those values.
The stamps don't produce a negative voltage, but instead just drop the level to 0volts at the output which is usually good enough for most devices.
So, if you are connecting your BS2 to the MAX232 RS232 in pin, you have to set your serout baudmodes to 16468 -- inverted mode.(this is all in the pbasic helpfile).
To be honest, you should be able to get away without using a MAX232 as you can set the BS2 to use the inverted mode and connect it straight to your ELM. NOTE though, that you need a series current limiting resistor on your serin line to avoid damaging that pin (this too is in the help file).
Steve,

·· Actually if you're using a MAX232 you'd want to use TRUE baud mode since the driver itself inverts the signal for you (it is basically an inverter).· So the 84 would be correct in this case.· Also, as for the direct connection...For some devices this does in fact work and we mention it in our manual.· It usually works when connecting to a PC.· Some devices/peripherals on the other hand expect RS-232 signals and do not work well without them.· In these cases you pretty much need a line driver.· I hope this helps.· Take care.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support

Robban
02-08-2007, 05:17 AM
Chris!

from what i have been wiriting...what are your conclutions? what do i do wrong?
i cant get the display to show anything....therefore i am not sure if the stamp gets the input from the elm327

Chris Savage
02-08-2007, 05:34 AM
That is hard to say without more information. As Steve said, it sounds like you have the wrong chip unless that was a typo above. Assuming you do have the right chip do you have the 4 required .1uF capacitors connected?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support

Robban
02-08-2007, 05:41 AM
i wrote the wrong max......i have the max232...the fingers slipped http://forums.parallax.com/images/smilies/smile.gif

no,i do not have the capasitors connected...
i have the pins from the max directly connected to the pins on the stamp.

Chris Savage
02-08-2007, 06:13 AM
Well the MAX232 requires the capacitors for the charge pumps. Without them it won’t work. Please see the datasheet for more information.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support

Robban
02-08-2007, 03:22 PM
the strange thing is that when i hook up the max and stamp against my pc and connect the cable to the max and write a program that get the input (serin) from my pc and then serout the to lcd i can write on the keyboard and directly i see it on the display...

like

serout 0,84,["Hello world"]·· 'send hello world to the terminalprogram (pbasics)
start:
serin 1,84,[keys]·············· ' get the inputs from the keyboard
serout 15,84,[keys]·········· ' send the inputs to the display
goto start


but when i replace "hello world" with 0100 and connect it to the elm the display is blank...

isn´t the caapasitors included on the Rs-232 link (see link in 1:st message)
·

Bruce Bates
02-08-2007, 03:44 PM
Robban -

What is the proper BAUD RATE for the "ELM Unit", according to their specifications?

Is more than one BAUD RATE available, and if so, do you have the "ELM Unit" strapped or jumpered to provide the BAUD RATE you are expecting?

Regards,

Bruce Bates

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->

Robban
02-08-2007, 04:42 PM
Read the sheet and from what i understand i should use the 9600 baudrate...but according to the sheet it can be set to 38400 also.

When i hook upp the elm againt a terminal program i use 9600 and this works very good.

Bruce Bates
02-08-2007, 05:04 PM
Robban -

Could you qualify "terminal program" a bit as there are many, many of them out there?

Additionally, which ELM Unit are you using?

Bruce

Post Edited (Bruce Bates) : 2/8/2007 9:08:59 AM GMT

Robban
02-08-2007, 06:11 PM
What do you mean with qulaify?

i am using the elm327

Robban
02-08-2007, 08:50 PM
i am using hyperterminal or the Pbasic 2.5 debug window

allanlane5
02-08-2007, 09:53 PM
1. The Max232 needs 4 capacitors to do its "Level Shifting" work (from TTL to RS-232 +- 10 volts). If you haven't put them in, then the MAX232 isn't going to work.

2. A "terminal Program" is something like Hyperterm, SmartTerm, PuTTY, etc. Which one are you using?

Robban
02-08-2007, 10:53 PM
i am using hyperterm or the pbasic:s terminalwindow...
but if you look at link 2 in the first message, shouldn´t those be enough or do i have to put addational 4?

allanlane5
02-08-2007, 11:12 PM
Ok, good, that board should work fine. And it DOES have 4 capacitors already installed, so that's OK.

Now, the other big difference between the PC and the BOE is that for a PC to talk to a BOE it uses pin 2 to 2, 3 to 3, pin 5 as ground. What they call a "straight through" cable.

BUT, when you connect the BOE directly to the ELM327 you need to connect pin 2 to 3, pin 3 to 2, and pin 5 straight through -- what they call a "null modem" cable.

It sounds like that is your problem.

Robban
02-09-2007, 02:01 AM
allan, i used my nullmodem cable between the elm and my stamp and wrote this

' {$STAMP BS2}
key VAR Byte
'SEROUT 16,84,["0100",13]
SERIN 16,84,[key]
SEROUT 2,84,[HEX4 key] ---display the incoming hex on my display

but still i cant make it work...
i should be recieving something like
41 00 BE 1F B8 10

but all i get was a bpunch of zeros and one or two ff och 3f..

Unsoundcode
02-09-2007, 02:38 AM
It seems you are getting a response now at least, maybe you could get a little more information if you adjusted your formatting a little. You could try and gather a little more from the elm by accepting a string of bytes,

key var byte(8)
SERIN 16,84,[STR key\8\10]
DEBUG STR key

and maybe adjusting the out string to see what effect that has

SEROUT 16,84,["01",13,"00"]

Jeff T.

allanlane5
02-09-2007, 02:38 AM
For your 'baudmode' (that 84 up there) try 16468. That's the "inverted" baud mode -- the logic of when to 'invert' or not is very unclear, I usually just try stuff until it works.

Note the SERIN "HEX" modifier takes IN a string that SHOULD be a 'hex' number, and converts it to a SINGLE 'word' value (32 bits). It looks like your SERIN recieves a single byte, then tries to print it as a 4-byte hex string.

To debug what's going on, you probably want something like:

KeyStr VAR BYTE(6)

SERIN 16, 84, [STR KeyStr\4] ' Get 4 bytes
SEROUT 2, 84, [STR KeyStr\4] ' Send 4 bytes.

Note on "port 16", everything recieved is echo'ed back out to the sending device -- this might confuse the Elm327. I assumed you were using your MAX232 to talk to the 'Elm'.

Anyway, send and recieve as STR, then you can put in the hex modifiers as so:

KeyVal VAR WORD
SERIN 16, 84, [HEX KeyVal] ' Recieve hexadecimal string, and put into KeyVal word
SEROUT 2, 84, [HEX KeyVal, 13] ' Convert KeyVal back into hexadecimal string

Robban
02-09-2007, 02:44 AM
tried the replace the 84 with 16468 but no change....no i did not use my max232...i connected the stamp directly with a nullmodem cable to the elm...

cause when i connect it with my max232 nothing happend with the leds on the elminterface...

but when i connect it to the dsub on the BOE and start the program there are 2 leds that start light up a while....

allanlane5
02-09-2007, 02:52 AM
Gosh, where to begin...

The first thing you must do is "debug your interfaces". Currently, you don't know (because nothing is working) if your wiring to the Elm is correct, or your baudmode setting to the Elm is correct. The "STR" versions of code above are to help you find out what is wrong and correct it.

Second -- you MUST NOT use the built-in DB-9 of the BOE to talk with the Elm. That connector assumes there's a "real" RS-232 device (like your PC) on the other end. And everything that port recieves is echoed, by the hardware, back to that device. For SURE this is going to confuse the Elm.

So, were I you, I would use the PC as a debug terminal, hang the Elm off the other port (port 2) with or without the MAX232 does not matter. If your PC talks directly to the Elm, then probably you MUST use the Max232 to get it to work, but you can find that out by trying it.

The key to successful software is to validate the low level interfaces (perhaps with a VERY simple program). Then build on stuff you KNOW works to get the next level of functionality.

Otherwise, there's WAY too many things that can be going wrong to be able to make any progress.

Oh, and only the pseudo "port 16" can be used for both input and output.· To talk to the Elm, you'll need a TX pin, and an RX pin.· Currently pin 2 looks like an 'output' pin.

Also, the Elm probably wants a CR character to know when a transmission is finished.· The PC gives it that automatically.· So, for the BS2:

SEROUT 14, 84, ["0100", 13]· ' Assuming the Elm is recieving from the BS2's pin 14.

And note you MUST run Vss to the Ground pin of the DB-9 connector (Ground) to the Elm for the Elm to properly get the data.


Post Edited (allanlane5) : 2/8/2007 6:59:16 PM GMT

Robban
02-09-2007, 02:59 AM
i have connected my max232 to the pin0 and pin 1 and tried to write a very small program (like above) so now i come to another question...is the pins0-15 indepenetly in and out?

i have hooked up the elm directly to my elm via the com1 and it works just as i should with hyperterminal....
when i wrote 0100 then i got the hexa numbers i menshend earlier..

Mike Green
02-09-2007, 03:04 AM
The direction (input vs. output) is set by the DIR register (look in the PBasic Manual for a discussion) which is a 16 bit register. A one in a bit position sets the corresponding pin to input mode. A zero in that bit position sets the pin to output mode. Many of the statements that include a pin number will implicitly set the I/O direction and the INPUT and OUTPUT statements will set the direction for the single pin specified. If you display the value in DIR using DEBUG and execute INPUT and OUTPUT statements, you will see how DIR changes.

allanlane5
02-09-2007, 03:05 AM
I don't know what "i have hooked up THE elm directly to MY elm via the com1" means. I assume it means you've connected the Elm directly to your PC's COM1 serial port. And apparently that works. This is good, this means that the Elm RX/TX and ground are working properly. You've validated the Elm chip.

Yes, pins 0..15 are independently in and out. Meaning, if you tie pin0 to the Elm RX, and pin 1 to the Elm TX, and Vss to the Elm Ground, then you should be able to send:

HexStr VAR BYTE(5)
I VAR BYTE

MAIN:
SEROUT 0, 84 ["0100",13]
SERIN 1, 84, [STR HexStr\4]
FOR I = 0 to 3
SEROUT 16, 16864, [HEX HexStr(I)] ' Assumes PC is connected
NEXT
SEROUT 16, 16864, [13]

PAUSE 1000 ' Wait 1 second so we don't get 'flooded'
GOTO MAIN

And the above should work.

Robban
03-03-2007, 01:42 AM
hi Allan!

Tried to hook up the BS2 and the Elm as you wrote o wrote the program.
But nothing happends..

I made a 9 pin dsub with pin 2 on the dsub to the pin 0 on the BS2
and pin 3 to the pin 0 on the bs2
and pin 5 to vss

can´t understand what is wrong..

I also tried to use my max232 on the same pins but still nothing.


One more thing.
When i connect the elm directley to the laptop and use a terminalprogram (Hyperterminal / pbasic:s terminal window and write 0100 the leds on the elm starts to light up for every key i press...
like

0 - flash
1 - flash
0 - flash
0 - flash

and i get the answer i should....

any clues/ideas?




·

allanlane5
03-03-2007, 02:42 AM
If you've tied DB-9 pin 2 to pin 0 on the BS2, AND tied DB-9 pin 3 to pin 0 on the BS2, then you've shorted TX to RX, and I'm not surprised it doesn't work.

Tie DB-9 pin 2 to P0 on the BS2, and DB-9 pin 3 to P1 on the BS2. On the good side, that DB-9 pin 5 to Vss is correct.

Robban
03-03-2007, 02:45 AM
oh...sorry if i got it wrong when i wrote....

i did tied it as you said....

allanlane5
03-03-2007, 02:52 AM
If it works in Hyperterminal then you probably need a properly installed Max232 chip.

You've got the stuff in front of you. If you can't figure it out, there's limits on what I can do to help you. I think I've reached those limits.

Robban
03-03-2007, 11:56 PM
allright....now i got the hexa decimal as i should on the display...it was the "84" that should be replaced by 16468 instead.
so hardware communication is working...http://forums.parallax.com/images/smilies/smile.gif

nw to another gueastion
my answer from the elm is 41 0C 1A F8
and from what i have inderstood it is only the 1AF8 that is the data...

Question is...is there a command in Pbasic to "mask" out the 1AF8 or do i have to make a small program for this?
if so....anyone has any examples?

regards
Robban

PJ Allen
03-04-2007, 02:44 AM
Robban said...
and from what i have inderstood it is only the 1AF8 that is the data...

Question is...is there a command in Pbasic to "mask" out the 1AF8 or do i have to make a small program for this?

Use one (or more)·of the data formatting options (see PBASIC Help for SERIN -- WAIT, STR, SKIP, etc.)

Robban
03-04-2007, 06:17 PM
i tried the skip command,but then i only got one charter...and since the line is diffrent for every "sensor" i cant´t use the wait command...

*confused*

Unsoundcode
03-05-2007, 06:12 AM
Hi Robban, with each command you send the elm unit you should·know how many bytes it will return and which bytes you require. In the example you have been dealing with you are saying that the elm returns four bytes and you only require the last two bytes. What you could do is assign a variable for the SKIP and HexStr dependent on what you were expecting to recieve. Assuming you are using allan's code

SEROUT tx,baud, ["0100",13]

skip_len=2

return_len=4


SERIN rx,baud, [SKIP skip_len,STR HexStr\return_len]

the skip and return variables would be dependent on the issued command

Jeff T.

Robban
03-08-2007, 12:44 AM
ok...i got that, can i then convert the 1a f8 to the actual value (6904)?

allanlane5
03-08-2007, 04:27 AM
"SERIN rx,baud, [SKIP skip_len,STR HexStr\return_len]"

Yes, but you'd use:

SERIN rx, baud, [SKIP skip_len, HEX HexVal.HighByte, HEX HexVal.LowByte]

HexVal (defined as a WORD) would then have the value 6904 in it, if given the two strings "1A" and "F8".
Or "1a" and "f8", same thing. And if you're begin given the string "1AF8", then you'd use:

SERIN rx, baud, [SKIP skip_len, HEX HexVal]

The "HEX" is a 'deformatting' keyword, that converts a hexadecimal formatted string into the equivalent decimal value, then puts the resulting value into a variable.