Shop OBEX P1 Docs P2 Docs Learn Events
BS2 to industrial controller using rs232 — Parallax Forums

BS2 to industrial controller using rs232

jelecguy31jelecguy31 Posts: 17
edited 2014-01-31 13:54 in BASIC Stamp
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.
«1

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-10-02 12:30
    Can you post a schematic? The garbage characters could be just an inversion problem with the RS232 signal. Have you tried inverting the RS232 from the Basic Stamps perspective?

    ....Also, how long is your RS232 connection from your stamp to your laptop/computer?
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-03 07:17
    hello. attached is a crude schematic. I have tried to invert the signal by using 84 instead of 16468. I get the same garbage data just on vertical lines instead of horizontal lines.

    mpc schematic.JPG


    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.
    855 x 557 - 24K
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-10-03 07:27
    "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." ... While the second Laptop can tolerate it, the DIGITEL may not like the low voltage RS232 generated from the BS2. A MAX232 might be necessary in your case to communicate properly.
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-03 07:27
    I just added a pause between the serout and serin and am getting part of the response packet. I am getting "K 00 Digitel" So i am missing the "05 O". but if i change the pause to 3 i get garbage again.


    ' {$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
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-10-03 07:36
    "I just added a pause between the serout and serin and am getting part of the response packet. I am getting "K 00 Digitel" So i am missing the "05 O". but if i change the pause to 3 i get garbage again." - Since the BS2 does not have any buffering and is a single threaded device, it sounds as though the DIGITEL is responding faster than the BS2 can switch between sending serial versus receiving serial and the BS2 is not able to sync, creating garbage, but the pause somehow is allowing it to synchronize to at least part of the incoming data.
  • stamptrolstamptrol Posts: 1,731
    edited 2013-10-03 08:12
    The BS2 will struggle to receive at 9600 baud for the reasons Beau noted.

    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,
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-03 10:37
    will the max232 chip help with the synchronization? or do i need to connect the RTs or CTS pins to the BS2?
  • stamptrolstamptrol Posts: 1,731
    edited 2013-10-03 11:47
    The max232 just shifts the ttl levels of a stamp pin to the +/- levels used by rs-232.

    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,
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-10-03 15:14
    Hi, the response string is 20 characters in length and you have only made room for 13
    SERIN 1,16468,1000,err,[STR respacket\13]

    the declaration needs expanding too if you need the full response string
    respacket VAR Byte(18)
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-10-03 15:19
    I had another thought, its quite likely the string is terminated with a carriage return (decimal 13) which is something you can use in the receive line of code
    SERIN 1,16468,1000,err,[STR respacket\20\13]
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-04 07:35
    Thanks for the guidance. I will try both suggestions. Yes you are correct that the line is terminated by a carriage return. When i use the BS super carrier board DB9 plug to connect to the digitel what is the serial pins to use?

    Serout 16,..........
    serin ?,..................
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-10-04 10:00
    It's SEROUT 16 and SERIN 16
    eg: SERIN 16,16468,1000,err,[STR respacket\20\13]

    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.
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-04 10:17
    I am not in my lab today so i am thinking and googleing. What about flow control???? say i used this code:

    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
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-04 10:20
    I am going to remove the debug code and have what data i receive sent to an LCD. eventually i will compare the data i get back and if it matches then a green light will turn on, otherwise a red light.
    Thanks.
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-04 10:23
    "It's SEROUT 16 and SERIN 16"

    The BS2 knows to send data from the DB9 plug pin 3 and receive on pin 2??!?!?!?!?!!? thus having real RS-232?!?!?!?
  • stamptrolstamptrol Posts: 1,731
    edited 2013-10-04 10:55
    Yes!

    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,
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-10-04 11:14
    Hi, P16 is not pin 16 it is port 16 which derives its tx/rx lines from Stamp pin 1 and pin 2 respectively.

    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
  • stamptrolstamptrol Posts: 1,731
    edited 2013-10-04 12:36
    Hey Jeff,

    Doing great. Just PM`d you.

    Tom
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-04 12:43
    MIND BLOWN!!!!!!

    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.
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-14 08:13
    I have been a busy little electro-maniac. I tried using the on board serial port with the code of:

    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.
  • stamptrolstamptrol Posts: 1,731
    edited 2013-10-15 06:15
    Are you still using the BS2? Expecting 25 characters may be overwriting some of the variable area, depending on what the rest of the program looks like..

    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,
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-29 12:40
    I know it has been a while but as promised here is the code i am using:

    ' {$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
  • stamptrolstamptrol Posts: 1,731
    edited 2013-10-30 05:49
    With the MAX232 in the circuit, the ttl input to the Max should use "inverted" mode, I believe. Try changing the LCD T9600 value from 84 to the corresponding inverted mode.

    Otherwise, time for schematics!
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-30 06:47
    If i take the capacitors off everything works fine. I will try to draw up some schematics.
    Thanks
  • GenetixGenetix Posts: 1,754
    edited 2013-10-30 15:09
    There are several different versions of the MAX232 chips that use different capacitor sizes. You might not be using the correct size. Also, those Digitel devices appear to be pump controllers and there are older and newer versions but you don't state exactly what you have. The manual for the older MPC shows that the baud rate can be changed using the DIP switches on the main circuit board inside the unit. The type of serial of serial output is also configured this way. You said that this equipment is used for a process so it should be as reliable as possible and a lower baud rate would ensure that.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-10-30 18:56
    A little higher in cost but does not require capacitors is the MAX233.

    Jeff T.
  • jelecguy31jelecguy31 Posts: 17
    edited 2013-10-31 09:00
    I will look into the max 233.

    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.
  • GenetixGenetix Posts: 1,754
    edited 2013-10-31 15:48
    According to the datasheet there are 5 1 uF capacitors total including a bypass capacitor (Vcc to Ground). Might you have had a missing or backwards capacitor?
    Also, are you supplying both a positive and negative supply voltage to this chip?
  • jelecguy31jelecguy31 Posts: 17
    edited 2014-01-15 10:29
    I am only supplying positive voltage by a wall wart. I need to keep this portable and use one power outlet. I ordered the Max 233 chip and will try that.
  • trookstrooks Posts: 228
    edited 2014-01-21 15:11
    Off topic to Tom and Jeff,

    Are you guys talking FoE?

    Tim
Sign In or Register to comment.