kennm

Hello,

Why am I getting "End of statement expected"

Thanks for the help.

Ken

Private Sub txtReadRegValue_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtReadRegValue.TextChanged

if txtreadregvalue.Text not txtreadregvalue.Text then

cmdOpenPort.PerformClick()

End If

End Sub



Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

ReneeC

I don't kknow if this is a board artifact or not but there should be a continuation line after handles

Private Sub txtReadRegValue_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _ txtReadRegValue.TextChanged

if txtreadregvalue.Text not txtreadregvalue.Text then

cmdOpenPort.PerformClick()

End If

The line in yellow doesn't make sense to me....






Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

kennm

Hi ReneeC,

Adding the continuation line did not work. I don't have a problem with that part of the code. I'm getting the "End of statement expected" on if txtreadregvalue.Text not txtreadregvalue.Text then. With this line I want to see if there's a change in the value and get the new value.

Ken





Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

ReneeC

Then in that case you have to store the old value somewhere to compare the current value either in a static variable as you exit the even or in a member variable.

The IS keyword is used of reference types and a string is a value type and you use "=" for that.






Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

Andreas Johansson

Not is a logical operator and not a comparison operator. You can not use Not to compare one value with another, you can only use it to change the value of a value to its (bitwise) logical negation.

The reason Renee see no reason with the yellow line is that you imply comparing the same value with itself. The line and what you say you want to achieve implies the following pseudo code

if the value of textboxreadregvalue is not equal to textboxreadregvalue then do something

Pay attention to that the text in bold is exactly the same. A value compared to it self will always be equal.

The value of txtreadregvalue.Text will never be the same as txtreadregvalue.Text before txtreadregvalue_TextChanged. That is why you have the TextChanged event, it gets raised when the text changed and you never have to worry about if the text had changed.

When TextChanged event gets raised the value of Text property has changed, your check if it has changed is redundant.






Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

kennm

Hi,

So how would I call  txtReadRegValue_TextChanged from another part of the program if there is a change in value .

Private Sub txtReadRegValue_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtReadRegValue.TextChanged

cmdOpenPort.PerformClick()

End Sub

Ken





Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

Andreas Johansson

You create a new subroutine and call that subroutine from the TextChanged handler and any other place that you might want to do the task associated with TextChanged.

Simplified code

Private Sub HandleTextChanged()
End Sub

Private Sub txtReadRegValue_TextChanged(...) Handles TextChanged
HandleTextChanged()
End Sub

Private Sub SomeOtherRoutine()
DoSomeStuff()
HandleTextChanged()
End Sub






Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

ReneeC

 

Given that it doesn't do much other than cmdOpenPort.PerformClick() which could be called in the other part of the program, there isn't much reason too but let me answer you question.

Assuming that you are not actually using the sending object or event args this is how to do it:

txtReadRegValue_TextChanged( new object, new System.EventArgs)

 






Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

kennm

Thank you both for trying to help me but I still don't get it. Let me try and explain what I am trying to do. I found some modbus source code that will allow me to read and write to a PLC but, in order to read I have to click on the Read Register button. By clicking the button it opens the com port gets the value and closes the port. I would like to keep the port open and read the changing values in an analog register. I tried to approach this from a different angle and remove all frm_Comms.Close() references but this did not work. I am relatively new to VB and I've only written a few small programs.  So any help is most appreciated.
Ken




Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

Andreas Johansson

It is also really difficult for us to get why removing frm_Comms.Close() didn't work for you. If you want to keep the connection open it might be one step to do it but what happens when a function tries to open an open com port

If you post more specific details about your problems you might get better help.






Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

kennm

Why when removing frm_Comms.Close do I still have to click Read Registers button to get updated values. Is leaving the port open a no no Is there a better way to continually read the read registers. Here is the code that opens the comm port. If you need to see additional code let me know.

Private Sub cmdReadRegister_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReadRegister.Click

cmdOpenPort.PerformClick()

Dim Response, message, msgg As String

Dim command_type, addres As Short

Dim registerr As Single

Dim data_value(63) As Single

Dim Function_Code As Single

Dim Number_of_registers As Single

Function_Code = FunctionCodeTextRead.Text

Number_of_registers = Read_num_register.Text

command_type = Function_Code 'Modbus command type for a read command

addres = Val(txtModbusAddress.Text) 'read the control's modbus address stored in this text box

registerr = Val(txtReadRegNum.Text) 'read the the register number stored in this text box

data_value(1) = Number_of_registers 'Read only one register

data_value(2) = END_OF_DATA_VALUES 'let the "build_modbus_message$" routine know this is the last data value

message = build_modbus_message(command_type, addres, registerr, data_value) 'send the parameters to the routine to build a modbus packet

'**checks the message to be transmitted to the controller without formatting**

'**shows the individual bytes to be sent in decimal**

Transmitted.Text = AsHex(message)

Select Case VB.Left(message, 3)

Case "Bad"

'something was wrong with the message data.

