Shop OBEX P1 Docs P2 Docs Learn Events
Monitor rs232 port > alter data > output to serial LCD solution — Parallax Forums

Monitor rs232 port > alter data > output to serial LCD solution

syxysyxy Posts: 25
edited 2006-04-10 14:36 in BASIC Stamp
Hi guys,

I've been working on a solution whereby the BS2 stamp receives data from an equipment RS232 port, makes some adjustments to the received string and output it to a serial 2x20 lcd display.

The equipment sends 20 characters at 9600 baud, 1 start bit, 1 stop bit and no parity. The LCD display works with the same specs. When I hookup the equipment port directly to the LCD it correctly displays the strings but I need the BS2 to change some of the data.

I currently have a problem whereby the LCD shows garbarge, apparently there's·a problem either with the timing or string conversion.

The code which I am using now (leaving out the conversion bit):

' {$STAMP BS2}
' {$PBASIC 2.5}

string··········· VAR···· Byte(20)····························· 'temp string of 20 characters

PAUSE 1000······················································ 'allow the display to initialize and startup
SEROUT 11,84+$4000,[noparse][[/noparse]254,1]······························· 'clear screen
PAUSE 200
SEROUT 11,84+$4000,[noparse][[/noparse]254,132]····························· 'Move one letter right
SEROUT 11,84+$4000,[noparse][[/noparse]"Hello"]·························· 'write text to pin 11 at 9600 baud
SEROUT 11,84+$4000,[noparse][[/noparse]254,192]····························· 'Move to second line, position 1
SEROUT 11,84+$4000,[noparse][[/noparse]254,196]····························· 'Move to second line, position 4
PAUSE 500
SEROUT 11,84+$4000,[noparse][[/noparse]"World"]·························· 'write text to pin 11 at 9600 baud
PAUSE 500

'
THE ABOVE PART OF THE CODE WORKS FINE

DO
····· SERIN 10,84+$4000,[noparse][[/noparse]string]
····· SEROUT 11,84+$4000,[noparse][[/noparse]string]
LOOP

END



Can you guys provide me with some help?

