BS2 to industrial controller using rs232
jelecguy31
Posts: 17
Hello, I am trying to create a simple communications tester using a BS2. I have a piece of equipment that i need to check if the communications card works. I can check it with a laptop and Docklight but i do not want to carry the laptop around. I need a small portable box that i will push one button (or the reset button) and it will send the command code using serout over RS232 and receive a command packet back. My code is:
' {$STAMP BS2}
' {$PBASIC 2.5}
respacket VAR Byte(18)
PAUSE 1000 'do not confuse with a mouse or plug n play device
DEBUG "MPC comm test - JZ", CR
SEROUT 0,16468,["~ 05 01 00",CR]
SERIN 1,16468,2000,err,[STR respacket\13]
DEBUG STR respacket, CR
END
err:
DEBUG "comm error, no response", CR
When i use Docklight or hyperterminal i get back the correct response of "05 OK 00 DIGITEL MPC" but when i use the BS2 i get garbage characters on the debug screen. I have searched and read a lot of the forums and articles on google and think this is a unique problem. Any suggestions are appreciated.
' {$STAMP BS2}
' {$PBASIC 2.5}
respacket VAR Byte(18)
PAUSE 1000 'do not confuse with a mouse or plug n play device
DEBUG "MPC comm test - JZ", CR
SEROUT 0,16468,["~ 05 01 00",CR]
SERIN 1,16468,2000,err,[STR respacket\13]
DEBUG STR respacket, CR
END
err:
DEBUG "comm error, no response", CR
When i use Docklight or hyperterminal i get back the correct response of "05 OK 00 DIGITEL MPC" but when i use the BS2 i get garbage characters on the debug screen. I have searched and read a lot of the forums and articles on google and think this is a unique problem. Any suggestions are appreciated.
Comments
....Also, how long is your RS232 connection from your stamp to your laptop/computer?
I have connected the BS2 to another laptop running docklight and i see the "~ 05 01 00" on the laptop and when i send "05 OK 00" to the BS from dock light it shows up on the debug screen.
thanks.
' {$STAMP BS2}
' {$PBASIC 2.5}
respacket VAR Byte(18)
PAUSE 1000 'do not confuse with a mouse or plug n play device
DEBUG "MPC comm test - JZ", CR
SEROUT 0,16468,["~ 05 01 00",CR]
PAUSE 4
SERIN 1,16468,1000,err,[STR respacket\13]
DEBUG STR respacket, CR
END
err:
DEBUG "comm error, no response", CR
If you can get the sending device to pause before sending a response, you may get a bit better results.
I learned to use a BS2sx when anything over 4800 baud was required.
Cheers,
Its been my experiencethat most rs232 devices correctly intepret the stamp levels of 0 volts and 5 volts.
If you use port 16 , the programming port (instead of port 0 in your example) , the level shifting is done on-board already.
Going back to earlier posts, I think this a mostly a speed issue.
Cheers,
the declaration needs expanding too if you need the full response string
Serout 16,..........
serin ?,..................
Jeff T.
EDIT: the DEBUG command will transmit through P16 so it might be wise to remove any debug statements before hooking up to your device.
Serout 0\1,16468,[ data]
serin 1\0,16468,1000,err,[data]
OR
would i connect the Digitel pin 8 (CTS) to a pin, say 3, on the BS2 then use a code like this:
Serout 0,16468,[ data] 'send command
low 3 'CTS
serin 1,16468,1000,err,[data] 'receive command
OR
connect BS2 pin 0 to send and BS2 DB9 pin 2 for serin and of course a ground to vss:
Serout 0,16468,[ data] 'send command
'possibly put a pause 4 here
serin 16,16468,1000,err,[data] 'receive command
Thanks.
The BS2 knows to send data from the DB9 plug pin 3 and receive on pin 2??!?!?!?!?!!? thus having real RS-232?!?!?!?
The only caveat with the Stamp's implementation of rs232, the device the Stamp is talking to must be a "real" rs232 device with the required negative voltage generation circuitry. So, most PC's and more recent equipment with MAX232 (or equivalent) chips built in will work fine.
Cheers,
Pins 1 and 2 are passed through a line driver so the resulting serial signals are an inversion of the input. This is why there are two different values for the serial command, one is true or non inverted and the other is inverted. For the BS2 and a baud rate of 9600 the true or non inverted value is 84 and the inverted value is 16468. The voltage levels of the line driver output is sufficient to drive the RS232 of a PC.
There is no option for flow control when using P16 but there is an option if using P0 through P15. The PBasic Syntax and Reference manual or the IDE help file descibe the serial instructions in detail.
I dont think you need flow control, as mentioned previously 9600 is borderline for the standard BS2 but it might work.
Jeff T.
EDIT: Hey Tom how are you
Doing great. Just PM`d you.
Tom
I did not know the BS2 could do that. I will be working on this this weekend. I hope to have it going next week. Thanks again for all the help.
I will post some pictures and code when i get it working.
Serout 16,16468,["~ 05 01 00",CR]
Pause 4
Serin 16,16468,1000,err,[str respacket\25\CR]
It worked just the same as using pin 1 and 0. If i take the pause out i get garbage. If i leave it in i get the partial message as mentioned before.
THEN, I used a max232 chip. Same result.
So i think i will now try to use the BS2sx or try to mess with the CTS signal.
But, partial data reads are pretty common when the Stamp can't keep up with the incoming data. Going to the BS2sx or px will give much more speed capability plus a lot more space to read in the characters.
Cheers,
' {$STAMP BS2}
' {$PBASIC 2.5}
'Created by JZ
'created October 15 2013
'modified october 29 - added debug line to check data returned from device on Debug terminal
'This comm test will use the db9 port on the super carrier board to talk to the MPC or
'a digitel 500 / 1500. Follow the on screen menu to choose a controller to test.
'
setup
respacket VAR Byte(26)
LCDpin PIN 15
T9600 CON 84
LCDon CON 22
LCDcl CON 12
LCDled CON 17
PB1 VAR IN10
PB2 VAR IN11
'
MAIN
PAUSE 1000 'do not confuse with a mouse or plug n play device
SEROUT LCDpin,T9600,[LCDon,LCDcl,LCDled] 'initalize LCD
PAUSE 5
Start:
SEROUT LCDpin,T9600,[128,"JzCommTst Choose", 148, "1-MPC 2-500/1500"]
PAUSE 5
IF PB1 = 1 THEN
GOSUB MPCtest
ENDIF
IF PB2 = 1 THEN
GOSUB Digiteltest
ENDIF
GOTO Start
'Digitel MPC
MPCtest:
SEROUT LCDpin,T9600,[LCDcl,128,"MPC comm test-JZ", 148, "Sending"] 'LCD display
PAUSE 5
SEROUT 0,84,["~ 05 01 00",CR] 'Send command to MPC,9600, 8 bit, 1 stop, no parity
PAUSE 4
SERIN 1,84,1000,err,[STR respacket\25\CR] 'Receive info from MPC
SEROUT LCDpin,T9600,[LCDcl,128,"MPC comm test-JZ",148, STR respacket\16] 'LCD display
'DEBUG STR respacket\16
PAUSE 9000
RETURN
'digitel 500 / 1500
Digiteltest:
SEROUT LCDpin,T9600,[LCDcl,128,"DIGITEL com test", 148, "Sending"] 'LCD display
PAUSE 5
SEROUT 0,8276,["RD",CR] 'Send command to Digitel 500,9600,7 bit, 1 stop, even parity
'PAUSE 4
SERIN 1,8276,1000,err,[STR respacket\25\CR] 'Receive info from Digitel
SEROUT LCDpin,T9600,[LCDcl,128,"DIGITEL com test",148, STR respacket\16] 'LCD display
'DEBUG STR respacket\16
PAUSE 9000
RETURN
err:
SEROUT LCDpin,T9600,[LCDcl,128,"Comm test-JZ",148,"Error, no reply"] 'LCD display error
PAUSE 9000
GOTO Start
The problem i have now is that with the Texas Instruments MAX232 chip installed with capacitors the display shows garbage or nothing? What could be causing the signal to the screen to do that? I will try a BS2p next.
Thanks
Otherwise, time for schematics!
Thanks
Jeff T.
I have old and new pump controllers. My BS2 program allows me to select either the model 500 or the model MPC. All our equipment is set to 9600 baud. The max chip i bought is a TI Max 232 that uses 1uF capacitor.
Also, are you supplying both a positive and negative supply voltage to this chip?
Are you guys talking FoE?
Tim