RS485, Visual Basic and BS2
Chen
Posts: 15
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?
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
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,
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.
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.
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.
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?
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,
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.
At the Stamp, have you tried using the "inverted" serial mode?
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.