Cheers
«1

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2006-04-04 11:57
    syxy,

    I've used this process to convert data formats from one manufacturer's to another, so I'm pretty sure you will be able to do it.

    With the SERIN command structure you have at the moment, the SERIN command has to be executing at the instant the data flows. If your data string has some sort of start character, use the WAIT modifier with the serial command. For instance, the scale indicators I work with start the data with an ASCII 002. The serin command then will wait for the the ASCII 002 before grabbing the data. As well, you can put modifiers in the SERIN command to grab a certain number of characters out of a longer string.

    If no start character is used in your data string, you can always trigger the SERIN by WAITing for the CR,LF which is likely what ends each data string.

    See the help files under SERIN for all the ways to modify the command.

    Secondly, make sure there is enough time for the stamp to do what you want it to with the data. At 9600, there may not be time enough to grab the 20 bytes, load the array then spit it back out to the LCD. You could either go with a slower baud rate ( I usually use the BS2 up to 4800 and use the BS2sx at 9600) or put some delay between the two functions.

    Cheers
  • syxysyxy Posts: 25
    edited 2006-04-04 12:24
    Hi Stamptrol,

    Thanks very much for your advice. I'll give it a try although there's no start character. I think it does give out a CR at the end of each line.

    As for the baud rates, the source gives out at 9600bps - I have no option to change this. As for the display, I can set it for 2400 baud or 9600 baud.

    Can you give me a line with sample code which includes a wait instruction? Isn't it true that the variable declaration limits the capture to 20 bytes?

    Cheers,

    Richard
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-04 14:28
    Richard,

    ·· You do not mention which LCD you're using, but I would venture to guess a Matrix Orbital or Seetron based on the command byte.· Also you're trying to input the "string" into a single byte value when you need to specify an the array you created in your delcarations.· Finally, if I/O pin 10 is not setup with an RS-232 driver, you may not be getting what you think.· Is there anything like a driver or resistor between the Stamp I/O pin and the RS-2323 signal?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • syxysyxy Posts: 25
    edited 2006-04-04 14:40
    Hi Chris,

    The display is interfaced with a serial backpack board from milford instruments UK.

    There is a resistor inbetween pin 10 and the serial board.

    Also, I'm having trouble understanding this: "Also you're trying to input the "string" into a single byte value when you need to specify an the array you created in your delcarations."

    Can you perhaps tell me what adjustments to my code are required to make it work?

    I just tried to have it wait for a CR or LF at the end of a string but the stamp:

    SERIN 10,84+$4000,[noparse][[/noparse]WAIT(CR),string]

    ..but it's waiting forever so I guess the equipment doesn't send out a CR at the end.

    I also tried interfacing the stamp and serial board at 2400bps but it still gives garbage. Note that writing text as the first part of the code shows is working fine.

    Cheers,

    Richard
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-04 14:57
    Actually, with that command it will wait for a CR and then input a character into the variable "string".· So if CR terminates the data then you are not getting it at that point.· To input data into an array you would use SERIN like below...

    SERIN 10, Baud, [noparse][[/noparse]STR string\9]

    Now, the 22K resistor should work, but you need use inverted baud mode, which it looks like you're doing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • syxysyxy Posts: 25
    edited 2006-04-04 15:07
    Hi Chris,

    Tried this, didn't work:

    DO
    string(20)=0
    SERIN 10,84+$4000,[noparse][[/noparse]STR string\20]
    PAUSE 200
    SEROUT 11,396+$4000,[noparse][[/noparse]string]
    LOOP
    END

    I also tried this:

    DO
    string(20)=0
    SERIN 10,16468+$4000,[noparse][[/noparse]STR string\20] 'inverted baud mode
    PAUSE 200
    SEROUT 11,396+$4000,[noparse][[/noparse]STR string\20]
    LOOP
    END

    now the display shows garbage but I can see bits of the supposed string inbetween the garbage. Things are getting there but not quite yet...
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-04 15:32
    What size resistor are you using on the serial data coming in?· Do you have a common ground between the sending device and the BASIC Stamp?·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • syxysyxy Posts: 25
    edited 2006-04-04 15:57
    Chris,

    I have a common ground and the resistor is a 22 Kohms, +/-5%.

    Cheers
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-04 16:23
    How long are the wires?· Long lead lengths could be picking up noise.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • stamptrolstamptrol Posts: 1,731
    edited 2006-04-04 16:47
    Richard,
    The idea of waiting for the CR is to be ready for the next full string when there is no start of transmission.

    For example:

    SERIN 12,baud,14000,tout,[noparse][[/noparse]WAIT (CR), SKIP 9, STR grab1\15, SKIP 13, STR grab2\10]

    was used on a water sampling package that sent the string every half second or so. I watched for the CR at the end of one string and the SERIN would wait until data started flowing at the beginning of the next transmission. I let the first 9 bytes go by ( because it was just housekeeping bytes), then grabbed 15 bytes and stored them in grab1. Skip another 13 bytes of non-essential stuff, then grab 10 more bytes and put them in grab2.

    Hope this helps.

    Tom
  • syxysyxy Posts: 25
    edited 2006-04-04 18:30
    The wires are 1.5 metres long to the stamp module and 2 inches from the stamp to the LCD.

    Cheers,

    Richard
  • syxysyxy Posts: 25
    edited 2006-04-05 17:23
    Any other thoughts about possible causes? I'm stuck!

    Cheers

    Richard
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-05 17:58
    Richard,

    ·· Are these shielded wires?· With 1.5 m and just using a 22K resistor I think noise would be a big concern here.· If you're going to run that much you should use shielded wire with an RS-232 driver such as the MAX232.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Tom WalkerTom Walker Posts: 509
    edited 2006-04-05 18:20
    ...or a slight investment in RS-485...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
  • syxysyxy Posts: 25
    edited 2006-04-05 18:31
    Chris Savage (Parallax) said...

    Richard,

    ·· Are these shielded wires?· With 1.5 m and just using a 22K resistor I think noise would be a big concern here.· If you're going to run that much you should use shielded wire with an RS-232 driver such as the MAX232.

    Would things change if I use the regular pins for serial comms?

    I've used wires that are 20 metres with the equipment, loading firmware and software over comms. Never had any problems with this.
    As for the MAX232, I'll look into that.
    Could it be that the BS2 is just not fast enough?
    Should I work with other resistor values?

    Cheers,

    Richard
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-04-05 19:22
    Just my two bits worth.... If you can, lower the baud rates down... If it clears up, you know it's either noise or a speed (lack of speed maybe) issue...
    Ok, maybe thats a few bytes of thoughtfullnes, but when ever I got got trash and not what I expected, I would slow things down real slow, so I could trouble shoot what was actually happening.

    [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-05 19:41
    Richard,

    ·· What do you mean use the regular pins?· I thought that was what you were using since you specified P10 and P11 in your example code.··Are you using something else, for example the programming port (16)?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • syxysyxy Posts: 25
    edited 2006-04-05 20:53
    I am using pin 10 and 11 with the resistors added in series. With the "regular pins" I meant the pins 1 & 2.

    As for trying slower baud rates, I cannot slow down the input, I did slow down the output to 2400.

    I'll try a shorter input cable although currently it's a high quality moulded serial cable so I wouldn't expect a difference.


    Chris, should this code normaly work with a BS2 with 9600 in and out? Or is this near the max of the specs of the BS2?

    Cheers

    Richard


    EDIT: the plain text output "hello world" from the basic stamp works fine. Shouldn't this confirm that it's not a cable issue? When I monitor the port from my equipment using hyperterminal all the strings received work fine. When I hookup the display directly to the equipment, the strings show up fine. To me this means that it's got to do with either bad code, slow stamp or wrong resistor values.

    Post Edited (syxy) : 4/5/2006 8:58:02 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-05 21:00
    You're using a resistor on the SERIAL OUTPUT line to your LCD?· Why?· Is it an RS-232 level LCD?· What value are you using?· Pins 1 & 2 or pins 10 & 11 doesn't matter for serial.· They're all normal I/O pins and can be SERIAL I/O.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • syxysyxy Posts: 25
    edited 2006-04-05 21:05
    I'm not using a resistor on the output, just on the inputs. Sorry for the confusion, I have a second resistor on another pin which is used for a second piece of equipment with a RS232 output...
  • stamptrolstamptrol Posts: 1,731
    edited 2006-04-06 12:18
    I know you've said the input speed is fixed at 9600, but everything you're experiencing seems point to a speed issue. Is there the option in the sending device to put a "pacing" time between characters? That may give you the time you need. Otherwise, just move up the the BS2sx which has the speed to work well with 9600 baud and the data manipulation you're looking for.

    The cable lengths you have shouldn't be a problem, in my experience.

    Regards
    Tom
  • syxysyxy Posts: 25
    edited 2006-04-06 12:23
    Hi Tom,

    The equipment concerned is a piece of audio equipment. No possibility of changing the data stream.

    As for BS2sx, I was hoping it could be done with the standard BS2. At least I want to try and work out wether upgrading is the only option left.

    Cheers,

    Richard
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-04-06 13:04
    Richard -

    Here are a couple of thoughts. Perhaps you have tried these ideas, perhaps not.

    Have you tried to receive this data transmission using a PC and Hyperterminal, or any other similar, general purpose, communications program? If not, I'd try that now, just to make sure the baud rate, parity, etc. are what you expect them to be. Using this method there can be no question about the receiving hardware or software, and if it fails, then you need to troubleshoot that problem before advancing to troubleshooting potential problems on the PBASIC Stamp.

    Next, I might consider adding an RS-232 input buffer, since the PBASIC Stamp uses a software UART, and as such, it has very limited input buffering capability. Here is at least one, reasonably inexpensive, external RS-232 buffer which could be added on the input side to eliminate this as a possible problem. This alone MAY permit you to use the BS-2 (rather than a faster PBASIC Stamp processor) in this application:
    http://www.protean-logic.com/Product Info/periph/rsb509l1.htm

    Last, I don't think you've given us any indication as to what this input data actually looks like. Sometimes, based on the contents of the input data, there are certainly "tricks" you can use to speed up SERIN processing. This too may prove very important to solving this seeming too-slow processor problem.

    If you need or want a demo-ware RS-232 datascope program, just ask and I'll shoot you a link to one. With this kind of software, you can see exactly what's coming across the line in binary, octal, hex, ASCII, text or any other format you should choose. You can also dump the data to a secondary storage medium. Additionally, you can change data on-the-fly if necessary.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • syxysyxy Posts: 25
    edited 2006-04-06 15:04
    Hi Bruce,

    Thanks for your comments. Good pragmatic thinking. I can tell you the following:

    "Have you tried to receive this data transmission using a PC and Hyperterminal, or any other similar, general purpose, communications program? If not, I'd try that now, just to make sure the baud rate, parity, etc. are what you expect them to be. Using this method there can be no question about the receiving hardware or software, and if it fails, then you need to troubleshoot that problem before advancing to troubleshooting potential problems on the PBASIC Stamp." - DONE and Works

    "Next, I might consider adding an RS-232 input buffer, since the PBASIC Stamp uses a software UART, and as such, it has very limited input buffering capability. Here is at least one, reasonably inexpensive, external RS-232 buffer which could be added on the input side to eliminate this as a possible problem. This alone MAY permit you to use the BS-2 (rather than a faster PBASIC Stamp processor) in this application:
    http://www.protean-logic.com/Product Info/periph/rsb509l1.htm&quot; - NOT done yet, will do

    "Last, I don't think you've given us any indication as to what this input data actually looks like. Sometimes, based on the contents of the input data, there are certainly "tricks" you can use to speed up SERIN processing. This too may prove very important to solving this seeming too-slow processor problem." - OK, here are some examples:

    The RS232 interface for the G68 can be operated from any terminal which operates at 9600 baud with 1 start bit, 1 stop bit and no parity. Commands take the form of 2 ASCII characters, in some cases followed by a signed argument (‘nn’ below). All characters are echoed by the processor. A carriage return character will execute the command, and backspace is also implemented. When a command is executed, the G68 will return 20 characters.

    The following table lists the commands available and shows examples of the messages returned. These always correspond to the display on the front panel.

    Source Commands.
    CD Selects CD source. (Message send back is this>) CD Trifield 65
    RD Selects Radio. Radio Music 65
    DV Selects DVD. DVD Cinema 65

    "If you need or want a demo-ware RS-232 datascope program, just ask and I'll shoot you a link to one. With this kind of software, you can see exactly what's coming across the line in binary, octal, hex, ASCII, text or any other format you should choose. You can also dump the data to a secondary storage medium. Additionally, you can change data on-the-fly if necessary
    " - I'm open to suggestions wink.gif

    Cheers

    Richard
  • SSteveSSteve Posts: 808
    edited 2006-04-06 22:18
    One question I have when I read through this thread is whether or not you've looked at the input data with DEBUG just to make sure that what you are sending to the display is valid in the first place.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-04-07 01:31
    If you're driving "TTL" rs-232, then you don't need the 22 Kohm resistor. The 22 Kohm resistor only works when the 'send' side is using true RS-232 +-12 volt levels, and you don't have a MAX232 reciever on the 'recieve' side.

    Now, if you're using the BS2 to 'send' to the 'back-pack' directly, then again you don't need the 22 Kohm resistor. If the Backpack requires true RS-232 levels, you're going to need a MAX232 driver on the BS2 side. I assume the backpack does support TTL RS-232 though, so you should be good there.

    One additional point -- TTL-based RS-232 may or may not need the 'invert' option -- which is that $4000 you have in there.
    So, measure the voltage coming back from the BackPack. If it's 5 volts, you don't need the 22 Kohm resistor. Then try it with and without the $4000.

    And do something simple at first, like a Reset followed by a Clearscreen command.
  • syxysyxy Posts: 25
    edited 2006-04-07 07:57
    The Backpack instructions set give: "SEROUT 0,84+$4000,[noparse][[/noparse]"Hello"]" as an instruction. It doesn't talk about TTL at all.

    As for the simple commands, if I send text from the stamp it shows up fine, I use plenty of time for the display to allow it to reset, startup and what have you before I give it any instructions.

    Cheers

    Richard.

    This problem seems to be a bit of a bugger......wink.gif simple task - horribly difficult to get it solved..
  • syxysyxy Posts: 25
    edited 2006-04-08 19:30
    I've tried a few more things and I received a new Matrix Orbital Pled display (which is very nice by the way) and I've come to the following solutions.

    First lets summarize the setup:

    Audio equipment RS232 9600 baud 8N1 out > BS2 stamp > perform actions on string > 9600 output to display

    The problem: text from the equipment is not shown properly on the display - as 20 times 1 character instead of a phrase like this: Radio 56 (20 characters including spaces)

    Possible bottlenecks: the basic stamp is too slow > turns out this is not the case because when I send a string like this using hyperterminal or display tuner at 9600 baud it works

    Possible bottleneck: the data stream from the equipment is not correct > turns out this is not true because if I hookup the display directly to the RS232 equipment port the data shows up fine. This also means that interference is not an issue.

    This leaves only one option: the way the data is treated by the basic stamp is wrong.

    Known factors: String is 20 characters long, The RS232 interface for the G68 (equipment) can be operated from any terminal which operates at 9600 baud with 1 start bit, 1 stop bit and no parity. Commands take the form of 2 ASCII characters, in some cases followed by a signed argument (‘nn’ below). All characters are echoed by the processor. A carriage return character will execute the command, and backspace is also implemented. When a command is executed, the G68 will return 20 characters.
    The following table lists the commands available and shows examples of the messages returned. These always correspond to the display on the front panel.

    I just use the echo bit which consists of 20 characters.

    My current code is like this:

    source VAR Byte(18)
    volume VAR Byte(2)

    DO
    source(18)=0
    volume(2)=0

    SERIN 8,84+$4000,[noparse][[/noparse]STR source\18, STR volume\2]
    PAUSE 50
    SEROUT 11,84+$4000,[noparse][[/noparse]STR source\18]
    PAUSE 50
    SEROUT 11,84+$4000,[noparse][[/noparse]STR volume\2]
    LOOP

    END


    Basically I'm stuck. Can you guys have another look at this case?

    Thanks for all your help. Response has been great.


    Cheers

    Richard
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-08 21:21
    Richard,

    ·· It's entirely possible that it is a timing issue and the Stamp Module is simply not fast enough to receive all 20 characters at once.· Do you have a BS2p or BS2px where you could try using the SPSTR function and use ScratchPad RAM instead?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • syxysyxy Posts: 25
    edited 2006-04-09 18:25
    Chris,

    I am absolutely delighted. For some strange reason, since hooking up a TX output from the stamp to the equipment it works !!!!!

    Good news..


    The next thing I am having difficulties with is the following. I have 2 pieces of equipment sending out RS232 strings. I have now hooked them both up to the stamp whereby equipment A's strings are displayed on line 1 of the display and equipment B's strings are displayed on line two. It works but I probably need to alter my code to minimise timing issues. The problem I am having is that I need to poll pin 8 more often and still leave enough time for the stamp to handle the incomming data on pin 9. I found that 720ms is the best I could come up with after trial and error, still there's data being missed offered on pin 8.

    Equipment A (G68) sends strings at random and equipment B (G91) sends strings every second.

    My code is as follows:

    G91_string VAR Byte '(12)
    G68_string VAR Byte '(14) 'max 26 variables - 21 works

    baud CON 84+$4000
    tolcd CON 11
    tog68 CON 10
    tog91 CON 12
    fromg68 CON 8
    fromg91 CON 9


    PAUSE 100

    SEROUT tolcd,baud,[noparse][[/noparse]254,88] 'clearscreen


    DO
    SERIN fromg68,baud,720,G91_routine,[noparse][[/noparse]STR G68_string\21] 'if no data is received within 0.72 seconds the goto G91_routine

    G68_routine:
    SEROUT tolcd,baud,[noparse][[/noparse]254,71,1,1] 'Goto Column 1, row 2
    SEROUT tolcd,baud,[noparse][[/noparse]STR G68_string\21]

    G91_routine:
    SERIN fromg91,baud,[noparse][[/noparse]STR G91_string\12]
    SEROUT tolcd,baud,[noparse][[/noparse]254,71,1,2] 'Goto Column 1(1-20), row 2
    SEROUT tolcd,baud,[noparse][[/noparse]"G91:",STR G91_string\12]

    LOOP



    I am looking for a 'sexier' piece of code which acts when there's data offered on a pin, not the way it's handled now whereby it moves on if there hasn't been any data on a particular pin for 720ms - I'd like it to move on to case A when there IS data on pin A or move on to case B when there's data on pin B.

    Can this be done?

    Cheers,

    Richard

    Post Edited (syxy) : 4/9/2006 6:29:03 PM GMT
Sign In or Register to comment.