Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have this "small" program.
HAve made a lot og buffer to send different command over my serialport.
But when i need to send ex time and date. and need som variables in my buffer.
Code
VB
Dim buffer24 As Byte() = {&H0, &H20, &H4, &HFF} 
' is ex the code for something that reciever can decode,
'Works great,
SerialPort1.Write(buffer24, 0, 4) ' send the line

'But i now need to send time and date. and that changes ;)
NewTime = Now.ToString("HH") ' TEST
 bufferTime = {&H0, &H0, NewTime,"mm", "dd","MM","yy", &HFF} 'TEST
 SerialPort1.Write(bufferTime, 0, 8)
'Format i need to send is 08 00 "hour" "Minut" "day" "Month" "year 2 digits" FF 

First block = 08 = Numbers of bytes.
Second Block= 00 = Group number in reciever.
3. Block = Hour
4. Bloxk = Minut
5. Block = Day
6. Block = Month
7. Block = Year in 2 digits.
8. Block = End command FF

What I have tried:

Need help to be able to add the variables to buffer,
Can add it, but it is in normal integer, need HEX.
ex 14 is 0E and NOT 14 to be send.
Posted
Updated 6-Nov-22 4:19am

Cast it from an integer to a byte and it'll be in hex, effectively.
Numbers aren't stored in decimal, they are stored in binary - so just casting your integers to byte values will mean you can insert them directly into an array of bytes and "get the hex version".

All bases other than binary are just sugar coating when the binary value is converter to a string for display. If you aren't displaying it, you don't need a string at all"
 
Share this answer
 
NyTime = Hex(Now.ToString("HH"))
            NyMinut = Hex(Now.ToString("mm"))
            NyDato = Hex(Now.ToString("dd"))
            NyMoned = Hex(Now.ToString("MM"))
            NyYear = Hex(Now.ToString("yy"))


            bufferTime = {&H0, &H0, CInt("&H" & NyTime), CInt("&H" & NyMinut), CInt("&H" & NyDato), CInt("&H" & NyMoned), CInt("&H" & NyYear), &HFF} 
            SerialPort1.Write(bufferTime, 0, 7)


This works,
 
Share this answer
 
Comments
PIEBALDconsult 6-Nov-22 11:15am    
Please don't try to answer your own question. Use the Improve question button to add detail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900