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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSComm (input command)

Status
Not open for further replies.

Siodeneo

Programmer
Mar 23, 2006
3
DE
HI,
I am writing you, because I have a problem with
reading holding register based on Modbus RTU protocol.
I am developing my software under using Visual Basic 6.0
I can communicate with my end device. But only give him
a write command.
I am using the following codes and settings:


MSComm.Settings = 9600,N,8,2

MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.InputLen = 0

MSComm1.Output = Chr(Hex(byte1)) + Chr(Hex(byte2)) + Chr(Hex(byte3)) + Chr(Hex(byte4)) + Chr(Hex(byte5)) + Chr(Hex(byte6)) + Chr(crc16) + Chr(crc16)

The output command is working perfect.So i can change parameters in my
end device. But now I also want to recieve(read) parameters out of my end
device. With Modbus RTU I am sending the folowing frame

MSComm1.Output = "station address" + "03h for read" " + 2 bytes for Function code" + "2 bytes of read data" + "2 bytes crc16 error check"
But I don´t recieve anything from my deviece.

'''''For input i am using..

Private Sub MSComm1_OnComm() ' for input
Select Case MSComm1.CommEvent
Case comOverrun: MsgBox "Lost data!"
Case comRxOver: MsgBox "Lost data!"
Case comEvReceive: text2.text = MsgBox(MSComm1.Input)
End Select
End Sub

What I am doing wrong? I thing the codes for MSComm1.Input is
wrong.
Thank you very much for your help.

Sio
 
First off:

The way you have coded the on comm method is going to result in loss of data.

You have specified the rxthreshold at 1. This means that on every character that arives at the port an event will be generated. Since your data probably exists of more than one character and it probably has a closing character (or sequence of characters), you should build up a string in the on comm event by concatenating everything that you receive on the port to this string up until you receive the closing character. The InputLen=0 property doesn't help you here because, even though it reads the entire contents of the rx buffer, the data to be received from the device might not yet be there completely yet, since the event is already fired upon the arrival of the first character.

Second:
Is the event fired at all? If not, you probably do not get any answer and you might want to hookup a protocol analyzer to check the data you send and the answers you get from the device.

Last, but not least:
Please note that on reading the Input property of the comm control, the input gets cleared. This means that, for example, with hovering your mouse, reading it in the immediate window etc. the rx buffer will be cleared and further upon in your code, where you actually want te read it in, it will be gone.....

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top