Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

serial communication - help please

Status
Not open for further replies.

nikmast

Programmer
Feb 17, 2005
5
NZ
Hi, I need help with visual basic 6 program or hardware of plc. I am trying to get vb6 program to read serial port that my plc s7-200 CPU 222 is conected to. I can not get the program to read anything at all. My code is (in vb6):


Private Sub cmdOpen_Click()
'opening the serial port

If MSComm1.PortOpen = False Then
MSComm1.CommPort = 1
MSComm1.Settings = "9600, N, 8, 1"
MSComm1.InputLen = 0 'to read the whole input buffer
MSComm1.PortOpen = True
MSComm1.Handshaking = comXOnXoff
Else
MsgBox "Connection has already been established", vbInformation, "Port opened"
End If

End Sub


Private Sub cmdDisplay_Click()
'to display the data from the receive buffer

MSComm1.InputMode = comInputModeBinary
txtDisplay.Text = MSComm1.Input
txtCharacters.Text = MSComm1.InBufferCount

End Sub

Please if anybody can tell me what is wrong with my code or give me a code that works, if posible a basic code (begginer) I would be very, very gratfull. If the code is ok, is there a problem with my connection with the plc, but I can download a code to a plc fine with the step7 template and also monitor the program how it works with the step7 program, so I do not think it is a hrdware/connection problem.


Do I have to tell or use some kind of comand (code) to make the plc to send me the information through the serial port.


Thank you.
 
Ok, you need to put the following code in your program before you open the port

MSComm1.RThreshold = 1

I think this will solve your problem. If not, then check your speed, and parity settings to insure they are equal at both ends. By-the-way, what MCU are you using?

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Thank you for your answer, ccan you tell me what is MCU?
 
'to read the whole input buffer

does the data have a fixed length? in which case set rthreshold to the length of the data

then in the oncomm event check commevent for comEvReceive

Code:
Private Sub cmdOpen_Click()
'opening the serial port

If MSComm1.PortOpen = False Then
MSComm1.CommPort = 1
MSComm1.Settings = "9600, N, 8, 1"
MSComm1.InputLen = 0 'to read the whole input buffer
MSComm1.PortOpen = True
MSComm1.Handshaking = comXOnXoff
MSComm1.InputMode = comInputModeBinary
Else
MsgBox "Connection has already been established", vbInformation, "Port opened"
End If

End Sub 

Private Sub MSComm1_OnComm()

    If MSComm1.CommEvent = comEvReceive Then
        txtCharacters.Text = MSComm1.InBufferCount 'read buffer count before you empty buffer
        txtDisplay.Text = MSComm1.Input
    end if
end sub

a little side note i only found out a few days ago, if your debugging dont hover your mouse over mscomm1.input, it empties the buffer

good luck

If somethings hard to do, its not worth doing - Homer Simpson
 
Sorry MCU = MicroController Unit

I set the RThreshold to 1 just so it will fire the onComm event immediately upon ANY data arriving on the port, the default is 0 which NEVER fires the OnComm event...


LF


"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Guys thank you very much for all your help, I figured what the problem was, I made a command in my plc to send the data through the serial port. Now it warks, sort off.

My new problem is that whatever the plc is sending me the computer can not understand it, as if it is not sending me 0's and 1's but something else. When I put mouse over mscomm1.input I see squares not 1's and 0's. Helpk, anyone?
 
nikmast,

Ok, have you ever received any data prior to what modifications you made? If so, was that data legible? There is a rule of thumb with respect to microcontroller communications that implies that Microcontrollers with send data using ASCII characters rather than legible letters. Doing so frees the programmer from having to only use the 26-letters of the alphabet. What you need to do is, on your "ComEvReceive" portion of your code you will want to include the following:

YOurTextvariableHere = AscB(MsComm1.input)

This will return the ANSI of the first letter of the input byte--I am assuming that you are correct about the input mode being "comInputModeBinary" as opposed to "comInputModeString". It is likely that your Microcontroller will need to "see" the opposite coming from your VB program which would be:

Mscomm1.output = ChrB(YourOutputByteHere)


Ok, Now, the question arises, "Are you supposed to have the Comm mode as binary?", if you are, then the above may work for you, however, if you are not, then you will have to change your mode to string. Frankly, I have only seen Byte mode when it comes to things such as serial port cameras where the Computer is expected to receive Bytes of data that, when compiled, will form into a picture. If you are simply sending commands back and forth to the MCU, then I would say that you will need to change the mode to String and see how that works for you. i have more, but I am going to wait for your responce on this until I take up anymore space.


LF




"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top