Basic help for dummies, Please!
I've just started my 16 lessons in Visual Basic Express 2005 and I'm learning my first programming since old GFA Basic.
But my goal doesn't seem to be part of the lesson plan, and that is to simply send "H1carriagereturn" to com2 at 4800 N81.
I've tried the code snippets, (probably don't know how to use them), and looked at examples of long serial port programs, (don't understand anything about them).
I've tried the simple example in the help file:
Sub SendSerialData(ByVal data As String)
' Send strings to a serial port.
Using com1 As IO.Ports.SerialPort = _
But simply copying and pasting it into my form1 window doesn't make it do anything.
I've also tried the get-serial-ports example:
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
ListBox1.Items.Add(sp)
Next
End Sub
I don't get any errors when I run it, but neither does it show anything in the list box that I drug over to my form1.
If someone could please send me the entire code to just send one simple "H1carriagereturn" to a comport, it might give me the incentive to learn more about coding in Visual Basic. It just seems very convoluted compared to the old Basic code. If I could just get this one little thing to work, it might start to actually be fun. I'm trying to turn on a communications receiver. If I can get that to work, I can build up some code from that point.
Thanks, Rick
Those methods will work, but you will need to call them. They won't do anything if you just paste the code.
For example call the GetSerialPortNames method at Form_Load.
Double Click on the Form and you will be presented a new subroutine as follows:
| | Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.LoadEnd Sub |
now simply call your subroutine within this method body. i.e. | | Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load GetSerialPortNames() End Sub |
Thanks to you both, Mick & samperiau, for trying to help me,
but I feel like I've been dumped into a foreign country, and don't speak a word of their language.I did call my subroutine snippet from the help example within the method body as you suggested, and here it is:
Public
Class Form1 Sub SendSerialData(ByVal data As String) ' Send strings to a serial port. Using com2 As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort("com2", 4800, IO.Ports.Parity.None) com2.WriteLine(
"H1" & vbCr) End Using End Sub End
ClassIt shows no errors at the bottom of the screen(and I think that means the syntax is OK), but when I run it, nothing happens. (No H1cr to com2) I've also tried samperiau's suggestions of adding:
SerialPort1.Encoding = System.Text.Encoding.Default
or
SerialPort1.Open()
SerialPort1.Write(H1 + Chr(13))
SerialPort1.Close()
But the syntax seems to be wrong for VB Express as I get a bunch of errors, most telling me the statements "SerialPort1" are not declared.The statement in my code:
My.Computer.Ports.OpenSerialPort("com2", 4800, IO.Ports.Parity.None)
I added the stuff between the parenthesis by clicking on a little pulldown menu that appeared when I cross the pointer over the first part of the statement. Probably all wrong, but you have to get the baud rate, etc., in there somewhere.
In my other line of code:
com2.WriteLine("H1" & vbCr)
Again, I'm not sure of the right syntax. In Basic code, I'd just say: H1$="H1"+CHR$13, then use H1$ in the parenthesis.
I agree, there's not much help out there on using the comports. And anything I do find, assumes that I know a whole lot more than I do.
Maybe if one of you guys could send me the whole code that I could paste into the form1 window. The simplest code you can come up with that will send something to com2, maybe I'd work for me. Other than that, somebody's going to have to come over here, knock me out of my chair, and show me!
>you have to get the baud rate, etc., in there somewhere.
Don't need to. For a simple start just use the serial port property windows and you can setup defaults from there. You really don't need much more than the above. I'm assuming that you have a serial port component loaded in you form.
I quite understand you feelings about the lingo...haven't used Basic in about 12 years, last app was under DOS 5 
Gentlemen, persistence pays off. I now have two buttons on the screen with message boxes that show when the buttons are pressed. One sends H1cr to com2, the other sends H0cr to com2. This toggles remote control on/off on a communications receiver. Now that I have this figured out, I can control a myriad of features via the comports.
Thanks for the help.
Here is my working code:
Public
Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using com2 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM2", 4800, 0, 8, 1)
com2.WriteLine("H1" + Chr(13))
End Using
MessageBox.Show(
"Receiver On-Line") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Using com2 As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort("COM2", 4800, 0, 8, 1) com2.WriteLine(
"H0" + Chr(13)) End Using MessageBox.Show(
"Receiver Off-Line") End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End
Class