Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String)
Serialport1.write (Hex not String) Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Go Back  Xtreme Visual Basic Talk > > > Serialport1.write (Hex not String)


Reply
 
Thread Tools Display Modes
  #1  
Old 07-24-2006, 04:32 AM
ukjock ukjock is offline
Regular
 
Join Date: May 2003
Location: Scotland
Posts: 57
Unhappy Serialport1.write (Hex not String)


I have a string which contain some hex that I need to output, to be a bit more specific its the following:

"00 FF FF 00 0B 12 FF 01 30 31 03 EF B0 EF A2 48 45 4C 4C 4F 20 57 4F 52 4C FF"

when i try to send it out using the following code

Code:
Serialport1.write ("00 FF FF 00 0B 12 FF 01 30 31 03 EF B0 EF A2 48 45 4C 4C 4F 20 57 4F 52 4C FF")
it outputs as string and not as Hex values. does anyone have a quick and easy solution to outputting as Hex?

Kind Regards

Chris
Reply With Quote
  #2  
Old 07-24-2006, 05:53 AM
mdent mdent is offline
Centurion
 
Join Date: Jun 2006
Location: Teesside, UK
Posts: 108
Default

serial communication.....send/recieive

The above thread shows how to do it in VB.NET, but the same principle applies.

You need to send as a byte array. For this, you need to do:

Code:
Dim myHex() As Byte = New Byte() {&H00, &FF, &H00} ' etc....
myComPort.Write(myHex)
Hope that helps.
Reply With Quote
  #3  
Old 07-25-2006, 07:37 AM
866JR 866JR is offline
Regular
 
Join Date: Sep 2002
Location: Guntown, Mississippi
Posts: 87
Default

Hex is generally ascii string data (0-F). One Hex character represents 1/2 of a binary byte, so you often see them as pairs representing a full binary byte. You may actually want to send binary data as 0-15 twice. However there is really no reason to do this because if you are sending binary data you don't need to represent it in hex because it will just take up more space. For example two hex data characters will give a binary range of 0-255, this is the same as one binary byte and you will need to send two if you send them as hex. So if you are attempting to send to a manufactured device in hex I would suggest that it is actually ascii data. Try removing the spaces between the hex pairs.

Good luck, Orwill
Reply With Quote
  #4  
Old 07-25-2006, 09:28 PM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

Hi,

The Data you have is in string form. If you try to send "00 FF FF" for example, what will be sent is Hex(3030204646204646) I don't think that's what you want to do. I suspect you actually want to send Hex(00FFFF). You will need to convert each pair of hex characters in your string to their binary equivalent.
There are loads and loads of ways to convert (everyone has their own 'favourate' way), here's an example:
Code:
Private Function ConvertStringToBinary(strHex As String) As String Dim strBytes() As String Dim strNibble As String Dim intI As Integer Dim intJ As Integer Dim intNibbleValue As Integer Dim intValue As Integer ' ' Assumes strHex contains Hex data represented in pairs of characters, ' each pair of hex characters being seperated by a single space. ' eg "FF 6B 7C" ' ' Isolate each Hex pair ' strBytes() = Split(strHex, " ") ' ' Calculate the Value of each pair ' For intI = LBound(strBytes) To UBound(strBytes) For intJ = 1 To 2 strNibble = UCase(Mid(strBytes(intI), intJ, 1)) If Not IsNumeric(strNibble) Then ' ' Asc ("A") = 65 so intNibbleValue would be 65 - 55 = 10 ' Asc ("B") = 66 so intNibbleValue would be 66 - 55 = 11 ' etc ' intNibbleValue = Asc(strNibble) - 55 Else intNibbleValue = CInt(strNibble) End If intValue = intValue + (intNibbleValue * (16 ^ (2 - intJ))) Next intJ ' ' Add this character to the output ' ConvertStringToBinary = ConvertStringToBinary & Chr(intValue) intValue = 0 Next intI End Function
To test it:
Code:
Private Sub Form_Load() Dim strHex As String Dim strChars As String Dim intI As Integer strHex = "00 FF FF 00 0B 12 FF 01 30 31 03 " strHex = strHex & "EF B0 EF A2 48 45 4C 4C " strHex = strHex & "4F 20 57 4F 52 4C FF" strChars = ConvertStringToBinary(strHex) Debug.Print "Original String: "; strHex Debug.Print "Binary String: "; For intI = 1 To Len(strChars) Debug.Print Format(Hex(Asc((Mid(strChars, intI, 1)))), "00"); Next intI Debug.Print End Sub
Regards
Doug
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String) Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String) Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
 
Serialport1.write (Hex not String)
Serialport1.write (Hex not String)
 
-->