MsgBox("Valid parameters not available to assemble Modbus packet")

Case Else

'transmit out of the port the modbus packet and get a response back from the control

Response = transmit(PROTOCOL_MODBUS, message, MAX_COMMS_TIMOUT_TIME)

'**checks what is coming back from the controller without formatting**

'**shows the individual bytes coming back from the control in decimal**

'Dim y% ', msgg$

'For y% = 1 To Len(Response$)

'msgg$ = msgg$ & Asc(Mid(Response$, y%, 1)) & " "

'Next y%

'MsgBox msgg$

'Debug.Print msgg$

'*********** testing routine for tdesting message**********

Reply.Text = AsHex(Response)

Response = convert_modbus_data(MESSAGE_RECEIVED, Response)

'convert the modbus response to decimal numbers.

End Select

Select Case Response

Case "No Response"

msgg = "There was No Response from the controller!" & Chr(13)

msgg = msgg & "Check Settings and try again!"

MsgBox(msgg)

Case "CRC ERROR"

MsgBox("A CRC Error occurred in the last packet!")

Case Else

txtReadRegValue.Text = Response

End Select

frm_Comms.Close()

End Sub

Private Sub cmdOpenPort_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOpenPort.Click

'call subroutine to initialize comm port. Arguments to be passsed

'to routine are the PC's Comm Port Number and the Baud Rate to use.

init_comms(Val(txtPCCommPort.Text), Val(txtCommBaudRate.Text))

txtPortInialized.Visible = True

'Sub init_comms(ByRef CommPortt As Short, ByRef CommBaudRatee As Short)

End Sub

Sub init_comms(ByRef CommPortt As Short, ByRef CommBaudRatee As Short)

'remove all old comms devices

frm_Comms.Close()

ReDim Comms_Ctrl(0)

Dim ctrl_num, comm_port As Short

Dim port_settings As String

Dim first_comma As Short

ctrl_num = 0

comm_port = CommPortt 'get_ini_int%(section$, entry$, int_default%, ProgIniFilename$)

Select Case comm_port

Case 1 To 6

'entry$ = "DEVICE " & LTrim$(Str$(ctrl_num%)) & " PORT SETTING "

port_settings = Str(CommBaudRatee) & ",N,8,1" 'get_ini_string$(section$, entry$, default$, ProgIniFilename$)

Select Case Len(port_settings)

Case 9, 10, 11

'this is a valid port setting.

Case Else

MsgBox("Port settings are not valid in sub init_comms")

port_settings = "9600,N,8,1"

End Select

'redimension the arrays to the number asked for.

ReDim Preserve Comms_Ctrl(ctrl_num)

'add (load) another comms controls.

If ctrl_num <> 0 Then frm_Comms.com_Device.Load(ctrl_num)

Comms_Ctrl(ctrl_num) = frm_Comms.com_Device(ctrl_num)

define_comms_control(ctrl_num, comm_port, port_settings)

first_comma = InStr(1, port_settings, ",")

'assume 10 data bits, so multiply reciprocal of baud rate by 10.

character_delay(ctrl_num) = ((1 / Val(Mid(port_settings, 1, first_comma))) * 10)

'convert milliseconds time to a whole number.

character_delay(ctrl_num) = (character_delay(ctrl_num) * 1000)

'set delay to at least 1 millisecond.

If character_delay(ctrl_num) = 0 Then character_delay(ctrl_num) = 1

ctrl_num = ctrl_num + 1

Case Else

MsgBox("Port number is not valid in sub init_comms")

End Select

End Sub





Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

ReneeC

K,

It would really helpful, if you have WORD to cut and paste from the VBE editor to Word and then cut and paste to the board, this will preserve your formatting and your code will be much easier to read.






Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

kennm

Try This.

Private Sub cmdReadRegister_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReadRegister.Click

cmdOpenPort.PerformClick()

Dim Response, message, msgg As String

Dim command_type, addres As Short

Dim registerr As Single

Dim data_value(63) As Single

Dim Function_Code As Single

Dim Number_of_registers As Single

Function_Code = FunctionCodeTextRead.Text

Number_of_registers = Read_num_register.Text

command_type = Function_Code 'Modbus command type for a read command

addres = Val(txtModbusAddress.Text) 'read the control's modbus address stored in this text box

registerr = Val(txtReadRegNum.Text) 'read the the register number stored in this text box

data_value(1) = Number_of_registers 'Read only one register

data_value(2) = END_OF_DATA_VALUES 'let the "build_modbus_message$" routine know this is the last data value

message = build_modbus_message(command_type, addres, registerr, data_value) 'send the parameters to the routine to build a modbus packet

'**checks the message to be transmitted to the controller without formatting**

'**shows the individual bytes to be sent in decimal**

Transmitted.Text = AsHex(message)

'Dim y% ', msgg$

'For y% = 1 To Len(message$)

'msgg$ = msgg$ & Asc(Mid(message$, y%, 1)) & " "

