By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

Convert number to hex in byte array (&H?? format)

P: 2
Hello!

I'm running Visual Basic 2012

I'm having a certain amount of sucess controlling my serial port but now I've hit a wall.

Expand|Select|Wrap|Line Numbers
  1.  Public com3 As New SerialPort("COM3", 9600, Parity.None, 8, StopBits.One)
  2.  
  3.     Public Hexe As Integer = Hex(100)
  4.  
  5.       Public Sub New()
  6.         InitializeComponent()
  7.         For Each sp As String In My.Computer.Ports.SerialPortNames
  8.             ListBox1.Items.Add(sp)
  9.         Next
  10.         If com3.IsOpen = False Then
  11.             com3.Open()
  12.         End If
  13.  
  14.             Dim Absolut() As Byte = {1, 4, 0, 0, 0, 0, 0, Int in Hex (&H format), Int in Hex + 5 (control bit)}
  15.             com3.Write(Absolut, 0, Absolut.Length)
  16.     End Sub
  17.  
As you can see I need either the whole byte in hex or just parts of it, problem is that Hex(Int) gives it as a string instead of the &H?? format I need. I can't find any solution on MSDN.

Am I just not seeing the obvious?

Best Regards
SciCat

EDIT: The code is incomplete, I didn't want to dump a 100+ line blob, the code works in its entirety.
Feb 25 '13 #1
Share this Question
Share on Google+
1 Reply


P: 2
I have managed to solve the conversion problem using Bitshift in the byte itself using an integer:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Public com3 As New SerialPort("COM3", 9600, Parity.None, 8, StopBits.One)
  3.  
  4.     Public Sub SendCMD(Adress As Byte, Command As Byte, Type As Byte, Motor As Byte, Value As Integer)
  5.         Dim TxBuffer() As Byte
  6.         Dim i As Integer
  7.  
  8.         TxBuffer = {Adress, Command, Type, Motor, Value >> 24, Value >> 16, Value >> 8, Value & &HFF, 0}
  9.         For i = 0 To 7
  10.             TxBuffer(8) = TxBuffer(8) + TxBuffer(i)
  11.         Next
  12.         com3.Write(TxBuffer, 0, 9)
  13.  
  14.  
  15.     End Sub
  16.  
  17.  
  18.     Public Sub New()
  19.         InitializeComponent()
  20.         For Each sp As String In My.Computer.Ports.SerialPortNames
  21.             ListBox1.Items.Add(sp)
  22.         Next
  23.         If com3.IsOpen = False Then
  24.             com3.Open()
  25.         End If
  26.         SendCMD(1, 1, 0, 0, 1)
  27.         Tid.Enabled = True
  28.  
  29.     End Sub
  30.  
  31.     Private Sub Tid_Tick(sender As Object, e As EventArgs) Handles Tid.Tick
  32.         SendCMD(1, 3, 0, 0, 1)
  33.     End Sub
But when I run the code I get an arithmetic overflow, did i do something wrong?


EDIT: Everything works! Keeping it here for future reference.
Mar 2 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.