I can control a single relay using the code as per published example.
I can not get the relay to respond when I attempt control via “B” - send byte.
Does anyone know the correct syntax for the use of a byte in the expression for the checksum? I want to address 2 relays at a time.
I am using vb 2005 and checksums greater than 127 display on a monitor as ?. The problem is that character between 128 - 255 are not represented in the standard vb encoding.
If I use unicode encoding I obtain the same character sequence as in the published example but the relays will not respond.
I monitor the transmitted data on a hyperTerminal.
The following is the code I am trying to use for control with a single byte command.
[code]
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
Dim checksum As Integer
Dim Relays As Byte
Relays = (TextBox2.Text)
checksum = (255 - ((((13 + 1 + Asc(“B”) + (Relays)) / 256) - Int((13 + 1 + Asc(“B”) + (Relays)) / 256)) * 256)) + 1
messagestring = Chr(13) & Chr(1) & "B" & (Relays) & Chr(checksum)
messagestring = messagestring & messagestring
messagestring = messagestring & messagestring
SendSerialData(messagestring)
End Sub
Sub SendSerialData(ByVal data As String)
' Dim enc As Encoding = Encoding.Unicode
' Send strings to a serial port.
Using com1 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM1", 2400, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
' com1.Encoding = enc
com1.WriteLine(data)
End Using
End Sub[/code]
The checksum syntax now works as long as no symbols or number has a code value larger than 128. The number represented by the byte must be greater that 48 for the values to be below 128.
What encoding is being used in the PIC device?
The behavior stated above occurred with ASCII or UTF-8 encoding. It was expected using ASCII since above 127 a ? is passed.
Would you verify that the vb6 example 3 = 1 + 2 = relay 7 & 8 does turn on those relays?
You don’t mean to imply 8 = 3 + 5 = relay 6 & 4 do you?
I think you just happened to chose an example that the binary representation matches the arithmetic.
I would think that these relays would be control by a 20 = 00010100. Is this correct?
In Visual Basic 6.0, if you do not specify ByVal or ByRef for a procedure parameter, the passing mechanism defaults to ByRef. This allows the variable passed into the procedure to be modified in the calling program.
When you declare a procedure in Visual Basic 2005, the passing mechanism defaults to ByVal for every parameter. This protects arguments against modification.
See also:
Array Bounds for Visual Basic 6.0 Users
Array Size Declaration for Visual Basic 6.0 Users
ReDim Statement for Visual Basic 6.0 Users
Data Type Changes for Visual Basic 6.0 Users
Universal Data Type Changes for Visual Basic 6.0 Users
Declaration Syntax for Visual Basic 6.0 Users
String Length Declaration for Visual Basic 6.0 Users
Structure Declaration for Visual Basic 6.0 Users
Variable Scope for Visual Basic 6.0 Users
Function Changes for Visual Basic 6.0 Users
Format Function for Visual Basic 6.0 Users
Date and Time for Visual Basic 6.0 Users
String ($) Function for Visual Basic 6.0 Users
Miscellaneous Language Changes for Visual Basic 6.0 Users
Boolean Operator for Visual Basic 6.0 Users
File Handling for Visual Basic 6.0 Users
Module Changes for Visual Basic 6.0 Users
Procedure Changes for Visual Basic 6.0 Users
Parameter Passing Mechanism for Visual Basic 6.0 Users
Procedure Calling Sequence for Visual Basic 6.0 Users
Procedure Declaration for Visual Basic 6.0 Users
Thank you for your prompt replies.
Your service has been exceptional.
I was able to determine the encoding page after getting the system working with vb6. I now have the relays controlled by my vb 2005 program.
You may see this issue again since there will be more use of vb 2005. For your future reference the required character page is 1252. The following is a snippet of the code I use.
Sub SendSerialData(ByVal data As String)
Dim enc As Encoding = Encoding.GetEncoding(1252)
' Send strings to a serial port.
Using com1 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM1", 2400, IO.Ports.Parity.None, 8)
com1.Encoding = enc
com1.WriteLine(data)
End Using
End Sub
Thanks again.
PS: 1 more question. What is the latency time of the cpu? I estimate it to be about 200us. I would like to turn on 2 relays located on different boards in rapid secession.
Please refer to the manual. It gives hints regarding the instruction timing. Respect this timing. You can optimise your software by increasing the speed until it no longer works, then reduce it again to a allow a reasonable margin.