realolman Posted February 22, 2005 Posted February 22, 2005 I got a serial comm component from Sax.comm in a VB resource package from Microsoft. Part of it is to write (only ) to a port. It requires a form with a textbox and a "send" button. It should write what is in the text box to the port problem is it doesn't work. This is all the code on the "send" button click event: ...... Try If Not SerialConnection1.IsOpen Then SerialConnection1.Open() SerialConnection1.Write(TextBox1.Text + vbCr) Catch ex As Exception MsgBox(ex.Message) End Try ....... It causes Build errors: Argument not specified for parameter 'count' of 'Public Overrides Function Write(buffer() As Byte, offset As Integer, count As Integer) As Integer'. Argument not specified for parameter 'offset' of 'Public Overrides Function Write(buffer() As Byte, offset As Integer, count As Integer) As Integer'. Value of type 'String' cannot be converted to '1-dimensional array of Byte'. ..... This is the write function: [Visual Basic]MustOverride Public Function Write( _ ByVal buffer As Byte, _ ByVal offset As Integer, _ ByVal count As Integer _ ) As Integer I can put two numbers in, after the "TextBox1.Text + vbCr" and it makes the two "argument not specified" errors go away ( it still might not work, I don't know, but the errors go away) But, I don't get the "Value of type string cannot be converted to byte" thing. Should it be trying to send TextBox.text one character at a time? What do you suppose it's problem is? This is code from Sax.comm, not from me. Why wouldn't it work? It's such a small potatoes thing. Any help would be greatly appreciated. thanks Quote
Diesel Posted February 22, 2005 Posted February 22, 2005 small potatoes? You are really old. SerialConnection1.Write(System.Text.Encoding.ASCII.GetBytes(TextBox1.Text + vbCr), 0, TextBox1.Text.Length+1) Quote
realolman Posted February 22, 2005 Author Posted February 22, 2005 I have not hooked this up to anything,yet, but it seems happy. thank you. If you wouldn't mind, would you please explain the System .text.encoding.ASCII.GetBytes stuff. I think I get the rest. I am new to .net and I don't seem to get it . How did you know what was missing? Why would a simple tutorial be so poorly done? It's not even close. thanks and yes.. I am old... thus the name Quote
Leaders snarfblam Posted February 22, 2005 Leaders Posted February 22, 2005 You need to include any listed parameters in a function call unless they are explicitly defined optional [they will appear in square brackets in intellisense], in this case buffer, offset, and count, which means you will also usually need to understand the purpose of each parameter. More importantly you need to pay attention to types. The function is asking for something as type byte(), or an array of bytes, which a String is not. When you call a function you must pass the information (i.e. arguements, parameters, etc.) of the correct type. Although depending on some options VB will automatically convert a few types of data for you, for the most part you need to make these conversions explicitly. As you get more experienced with .Net you will learn how to convert data to and from more different types. ASCII is one form of encoding text (i.e. storing a binary representation of text in the computer's memory). The System.Text.Encoding.ASCII.GetBytes() function returns the raw binary data of your text using ASCII encoding (in the form of a byte array: Byte()). If any of that went over your head, don't sweat it. All you really need to understand is that System.Text.Encoding.ASCII.GetBytes() converts data from the type you have (String) to the type that SerialConnection1.Write() reqires (Byte()). Quote [sIGPIC]e[/sIGPIC]
realolman Posted February 22, 2005 Author Posted February 22, 2005 I cut and pasted the code from a tutorial I got in a resource kit from MS. I understand ASCII and types, I don't understand how you know there is a function available to do the conversion, what it is for, and why it should need to be used. Especially in an elementary tutorial. Quote
VBAHole22 Posted February 22, 2005 Posted February 22, 2005 I'm fairly certain that dealing with rsport communication is going to become a lot more simple in .NET framework 2.0 There will be a serial port class ingrained in the framework so we won't have to rely so much on win32 api dll whatever you want to call it. For some reason this was left out of the 1.0 framework. I sure hope it works as advertised Quote Wanna-Be C# Superstar
coldfusion244 Posted February 22, 2005 Posted February 22, 2005 While we are on the subject of communication to ports... Anyone know how to enumerate USB devices? sending data and calculating the CRC? Quote -Sean
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.