Shop OBEX P1 Docs P2 Docs Learn Events
RS485, Visual Basic and BS2 — Parallax Forums

RS485, Visual Basic and BS2

ChenChen Posts: 15
edited 2011-09-17 07:22 in BASIC Stamp
I am trying using RS485 for communication between PC and BASIC Stamp (BS2). To convert level, MAX232 is used together with SN75176. RD of PC serial port is connected to pin 14 of MAX232. Pin 11 of MAX232 is connected to R of SN75176. RTS of PC serial port is connected with pin 13 of MAX232. Pin 12 of MAX232 is connected with both RE/ and DE of SN75176. TD of PC serial port is connected to pin 8 of MAX232. Pin 9 of MAX232 is connected to D of SN75176.

On the other hand, pin 15 of BS2 is connected to R of the other SN75176. Pin 14 of BS2 is connected to D of the other SN75176. Pin 13 of BS2 is connected to RE/ and DE of the other SN75176.

In all, between the PC serial port and BS2 are the MAX232 and two SN75176s.

Here is my BS2 code:

N96N CON 84 'non-inverted, 9600
getdata var byte
RS CON 14
TRControl CON 13
DIR13=1 '1 stands for output
DIR14=1
DIR12=1
'Direction control:
DIR15=0
qq CON 12 'LED connected

aa:

LOW TRControl
PAUSE 100
SERIN 15, 84, 1000,aa, [DEC getdata]
IF (getdata=1) THEN conf

conf:
low qq
HIGH TRControl
SEROUT RS, N96N,["ok"]
GOTO aa
else
HIGH qq
GOTO aa

ENDIF

and my Visual Basic program on the PC is:

mscomm1.portopen=true
On Error Resume Next
MSComm1.InputMode = comInputModeText
MSComm1.InputLen = 0
MSComm1.RThreshold = 0
MSComm1.RTSEnable = False
Sleep 200
Do
MSComm1.Output = Str(1)
Loop Until MSComm1.OutBufferCount = 0
MSComm1.RTSEnable = True
Sleep 200
Buffer = MSComm1.Input




===================the response is:
I could not see the characters "ok" appears as the buffer text, nor could I see the LED is on(it is always off). What happens to my program? Is my wiring correct?

