|
-
Jul 30th, 2005, 10:21 AM
#1
Thread Starter
New Member
Hex problems
Hi..
I'm having some trouble trying to send some information through to a controller connected to my serial port.
I'm trying to send an FF but its being received as a 3F
Heres how I've been sending it
Serialport1.Output(Chr(&HFF))
If I dont send it with the Chr() it comes out as 32 35 35 which is no good as my controller wont reply if its not in the format of FF
Any help anyone can give me would be greatly appreciated.
-
Jul 30th, 2005, 01:34 PM
#2
Thread Starter
New Member
Re: Hex problems
I tried rewriting the code and using a different serial port ocx.. heres what i have
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With SerialConnection1
.BaudRate = 9600
.PortName = "COM1"
.Open()
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialConnection1.Write(Chr(255))
End Sub
My port sniffer is still coming up with a 3F though 
Not sure if this helps, but I did a search on google to see if anyone else had the same trouble as me, and came across this post http://www.vbip.com/forum/topic.asp?id=4428
Last edited by torpkevuk; Jul 30th, 2005 at 01:38 PM.
-
Aug 5th, 2005, 10:22 AM
#3
Re: Hex problems
How is SerialConnection1 declared? Is the paramater of the .Write method typed as a String?
-
Aug 25th, 2005, 02:17 AM
#4
New Member
Re: Hex problems
 Originally Posted by penagate
How is SerialConnection1 declared? Is the paramater of the .Write method typed as a String?
Hi There,
Just having the exact same problem.
Here's the code, very simple!:
---------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SerialPort1.IsOpen = False Then
SerialPort1.Open()
End If
SerialPort1.Write(Chr(255))
-------------------------
The output gives in the port-monitor a "?" and hex-code 3F.
I need it to give the hex-stringoutput " AA AA 09 "
I found the vbTAB to give me the "09" part, similar to the dec.code for TAB.
Now the "AA" should be dec.170 similar to ª.
I've been dangling with this crap for two weeks now, trying so many combinations and so, only having found the "09".. 
If anyone could have an idea of solving this - still simple problem, it'd be much appreciated.
By the way, I'm using vb 2005 B2
-
Aug 25th, 2005, 03:56 AM
#5
Re: Hex problems
I don't the .NET equivalent for this function offhand but in VB I would use StrConv, since VB strings are Unicode and you look like you are sending ANSI.
VB Code:
SerialPort1.Write(StrConv(Chr$(&HAA) & Chr$(&HAA) & Chr$(&H09), vbFromUnicode))
-
Aug 25th, 2005, 05:45 AM
#6
New Member
Re: Hex problems
 Originally Posted by penagate
I don't the .NET equivalent for this function offhand but in VB I would use StrConv, since VB strings are Unicode and you look like you are sending ANSI.
VB Code:
SerialPort1.Write(StrConv(Chr(&HAA) & Chr(&HAA) & Chr(&H09), vbFromUnicode))
It seems that the "vbFromUnicode" does not apply to the .net...
It only has types like lowercase, uppercase and so..
Any ideas? It seems to be totally impossible to find anything or anyone with similar problems.
-
Aug 25th, 2005, 05:50 AM
#7
Re: Hex problems
You gotta know where to look 
According to KB 311338:
VB Code:
Bytes = System.Text.Encoding.GetEncoding(1252).GetBytes(SomeString)
Now, what does the .Write method accept as a parameter?
-
Aug 25th, 2005, 06:06 AM
#8
New Member
Re: Hex problems
It accepts .Write(buffer() As Byte, offset As integer, Count As Integer)
buffer: The byte array to which the output is written.
and ofcourse also a string..
-
Aug 25th, 2005, 06:10 AM
#9
Re: Hex problems
OK, in that case it's much easier. Just chuck the whole load in a byte array.
VB Code:
' I need to learn how to initialise arrays in VB.NET :D
Dim chBuf(2) As Byte
chBuf(0) = &HAA
chBuf(1) = &HAA
chBuf(2) = &H09
SerialPort1.Write(chBuf, 0 , 3)
assuming that Count = length of transmission in bytes.
You may also need to add a terminating null character.
-
Aug 25th, 2005, 06:19 AM
#10
New Member
Re: Hex problems
 Originally Posted by penagate
OK, in that case it's much easier. Just chuck the whole load in a byte array.
VB Code:
' I need to learn how to initialise arrays in VB.NET :D
Dim chBuf(2) As Byte
chBuf(0) = &HAA
chBuf(1) = &HAA
chBuf(2) = &H09
SerialPort1.Write(chBuf, 0 , 3)
assuming that Count = length of transmission in bytes.
You may also need to add a terminating null character.
WOW, it's that simple.! Now, i'm quite new to coding, so I guess I gotta learn to translate those - to me - cryptic help popups..! 
Now what are you saying about that term. null char?
-
Aug 25th, 2005, 06:23 AM
#11
Re: Hex problems
A null character is just a zero 
Strings in pretty much all languages are null-terminated. Because they are (usually) variable in length, the null character is appended to signify the end of the string data. Otherwise, whatever function it was would keep on reading memory (thinking it was string data) until it chucked an access violation 
I'm not sure whether you need to send terminating null characters in serial transmissions (I suspect you do), just in case here's how you do it.
VB Code:
' Still haven't found out how to initialise an array properly :)
Dim chBuf(3) As Byte
chBuf(0) = &HAA
chBuf(1) = &HAA
chBuf(2) = &H09
chBuf(3) = 0
' and then, the length is just increased by 1 byte
SerialPort1.Write(chBuf, 0, 4)
-
Aug 25th, 2005, 06:28 AM
#12
Re: Hex problems
w00t, you learn something every day 
VB Code:
Dim chBuf(3) As Byte = {&HAA, &HAA, &H09, 0}
SerialPort1.Write(chBuf, 0, 4)
-
Aug 25th, 2005, 06:47 AM
#13
New Member
Re: Hex problems
 Originally Posted by penagate
w00t, you learn something every day
VB Code:
Dim chBuf(3) As Byte = {&HAA, &HAA, &H09, 0}
SerialPort1.Write(chBuf, 0, 4)
Man... you are sooo kind!! This is really appreciated!!!
What i'm using this for is a I/O interface card board thingy and it was sold with a sample software, but no source code.
Well.. the last thing i think i need is something like a "enter" or something.
If I paste the ª ª "tab" sequence in hyperterminal the unit responds with the status of the input lines. My port-monitor shows AA AA 09 and then makes a new line.
wonder what the difference from this is...
-
Aug 25th, 2005, 06:54 AM
#14
Re: Hex problems
How to do a new line depends on the platform. On Windows and DOS it is CR plus LF (carriage return and line feed) and that is character codes 13 and 10 respectively. However on some platforms you can get away with just sending the carriage return character.
Try these:
VB Code:
Dim chBuf(5) As Byte = {&HAA, &HAA, &H09, 10, 13, 0}
' or
Dim chBuf(4) As Byte = {&HAA, &HAA, &H09, 13, 0}
and remember that the Count parameter takes the UBound of the array + 1, because the array is zero based
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|