Trouble converting to byte
Trouble converting to byte
Trouble converting to byte
Trouble converting to byte
Trouble converting to byte
Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte
Trouble converting to byte Trouble converting to byte
Trouble converting to byte
Go Back  Xtreme Visual Basic Talk > > > Trouble converting to byte


Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2010, 11:18 PM
bonedoc bonedoc is offline
Junior Contributor
 
Join Date: Mar 2006
Posts: 376
Default Trouble converting to byte


I am sending data to some microchips with a serial port control using:

Code:
dim data as byte = convert.tobyte(textbox1.text)
serialport1.open
serialport1.write(data)
serialport1.close
well, it work up to the value 9, but anything with more that one decimal place sends a byte value of the rightmost digit. So, 16 in the text box will send 6. Basically, it appears to be sending just 6, or 1 followed by 6. Is this how the serial port sends data...one digital at a time? Or, is the conversion to a byte failing?
Reply With Quote
  #2  
Old 06-28-2010, 11:31 PM
bonedoc bonedoc is offline
Junior Contributor
 
Join Date: Mar 2006
Posts: 376
Default

Ok, I slowed the rate down on the serial port and chips and is appears that any number is broken into seperate digits and sending them each as a 8 bits. So, 255 is sent as

00000010 00000101 00000010

and not

11111111

can someone tell me how to send the value 11111111 from decimal 255 so this does not happen. Sorry, did not mean to out a serial question here, if it is.
Reply With Quote
  #3  
Old 06-29-2010, 04:15 AM
PlausiblyDamp's Avatar
PlausiblyDampTrouble converting to byte PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Try adding Option Strict On to the top of the file and fix any errors it raises, I have a feeling (i.e. I read the documentation at http://msdn.microsoft.com/en-us/libr...t_members.aspx) that the
Code:
serialport1.Write(data)
isn't doing what you think it is.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #4  
Old 06-29-2010, 06:44 AM
bonedoc bonedoc is offline
Junior Contributor
 
Join Date: Mar 2006
Posts: 376
Default

I did that and used serialport1.write(CStr(data)) but no luck....hmmm
Reply With Quote
  #5  
Old 06-29-2010, 06:58 AM
PlausiblyDamp's Avatar
PlausiblyDampTrouble converting to byte PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Think about what you are doing, what is the string representation of a byte larger than a single digit going to be...

If you have a string "16" then the documentation (you did read the documentation didn't you) states it will be sent as a series of characters - this is exactly the problem you are having. You will need to use one of the other overloads (http://msdn.microsoft.com/en-us/library/5109k29x.aspx) instead.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #6  
Old 06-29-2010, 08:47 AM
bonedoc bonedoc is offline
Junior Contributor
 
Join Date: Mar 2006
Posts: 376
Default

It doesn't sound like i read it, but I did. Even tried it. I am confused why the overload would work. If I use a buffer, won't it just create a buffer that turns 255 into 3 bytes instead of 11111111? I need another clue.
Reply With Quote
  #7  
Old 06-29-2010, 09:24 AM
PlausiblyDamp's Avatar
PlausiblyDampTrouble converting to byte PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you pass a string it will send (by default) one byte per ascii character in the string. Passing a string that holds the value "16" will result in two bytes being sent, one for each ascii character.

You need one of the other two overloads in the link I posted, given only one of the two remaining overloads deals in bytes I thought it was a fairly obvious choice. If the method is listed as taking a buffer of bytes why on earth would it then be expected to convert them to a string and transmit them? The overload for a string is provided as a convenient way of working with strings

http://msdn.microsoft.com/en-us/library/ms143551.aspx
Code:
serialport1.Write(new byte() {16},0,1)
should write the single byte 16 to the serial port, if you are passing multiple bytes . the data is in a byte array already you will need to adjust the parameters accordingly.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
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
Trouble converting to byte
Trouble converting to byte
Trouble converting to byte Trouble converting to byte
Trouble converting to byte
Trouble converting to byte
Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte Trouble converting to byte
Trouble converting to byte
Trouble converting to byte
 
Trouble converting to byte
Trouble converting to byte
 
-->