-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Did you try the code I posted in Post #56?
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Quote:
Originally Posted by
Doogle
Did you try the code I posted in Post #56?
I did, and it did not do anything (no error or no action)
So, this is when I went and tried their tool
sent
x1a05//
It also did not do anything.
So your code maybe right on, but their instructions could be wrong or misunderstood?
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
So I basically sent an email to Manufacturer with the same info as my post 60
Ask specifically how to exactly send the command.
This is what I got back
Quote:
The whole command consist of 5 bytes.
Byte N1 is the char "x"
Byte N2 represents the relays from 1 to 8. The MSB is relay 1. - This means that if you have this byte b10000000 / 128(dec) or 0x80(hex) or the ASCII symbol '€' - relay N1 is ON. If you have b10000001 - relay N1 and relay N8 are ON. What I mean is that when you send this byte you must remember that this byte consist of 8 bits and each bit is one relay. Bit N7 is relay1, bit N6 is relay2...bitN0 is relay 8.
Byte N3 represents the relays from 9 to 16. The MSB is relay 9. - the same story like byte N2.
Byte N4 is the char "/"
Byte N5 is the char "/"
So I am still unclear what to send as even with their Com Tool
x1a05// does not work.
Can some one help me interpret?
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Re my code in Post #56, it is not calling funask so to see the results of the 'Send Sequentially' you'd either need to click on 'ask' or modify the code to:
Code:
Private Sub cmdSequential_Click()
Dim Command As String
Dim byts(1) As Byte
Dim intI As Integer
Dim intJ As Integer
Dim intK As Integer
'
' Assumes that odd numbered Options = on
'
intK = 1
For intI = 0 To 1
For intJ = 7 To 0 Step -1
'
' Set the appropriate bit if the relay is to be turned on
'
If OpRelay(intK).Value = True Then
byts(intI) = byts(intI) Or 2 ^ intJ
End If
intK = intK + 2
Next intJ
Next intI
Command = "x" & byts(0) & byts(1) & "//"
MSComm1.Output = Command
Attente (50)
funAsk
End Sub
also, for Debugging purposes, you may like to include the following into your Form. It will report any Communications problems to the Immediate Window
Code:
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEventBreak
Debug.Print "Break Received"
Case comEventDCB
Debug.Print "Unexpected error retrieving Device Control Block"
Case comEventFrame
Debug.Print "Framing Error"
Case comEventOverrun
Debug.Print "Overrun"
Case comEventTxFull
Debug.Print "Transmit Buffer Full"
Case comEventRxOver
Debug.Print "Receive Buffer Overflow"
Case comEventRxParity
Debug.Print "Receive Parity Error"
'
' The following are only necessary if Hardware handshaking
' is being used
'
Case comEventCDTO
Debug.Print "CD Timeout"
Case comEventCTSTO
Debug.Print "CTS Timeout"
Case comEventDSRTO
Debug.Print "DSR Timeout"
End Select
End Sub
EDIT: I haven't looked at their Comm Tool but the instructions are quite clear as to what has to be sent.
The Command consists of 5 bytes.
1st Byte is "x"
2nd Byte is a value representing the state of relays 1 to 8.
3rd Byte is a value representing the state of relays 9 to 16
4th Byte is "/"
5th Byte is "/"
In the second and third bytes, each bit represents one relay, if the bit is set (ie = 1) the relay is to be turned on, if it's not set (ie = 0) the relay is to be turned off.
In the second byte the most significant bit (MSB, aka 'Leftmost') represents relay 1, the next bit represents relay 2, the next, relay 3, and so on. In the third byte, the MSB represents relay 9, the next bit represents relay 10, the next, relay 11, and so on.
When you execute the cmdSequential_Click subroutine, it is building bytes 2 and 3 according to the OptionButton settings.
The 'For I' loop is selecting which byte to process and the 'For J' loop is checking each option in turn and setting the appropriate bit if 'On' has been selected.
Thus, if you wanted relays 3,6,7,10 and 12 on:
In the second byte you'd set the 3rd, 6th and 7th bits which would be binary 00100110 = Hex 26 = Decimal 38 = ASCII Character '&'
In the third byte you'd set the 2nd and 4th bits which would be binary 01010000= Hex 50 = Decimal 80 = ASCII Character 'P'
So you'd send: x&P// to the device and it should turn relays 3,6,7,10 and 12 on, and all the others off.
(In your post above, it looks as if, when you're trying to send x1a05// you're sending 7 bytes which is confusing the device.)
Since values above 127 (Decimal) are in the extented ASCII Table, it may be difficult to enter the appropriate ASCII Character representations for those values from the keyboard, (and those less than 32 Decimal) so I suspect the Comm Tool will have some options / methods which allow you to specify the Character or Binary or Hex or Decimal or even Octal values, which it then converts to the bit values required and sends to the device.
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Ahhh bytes, so you send ASCII symbol.
Tat makes sence now, I knew I just was not understanding something simple.
I will check this out and give it a try.
Thanx so much!
Edit ad: Though you code did not have the funask, the relays also did not turn on( there are leds for then and you can here them)
Most likely something I had done, I will recheck it as well.
Edit 2: x&P// turned on 3,6,7,10,12 and the rest off! Thank you!
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
I am starting to get it, see what you neam
I need to convert byts(0) & byts(1) to Ascii
ried to convert to hex first but get invalid operation, I will keep trying though, I am almost there!
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
ok, this seems to be working, still need to test a bit more.
A few Items I just Dim'd did not "Dim as" because I am not sure what to dim them as?
Dim B1 ?
Dim B2 ?
Dim num as ?
Dim Value as ?
Sub
Code:
Private Sub cmdSequential_Click()
Dim Command As String
Dim byts(1) As Byte
Dim intI As Integer
Dim intJ As Integer
Dim intK As Integer
Dim B1
Dim B2
'Assumes that odd numbered Options = on
intK = 1
For intI = 0 To 1
For intJ = 7 To 0 Step -1
'Set the appropriate bit if the relay is to be turned on
If OpRelay(intK).Value = True Then
byts(intI) = byts(intI) Or 2 ^ intJ
End If
intK = intK + 2
Next intJ
Next intI
B1 = HexByte2Char(byts(0))
B2 = HexByte2Char(byts(1))
B1 = hex2ascii(B1)
B2 = hex2ascii(B2)
MsgBox B1
MsgBox B2
Command = "x" & B1 & B2 & "//"
MsgBox Command
MSComm1.Output = Command
End Sub
Functions
Code:
Public Function hex2ascii(ByVal hextext As String) As String
Dim y As Integer
Dim num
Dim Value
For y = 1 To Len(hextext)
num = Mid(hextext, y, 2)
Value = Value & Chr(Val("&h" & num))
y = y + 1
Next y
hex2ascii = Value
End Function
Public Function HexByte2Char(ByVal Value As Byte) As String
' Return a byte value as a two-digit hex string.
HexByte2Char = IIf(Value < &H10, "0", "") & Hex$(Value)
End Function
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
You shouldn't need to convert byts(0) or byts(1) since they contain the correct bit pattern that the device expects.
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Seems perfect!
Thanx SO MUCH
I only have one bug to work out when sending command manually and the funAsk
FirstByte = Asc(Mid(MyStrg, 1, 1)) ' error 5 invalid procedure or call
Small bug I imagine, something for tomorrow!!!
Thanx again!
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
You know, I'm being / am particularly dense / stupid in trying to concatinate a Byte into a String, must be my age or something. :o
All you needed to do is just change
Code:
Command = "x" & byts(0) & byts(1) & "//"
to
Code:
Command = "x" & Chr(byts(0)) & Chr(byts(1)) & "//"
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Quote:
Originally Posted by
planethax
I only have one bug to work out when sending command manually and the funAsk
FirstByte = Asc(Mid(MyStrg, 1, 1)) ' error 5 invalid procedure or call
The implication of that error is that MyStrg is empty (has a length of zero), which is surprising since it appears that the comEvReceive event has been recognised,there should be at least 1 byte available and you've read it. I suspect it's the dreaded 'DoEvents' in the attente subroutine which you're calling after recognising the comEvReceive event but befoe processing the data. The cause is probably also due to the OnComm event code I suggested you added, being re-entered whilst already active, which can lead to unpredictable results, so perhaps you should remove it.
This is one of the reasons I prefer an asynchronous approach rather than timing, you don't need DoEvents at all, also, although it's highly unlikely to the point of being impossible in your application, theoretically, you could miss a response if the device takes longer than 300 mS to respond, also you are adding a 300mS processing delay, all of which might not be necessary.
However the pros and cons can be discussed forever, there's always trade-offs and one man's meat is another man's poison :)
Whatever, with the approach you've taken, I think you should wait and then check for the comEvReceive. I'd move the call to Attente and 'tidy up' the code a little:
Code:
Private Function funAsk()
Dim I As Integer
Dim FirstByte As Byte
Dim SecondByte As Byte
Dim intRelay As Integer
Dim Relays(15) As Boolean
MSComm1.InBufferCount = 0
MSComm1.Output = "ask//"
'
' Wait for the device to respond
'
Attente (300)
'
' Anything to receive ?
'
If (MSComm1.CommEvent = comEvReceive) Then
'
' Yes - Process it
'
MyStrg = MSComm1.Input
FirstByte = Asc(Mid(MyStrg, 1, 1))
SecondByte = Asc(Mid(MyStrg, 2, 1))
For I = 7 To 0 Step -1
Relays(intRelay) = FirstByte And 2 ^ I
intRelay = intRelay + 1
Next I
For I = 7 To 0 Step -1
Relays(intRelay) = SecondByte And 2 ^ I
intRelay = intRelay + 1
Next I
For I = 0 To 15
If CStr(Relays(I)) = False Then
fStatus(I).BackColor = &HFF&
Else
fStatus(I).BackColor = &HC000&
End If
Next I
End If
End Function
(I've indented it to make it more 'readable')
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
So, this is just a code fragment (what I call, "air code").
To set a relay state, you have to follow the protocol rules. The first rule is that you must send a bit-mapped command to the relay board that changes only the relays that you want to change and leave all others in the state that they were before the command. This means that you must execute the change in three (or four) steps.
1. Send an "ASK" command that gets the current relay states. In my code fragment, I call these two bytes n1 and n2, where previously we I called them FirstByte and SecondByte.
2. Change only the bit for the relay that we are currently changing, while keeping the bits for all other relays unchanged.
3. Send the relay change command.
4. If you want to do this -- send another "ASK" command to verify the change, and to update the UI.
Note, again, I have used control arrays, this time for the option buttons. I added the On/Off option buttons to the status Frame controls (the Index of which is the same as the Index for the On/Off option buttions). Note, also, the UI intialization code, and the bit-mapped On/Off commands, which I encoded in an array.
Code:
Dim RelayOnValL(7) As Byte
Dim RelayOffValL(7) As Byte
Dim RelayOnValH(7) As Byte
Dim RelayOffValH(7) As Byte
Private Sub Form_Load()
For I = 7 To 0 Step -1
RelayOnValL(I) = 2 ^ I
RelayOffValL(I) = &HFF Xor (2 ^ I)
RelayOnValH(I) = 2 ^ I
RelayOffValH(I) = &HFF Xor (2 ^ I)
Next I
For I = 0 To 15
'set the initial state of all option buttons to "Off"
RelayOff(I).Value = True
'presumably, this is the power on state?
Next I
End Sub
Private Sub RelayOff_Click(Index As Integer)
If MSComm1.PortOpen = True Then
Dim n1 As Byte
Dim n2 As Byte
MSComm1.Output = "ask//"
Attente (300)
Dim myStrg As String
myStrig = MSComm1.Input
n1 = Asc(Mid(myStrg, 1, 1))
n2 = Asc(Mid(myStrig, 2, 1))
'To turn on a relay, send the command:
'"x" & Chr(n1) & Chr(n2) & "//"
'where n1 and n2 are first read from the board
'using the ASK command,
'Then, create NEW n1 and n2 values:
If Index < 8 Then
n1 = n1 And RelayOffValL(Index)
Else
n2 = n2 And RelayOffValH(Index)
End If
'n1 and n2 to the result
'the block above resets just a single bit to reflect the change
MSComm1.Output = "x" & Chr(n1) & Chr(n2) & "//"
End If
End Sub
Private Sub RelayOn_Click(Index As Integer)
If MSComm1.PortOpen = True Then
Dim n1 As Byte
Dim n2 As Byte
MSComm1.Output = "ask//"
Attente (300)
Dim myStrg As String
myStrig = MSComm1.Input
n1 = Asc(Mid(myStrg, 1, 1))
n2 = Asc(Mid(myStrig, 2, 1))
'To turn on a relay, send the command:
'"x" & Chr(n1) & Chr(n2) & "//"
'where n1 and n2 are first read from the board
'using the ASK command,
'Then, create NEW n1 and n2 values:
If Index < 8 Then
n1 = n1 Or RelayOnValL(Index)
Else
n2 = n2 Or RelayOnValH(Index)
End If
'the block above sets just a single bit to reflect the change
MSComm1.Output = "x" & Chr(n1) & Chr(n2) & "//"
End If
End Sub
Realize that this code isn't perfect (and I haven't tested it, since I do not have your hardware). There are several places where it could be optimized, and there is no error handling. I've tried to make clear what I've done, but you need to study the subtle point of using the Or function to set an individual bit, and the And function to clear an individual bit.
Dick
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
To implement Dick's suggestion into your existiing code you need to add a couple of lines into funAsk where you are setting the colour of the fStatus control array to reflect the current status.
Code:
For I = 0 To 15
If CStr(Relays(I)) = False Then
fStatus(I).BackColor = &HFF&
OpRelay(2 * I).Value = True
Else
fStatus(I).BackColor = &HC000&
OpRelay((2 * I) + 1).Value = True
End If
Next I
Since you're always calling funAsk after any change, this ensures that the Option Buttons always reflect the current state of the relays so those that you haven't changed when you click on 'Send Sequentially' will retain their existing state. This allows you to follow the protocol, described in Dick's post, in one step, without the need to repeatedly interrogate the device.
EDIT: BTW the above changes removes the need for the set of cmdRelayxx buttons since you can change the state of any single relay, without affecting the others, using the Option Buttons and 'Send Sequentially'
However, as Dick states, it's important to get a grasp of the Logical Operators: And, Or and Xor, for manipulating bits within bytes, especially if you want to do more complex things, like setting up an automated timed sequence. Take a good look at Dick's code, especially (IMHO) the '&HFF Xor (2 ^ I)' for generating a Mask which is used to turn a single bit off.
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Quote:
Originally Posted by
planethax in Post #6
This should be a simple 5 minute app lol
I just had a little giggle to myself; here we are 3 days and 67 posts later, still developing it. :):):)
Let's hope that the light at the end of the tunnel is not an oncoming Train !
-
Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Sorry I neglected thread, got tied up with other projects.
I have not tried the last code bits yet, but have saved them and will work with them.
I must give a HUGE thanx to you both, I have learned a lot!
At this point I am happy with how this is working.
Honestly, I do not even need this particular app directly for my app.
This was just an excercise to learn how to work with relay board and now have reference and C/P available for using it in my main project.
BTW, I have managed to get the relays to Fire/Check/Report via Voice commands!!!!!
Needs lots of tweeking and testing but hope to have full Project done by mid March!
Thank you again!!!
I am marking this thread Resolved++++++
If I have further questions I will create a new thread as it shouldn't be related to this threads title.
Quote:
Originally Posted by
Doogle
I just had a little giggle to myself; here we are 3 days and 67 posts later, still developing it. :):):)
Let's hope that the light at the end of the tunnel is not an oncoming Train !
Haha, most a knowledgeable coder should only have taken 5 minutes :D
Here is a quick clip of controlling relays via voice.
http://www.youtube.com/watch?v=K-uG7JmJpvk
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Oh i'm bringing this back from the dead :)
I have the 4 port relay version and i seem to not be able to get it to work with the current mycontrol4.zip code posted on this topic.
I've tried emailing/PMing the OP (planethax) but have not had any reply back. Could anyone be willing to help me out in figuring out why its not working for me but works for him? To my understanding all the relays use the same chip - just different relay count.
David
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Are you sure you're using the right Comm Port ?
(BTW you should really start your own thread rather than hi-jack someone else's)
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
I have been away.
Let me dig up old source (new is now integrated into complete app)
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Quote:
Originally Posted by
planethax
I have been away.
Let me dig up old source (new is now integrated into complete app)
I appreciate it, planethax :)
David
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Any update on finding that code, planethax?
David
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
The source is on my Network backup, which decided to crap out.
I am waiting for a new power adapter to arrive in mail.
If you are in a rush, email the seller of the boards, I sent him the source code so he could give it out (or link to) when selling them.
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Quote:
Originally Posted by
planethax
The source is on my Network backup, which decided to crap out.
I am waiting for a new power adapter to arrive in mail.
If you are in a rush, email the seller of the boards, I sent him the source code so he could give it out (or link to) when selling them.
Already have. I don't think he wants to share the source because i've asked that and he hasn't replied back since.
David
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Quote:
Originally Posted by
Stealthrt
Already have. I don't think he wants to share the source because i've asked that and he hasn't replied back since.
David
That is odd as I gave him the source so he could give it away to help his sales.
PM me the seller (maybe not same guy)
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Could you please post your current source for this, planethax, sence you have not replayed to my emails about it. Would be really nice to get my 4 port relay board working...
Thanks.
David
-
1 Attachment(s)
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
This may help with the array bit
-
Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled
Hi,
I am new to the forum and I got here because I just bought a 4 Port USB relay just like Stealthrt... I am not a professional coder but I was not expecting such a difficulty to make this work with VB6 :(
This thread is a bit old but i hope that planethax can share his latest source code.
Anyway, thank you all in advance for any help!