Data communication between PLC and VB6 - RS232


Thread Starter


hello friends, i am trying to send and receive the data from PLC, which has a RS232 port to PC. i connected PLC and PC by using RS232 cable and wrote a five line code in Visual Basic form load, which are as follows (i am using Mscomm object) to display the data which the PLC is sending... private sub form_load() Mscomm1.comport=1 Mscomm1.settings="9600,N,8,1" mscomm1.inputlen=0 Mscomm1.portopen=true Text1.text=Mscomm1.input end sub i neither geting any data or any errors. can any one correct me where i am wrong? expecting the replies..... bye girish

Crucius, Wesley

Seems to me you asked and received several answers to this question already, but here goes again: In order for your code example to work, the serial data would have to arrive in between the execution of the following lines: Mscomm1.portopen=true Text1.text=Mscomm1.input Hopefully it is obvious why that is not likely to happen. What you want to do is catch the OnComm event, then use a select statement to test for a received data event, then get and save the data, then go back and see if any more data arrived while you were getting the first chunk of data. If you ignore this last step, you have the potential to leave characters sitting in the receive buffer until more data is received. The other alternative you have is to poll for data either via timer event or button/keypress event. This is however NOT appropriate for most real-world applications... If this still doesn't make sense, let me know and I will try to spell it out more explicitly.

Gerald Moore

The first thing you need to do is connect your computer to another with a null modem cable. Then using Windows terminal on the dummy or test computer. Send and receive characters to your computer with the VB program. Do this until you nail down your code. As suggested earlier use the OnComm Event to receive and loop it until you get all the characters in the string, you should be searching for some sort of end character to mark the end of string.