Can anyone send me a working example using Visual BASIC and BS2 for application of RS485, including the wiring? How to output "1" to the BS2 in my case?

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2011-08-21 05:10
    The first step is to get the PC to talk to the stamp via rs-232. That would have the serial port of the PC talking to the stamp's programming port. That way, the hardware challenges are minimized.

    Once you know that the two devices can talk/listen and both pieces of code work, then put the max232 at the stamp using two regular i/o pins.

    Finally, with the code working, have the stamp drive the 75176 directly and put a 75176 and max232 at the PC.

    A circuit diagram would be a better way of keeping track of the connections ; )

    Cheers,
  • ChenChen Posts: 15
    edited 2011-09-02 01:13
    I used the reference of Jan Axelson's book of Serial Port Complete. Included in the book is an example of RS485 for BS2 and Visual Basic. However, it seems that book has some errors and the program does not work. Anyone has experience? I don't have any problem for using RS232. My problem is that I could not use the I/O pin of BS2 or SERIN/SEROUT to communicate with PC port programmed in the language of Visual Basic for RS485 communication. If anyone may help! Please make an example.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-09-02 18:36
    Hello Chen, as Tom suggests start with RS232, the simplest of programs and a basic hardware setup.

    I have here a minimal VB program and a complimentary BS2 program that will accept the ASCII number 1 and transmit "ok".

    For this test only 3 wires will be required from the MAX 232, a common ground wire from the MAX to the BS2, a TX wire from the MAX to P15 of the BS2 and a RX wire from the MAX to P14 of the BS2.

    The VB program has a form with two text boxes and one button, Text 1 will display characters received from the Stamp and Text 2 is used to transmit a value between 0 and 255 to the Stamp (the appended CR is important). Rather than poll the port buffer for characters this program uses the event driven OnComm. The port is configured when the app is started, all you need to do is modify the CommPort to suit the com port that you are using.
    Option Explicit
    
    Private Sub Form_Load()
    
    	MSComm1.Settings = "9600,n,8,1"
    	MSComm1.InputLen = 1
    	MSComm1.RThreshold = 1
    	MSComm1.CommPort = "7"
    	MSComm1.PortOpen = True
    
    End Sub
    
    Private Sub Command1_Click()
    
    	MSComm1.Output = Text2.Text
    
    End Sub
    
    Private Sub MSComm1_OnComm()
    
    	Dim sdata As String
    	sdata = MSComm1.Input
    	Select Case MSComm1.CommEvent
    	Case comEvReceive
    	Call HandleInput(sdata)
    	End Select
    
    End Sub
    
     Sub HandleInput(InBuff As String)
                   
             	Text1.SelStart = Len(Text1.Text)
             	Text1.SelText = InBuff
               
     End Sub
    
    getdata VAR Byte
    
    Main:
    
    SERIN 15, 84,[DEC getdata]
    
    IF (getdata=1) THEN GOSUB conf
    
    GOTO Main
    
    conf:
    
    SEROUT 14,84,["ok"]
    
    RETURN
    

    When you have this working you will have a good foundation to build on. If there is anything you don't understand please ask.

    Jeff T.
  • ChenChen Posts: 15
    edited 2011-09-02 21:39
    Your program should work for RS232. My question relates with RS485. I followed that book but could not make it work.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-09-03 07:17
    Hello Chen,
    Your program should work for RS232. My question relates with RS485. I followed that book but could not make it work.

    your missing the point, unless you fully understand how to communicate between Visual Basic and the Basic Stamp and you continue to copy and paste large programs that someone else has written it is going to take you much much longer to learn anything.

    RS485 describes the electrical charateristics of a circuit and not the communication protocol. If you can build and develop a RS232 communications between VB and the BS2, with a simple program that you understand enough to be able to modify, then you can move on to designing your 485 hardware.

    If you suspect a problem with your hardware design then attatch a schematic of the circuit so that others can advise on any improvements.

    Jeff T.
  • bsnutbsnut Posts: 521
    edited 2011-09-09 23:56
    I agree with Jeff T. point here.
    RS485 describes the electrical charateristics of a circuit and not the communication protocol. If you can build and develop a RS232 communications between VB and the BS2, with a simple program that you understand enough to be able to modify, then you can move on to designing your 485 hardware.
    The Basic Stamp uses RS232 only to communicate with a computer and also uses RS232 to communicate with the RS485 chip with a Basic Stamp I/O pin used to allow the RS485 chip to transmit over the RS485 network.

    If your Basic Stamp isn't connected at the computer and is 100ft away, RS485 is what you need. But, you will need a RS232 to RS485 converter at the computer end to pull this RS485 network off. Is this what you are trying to do?
  • ChenChen Posts: 15
    edited 2011-09-13 03:02
    Agree. All I need is someone who can point out the error in the program codes as listed. I don't have any problem in using RS232. But my first project on RS485 fails. Although I followed Jan Axelson's book I could not make it work. In the beginning I doubt if the components I used may have problems, but when I replace them with other ones it could not work either. If I use BS2's serial port and continuously send out one data then may be seen on the PC. Nevertheless, when I want to send out "1" from PC and tell the BS2 to respond and send back "ok" using RS485 it did not work at all. I applied Texas Instruments' 75176 and MAXIM IC's MAX232 in the between. If someone ever read Jan Axelson's book-serial port complete and can give some hints to make it work it will be great.
  • stamptrolstamptrol Posts: 1,731
    edited 2011-09-13 03:55
    The rs485 projects I've worked on are all based on Jan Axelson's book. They do work.

    Again, a schematic of your connections (what they actually are, not what you think they are) will help with sorting out the hardware side.

    On the software side, its easier to work from something that DOES work rather than flail away with something that doesn't work. That's all we're suggesting here..... get it working with rs232 then move on to '485.
    None of your effort will be wasted and you'll end up with a better understanding.

    Cheers,
  • bsnutbsnut Posts: 521
    edited 2011-09-14 02:45
    I agree with Tom's point here 100%
    That's all we're suggesting here..... get it working with rs232 then move on to '485.
    None of your effort will be wasted and you'll end up with a better understanding.
    Start small then work your way up to bigger things and this case RS485.

    One thing, that I will do for you is provide this App Note by Scott Edwards as an attachment, which should give you basic understanding what we are talking about.
  • ChenChen Posts: 15
    edited 2011-09-15 20:01
    Well, although my project problem has not been solved I still want to thank you guys. Maybe something is wrong with the electronic devices I used. But Jan's book was really wrong in some places for RS485, though some had been corrected for the second edition. The schematic and the code don't match and malfunction. Now I am waiting for someone who ever used Jan's book as Serial Port Complete on the part of RS485. If you furthermore used TI 75176 and MAX232, and programmed in Visual Basic 6.0, I hope you can point out my programming problem for the codes listed at the top of this post. Moreover, if you have such codes in VB for the application of TI 75176 and MAX232 for communication between a PC and a BS2 then most probably my question can be solved when you post or send me the example codes. Please notice it that I just want to send out text "1" from PC to a BS2 microcontroller (first enable the transmitter), let BS2 wait for "1"(first enable receiver), then let BS2 send out "ok" to PC(first enable receiver of 75176). I must use TI 75176 and MAX232. The SERIN and SEROUT pins on the BS2 are used for communication. In addition, I used TIMER EVENT to call the VB subroutine listed on the top of this post. Should I use the MSCOMM1_ONCOMM subroutine as instead? Waiting, I am waiting, hope the world respond!
  • stamptrolstamptrol Posts: 1,731
    edited 2011-09-16 06:24
    What are your connections between the two 75176 chips?

    At the Stamp, have you tried using the "inverted" serial mode?
  • bsnutbsnut Posts: 521
    edited 2011-09-16 20:14
    What I would suggest to you is to try to get two Basic Stamps working using Scott Edwards app note that I attached in my last post. This way you will get a better understanding what is happening. The good thing is, that Scott did the hard work for you and all you haved to do is wire it up as shown and programmed the Basic Stamps. Then, we will be happy to help you out with Visual Basic 6.0 code
  • ChenChen Posts: 15
    edited 2011-09-16 21:04
    Hi, Tom, except some resistors and wires there is nothing else between the 75176. I tried both inverted and non-inverted methods but neither works. I tried both TIMER EVENT communication and MSCOMM1_ONCOMM interrupt communication and neither works. I think it will work for putting two microcontrollers such as BS2 together. But when it comes to PC and BS2 then it gets stucked.
  • stamptrolstamptrol Posts: 1,731
    edited 2011-09-17 05:07
    Chen,
    As I've been trying to hint at all along, the hardware aspects are just as important as the software. I was trying to check the hardware connections between your two 75176 chips. Not having your schematic, I've been looking up the connections and checking things in a less than optimum manner.

    You've already been given working examples of the software for both Stamp and PC, so the hardware needs to be examined more closely.
  • Mike GMike G Posts: 2,702
    edited 2011-09-17 07:22
    @chen, Use the same connection you use to program the BS2 to verify your VB and P-Basic code. First thing, get the BS2 and PC communicating. Then you can set, the know good, code aside and start working on the physical connections. This is called unit testing and is a vital process/skill in your development toolbox. Doing too many things at once causes frustration and shot-in-the-dark troubleshooting.
Sign In or Register to comment.