'Next y%

'MsgBox msgg$

Select Case VB.Left(message, 3)

Case "Bad"

'something was wrong with the message data.

MsgBox("Valid parameters not available to assemble Modbus packet")

Case Else

'transmit out of the port the modbus packet and get a response back from the control

Response = transmit(PROTOCOL_MODBUS, message, MAX_COMMS_TIMOUT_TIME)

'**checks what is coming back from the controller without formatting**

'**shows the individual bytes coming back from the control in decimal**

'Dim y% ', msgg$

'For y% = 1 To Len(Response$)

'msgg$ = msgg$ & Asc(Mid(Response$, y%, 1)) & " "

'Next y%

'MsgBox msgg$

'Debug.Print msgg$

'*********** testing routine for tdesting message**********

Reply.Text = AsHex(Response)

Response = convert_modbus_data(MESSAGE_RECEIVED, Response)

'convert the modbus response to decimal numbers.

End Select

Select Case Response

Case "No Response"

msgg = "There was No Response from the controller!" & Chr(13)

msgg = msgg & "Check Settings and try again!"

MsgBox(msgg)

Case "CRC ERROR"

MsgBox("A CRC Error occurred in the last packet!")

Case Else

txtReadRegValue.Text = Response

End Select

frm_Comms.Close()

End Sub

Private Sub cmdOpenPort_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOpenPort.Click

'call subroutine to initialize comm port. Arguments to be passsed

'to routine are the PC's Comm Port Number and the Baud Rate to use.

init_comms(Val(txtPCCommPort.Text), Val(txtCommBaudRate.Text))

txtPortInialized.Visible = True

'Sub init_comms(ByRef CommPortt As Short, ByRef CommBaudRatee As Short)

End Sub

Sub init_comms(ByRef CommPortt As Short, ByRef CommBaudRatee As Short)

'remove all old comms devices

frm_Comms.Close()

ReDim Comms_Ctrl(0)

Dim ctrl_num, comm_port As Short

Dim port_settings As String

Dim first_comma As Short

ctrl_num = 0

comm_port = CommPortt 'get_ini_int%(section$, entry$, int_default%, ProgIniFilename$)

Select Case comm_port

Case 1 To 6

'entry$ = "DEVICE " & LTrim$(Str$(ctrl_num%)) & " PORT SETTING "

port_settings = Str(CommBaudRatee) & ",N,8,1" 'get_ini_string$(section$, entry$, default$, ProgIniFilename$)

Select Case Len(port_settings)

Case 9, 10, 11

'this is a valid port setting.

Case Else

MsgBox("Port settings are not valid in sub init_comms")

port_settings = "9600,N,8,1"

End Select

'redimension the arrays to the number asked for.

ReDim Preserve Comms_Ctrl(ctrl_num)

'add (load) another comms controls.

If ctrl_num <> 0 Then frm_Comms.com_Device.Load(ctrl_num)

Comms_Ctrl(ctrl_num) = frm_Comms.com_Device(ctrl_num)

define_comms_control(ctrl_num, comm_port, port_settings)

first_comma = InStr(1, port_settings, ",")

'assume 10 data bits, so multiply reciprocal of baud rate by 10.

character_delay(ctrl_num) = ((1 / Val(Mid(port_settings, 1, first_comma))) * 10)

'convert milliseconds time to a whole number.

character_delay(ctrl_num) = (character_delay(ctrl_num) * 1000)

'set delay to at least 1 millisecond.

If character_delay(ctrl_num) = 0 Then character_delay(ctrl_num) = 1

ctrl_num = ctrl_num + 1

Case Else

MsgBox("Port number is not valid in sub init_comms")

End Select

End Sub





Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

ReneeC

"
Thank you both for trying to help me but I still don't get it. Let me try and explain what I am trying to do. I found some modbus source code that will allow me to read and write to a PLC but, in order to read I have to click on the Read Register button. By clicking the button it opens the com port gets the value and closes the port. I would like to keep the port open and read the changing values in an analog register. I tried to approach this from a different angle and remove all frm_Comms.Close() references but this did not work. I am relatively new to VB and I've only written a few small programs. So any help is most appreciated."
Ken
I'll do my best. I'm not an expert on this kind of Comm.
There are som things that are not clear. How often do you want Samples( ) or data How do you expect it to driven
Like would ten samples per second be enough Too Much
Analog register Is this on the PLC
I think I'm asking for a clearer picture in the nature of is it smaller than a breadbox or smaller than a house
How often do you want a sample, how data is flowing





Re: Visual Basic Express Edition Why am I getting "End of statement expected"?

kennm

I would like to start the VB program and have it automatically sample the data every 100 milliseconds.  The analog information is a16 bit value in the plc. Data is flowing over rs232 or 485 using Modbus RTU protocol (http://www.modicon.com/techpubs/toc7.html).  I don't understand what you mean by "How do you expect it to driven ".