andicom Posted August 14, 2012 Posted August 14, 2012 (edited) Hi guys, first of all I'm not very good at programming and I've tried to be as explanatory as possible in my codes. I've been trying to write a program which enables a user to chat (recieved & transmit) messages via Serial Port using a simple custom made IR modem. I've constructed the modem and now trying to write my own simple program instead of using Terminal or other already written program (i.e. Tera Term). Btw, I'm on Windows 7 and using the Microsoft Visual Express 2010 Environment I've written the codes below, based on the tutorial at this site. Tutorial Site as well as this microsoft website for reference (putting it here just in case) MSDN Serial Port Class reference Problem (as of now) The combo boxes cmbComPort doesn't seem to catch any available ports on my computer. I suspected that probably there is none! So I checked and here is what I found. Based on this, I assume there is a Serial Port on my computer and hence this is not the source of the problem (Let me know otherwise, because I'm not really sure). I run the debug and an error message popped out. I then build the program to test it out. Both error messages are attached below Questions 1. Are my codes somewhere wrong? or is it my computer? Please show which part is the error 2. Is there anyway to beutify the codes? (make it shorter, simpler, that would be nice) 3. This is a project which accepts any extra features (writing my own program is definitely one of it). I could appreciate if you guys can drop any ideas on the features, whether related to the serial port communication or the program interface itself. I can think of a few, such as Save Chats to file, Load Chat files, Help/Tutorial file - all are to be implemented once I figured the above error. Moreover, I was thinking is there anyway I can draw a 'bubble chat' to display the conversations instead of plain rich text box? that would be nice. Thanks in advance! :D The program files are attached as well as the error message 'Chatty Raffy Version 1.3 'This is a simple program to demonstrate Serial Ports communication via a simple custom made IR Modem. Imports System Imports System.ComponentModel Imports System.Threading Imports System.IO.Ports Public Class frmMain Dim myPort As Array 'an array to store list of available ports Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Fill up the cmbBaudRate Combo box to common baud rates used cmbBaudRate.Items.Add(9600) cmbBaudRate.Items.Add(19200) cmbBaudRate.Items.Add(38400) cmbBaudRate.Items.Add(57600) cmbBaudRate.Items.Add(115200) 'procedure to detect all available ports and store them in the myPort array For Each port_name As String In IO.Ports.SerialPort.GetPortNames Dim myPort As New IO.Ports.SerialPort(port_name) If myPort.IsOpen = True Then cmbComPort.Items.Add(port_name) End If Next 'initiate the combo boxes cmbComPort.SelectedIndex = 0 'set cmbComPort text to the first COM port detected cmbBaudRate.SelectedIndex = 0 'set cmbBaudRate text to the first Baud rate on the list cmbParity.SelectedIndex = 0 'set cmbParity text to the first Baud rate on the list cmbStopBit.SelectedIndex = 0 'set cmbStopBit text to the first Baud rate on the list 'btnDisconnect.Enabled = False 'disable the disconnect button End Sub 'open the selected serial port and start the connection Private Sub btnConnect_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click SerialPort1.PortName = cmbComPort.Text 'set Serial Port to the selected COM port SerialPort1.BaudRate = cmbBaudRate.Text 'set Baud rate to the selected value Baud rate SerialPort1.Parity = cmbParity.Text 'set parity setting to the selected value SerialPort1.StopBits = cmbStopBit.Text 'set stop bit setting to the selected value SerialPort1.DataBits = 8 'use the default 8 bit for data bit length SerialPort1.Open() 'open the chosen serial port btnConnect.Enabled = False 'disable the Connect button btnDisconnect.Enabled = True 'enable the Disconnect button picboxDisconnect.Visible = False 'disable the disconnect picture picboxConnect.Visible = True 'enable the disconnect picture End Sub 'close the serial port currently in used, hence closing the connection Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click SerialPort1.Close() 'close the used Serial Port btnConnect.Enabled = True 'esable the Connect button btnDisconnect.Enabled = False 'disable the Disconnect button picboxDisconnect.Visible = True 'enable the 'disconnect' picture picboxConnect.Visible = False 'disable the 'connect' picture End Sub 'send the text contained in the txtText to the serial port in ASCII using the 'SEND' key Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) SerialPort1.Write(txtTransmit.Text & vbCr) End Sub 'detect data at the serial port and call ReceivedText automatically Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) ReceivedText(SerialPort1.ReadExisting()) End Sub 'read incoming messages at serial port once data is detected Private Sub ReceivedText(ByVal [text] As String) 'compare the ID of the Creating Thread to the ID of the Calling Thread If Me.rtbReceived.InvokeRequired Then Dim x As New SetTextCallback(AddressOf ReceivedText) Me.Invoke(x, New Object() {(text)}) Else Me.rtbReceived.Text &= [text] End If End Sub 'this section prevents user from making any changes to the current connection without disconnecting Private Sub cmbComPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) If SerialPort1.IsOpen = False Then SerialPort1.PortName = cmbComPort.Text Else 'pop an error message if user try to change port without closing the current port in use MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical) End If End Sub Private Sub cmbBaudRate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaudRate.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.BaudRate = cmbBaudRate.Text Else 'pop an error message if user try to change Baud rate without closing the current port in use MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical) End If End Sub Private Sub cmbParity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaudRate.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.Parity = cmbParity.Text Else 'pop an error message if user try to change Baud rate without closing the current port in use MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical) End If End Sub Private Sub cmbStopBit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaudRate.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.StopBits = cmbStopBit.Text Else 'pop an error message if user try to change Baud rate without closing the current port in use MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical) End If End Sub End Class ChattyRaffy V1.3.zipError messages.zip Edited August 15, 2012 by andicom Quote
Administrators PlausiblyDamp Posted August 15, 2012 Administrators Posted August 15, 2012 Given the link was to a .rar file that contained several binaries (.exe etc) I didn't actually look at or execute anything in there.... Which line is the one throwing the error? What exactly was the error in question? Does windows itself report you has having any COM ports? If so does it give any details about the type or settings? Are the settings used in your application the same as the port itself? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
andicom Posted August 15, 2012 Author Posted August 15, 2012 (edited) Actually I attached the whole program files including those already built binaries. My friend over here can open and see the same files, using Microsoft Visual Experess 2010, which I use writing the program. I also include some screenshots of the errors and the screenshot of the Serial port setting via device manager. Here is the link, again.... http://dl.dropbox.com/u/88282904/Version%201.3.rar 1. Which line is the one throwing the error? What exactly was the error in question? The combo boxes cmbComPort doesn't seem to catch any available ports on my computer. I suspected that probably there is none! So I checked and here is what I found. Based on this, I assume there is a Serial Port on my computer and hence this is not the source of the problem That lines would be 'procedure to detect all available ports and store them in the myPort array For Each port_name As String In IO.Ports.SerialPort.GetPortNames Dim myPort As New IO.Ports.SerialPort(port_name) If myPort.IsOpen = True Then cmbComPort.Items.Add(port_name) End If Next 'initiate the combo boxes cmbComPort.SelectedIndex = 0 'set cmbComPort text to the first COM port detected 2. Does windows itself report you has having any COM ports? If so does it give any details about the type or settings? Are the settings used in your application the same as the port itself? Windows doesnt report anything. The combo box just doesnt show any port. That's all. After pressing connect, an error message appear as in the picture below http://s12.postimage.org/gnqcq4bob/error.jpg which mentioning about the missing port in the combo box Thanks for your reply. Help? Edited August 15, 2012 by andicom Quote
Administrators PlausiblyDamp Posted August 15, 2012 Administrators Posted August 15, 2012 If you want to include the files in question then you can simply attach them to this post, for the source code just delete the bin and obj folders, zip it up and attach directly to this post. Images can also be attached directly here. Linking to external sites means in future the file may be unavailable and people reading this post in the future will not have access to them. If you run the application under Visual Studio's debugger which line of code does it break on? Does the call to IO.Ports.SerialPort.GetPortNames return anything? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
andicom Posted August 15, 2012 Author Posted August 15, 2012 the error says that the Port_name cannot be empty, which means the combo box (cmbComPort) is empty, SerialPort1.PortName = cmbComPort.Text which means either the code below didnt capture the available ports, or something else... 'procedure to detect all available ports and store them in the myPort array For Each port_name As String In IO.Ports.SerialPort.GetPortNames() Dim myPort As New IO.Ports.SerialPort(port_name) If myPort.IsOpen = True Then cmbComPort.Items.Add(port_name) End If Next Quote
Administrators PlausiblyDamp Posted August 15, 2012 Administrators Posted August 15, 2012 If you try the sample code from http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx#Y675 does anything get listed to the console? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
andicom Posted August 15, 2012 Author Posted August 15, 2012 Great! now I know that my comp has the port and it's working. So the problem is now is in the code somewhere.. Here is what the console output The following serial ports were found: COM1 Quote
Administrators PlausiblyDamp Posted August 15, 2012 Administrators Posted August 15, 2012 In your code you are only adding the ports to the combobox if it is already open (If myPort.IsOpen = True Then), this is probably filtering out the actual port as it won't be open at this point in your application. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
andicom Posted August 15, 2012 Author Posted August 15, 2012 Okay, you have corrected my understanding on that one. I thought the port will be open when no program is using it and closed when a program is assessing it (as in 'closed' for other program to access the port currently being used) My mistake! :mad: Thanks! however, the problem persists. Seems that the combo box doesnt capture the port available still.. the codes below still give the same error, which means the combo box is still empty when the program is executed 'procedure to detect all available ports and store them in the myPort array Dim myPort As String() = SerialPort.GetPortNames() 'an array to store list of available ports Dim port_name As String For Each port_name In myPort cmbComPort.Items.Add(port_name) Next port_name Quote
Administrators PlausiblyDamp Posted August 15, 2012 Administrators Posted August 15, 2012 If you put a break point on the line Dim myPort As String() = SerialPort.GetPortNames() 'an array to store list of available ports is the array being populated with the available ports? When you run the application is anything appearing in the dropdown portion of the combobox? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
andicom Posted August 15, 2012 Author Posted August 15, 2012 No, nothing is there as in the dropdown, but I can't tell the same for the array content (how do i check it?) btw, i did a quick check again and found out the same thing happens to the combo box cmbBaudRate. The following codes gives nothing to the collection of items in the combo box (I can result to adding items manually for this box, but thats later). 'Fill up the cmbBaudRate Combo box to common baud rates used cmbBaudRate.Items.Add(9600) cmbBaudRate.Items.Add(19200) cmbBaudRate.Items.Add(38400) cmbBaudRate.Items.Add(57600) cmbBaudRate.Items.Add(115200) 'initiate the combo boxes cmbBaudRate.SelectedIndex = 0 'set cmbBaudRate text to the first Baud rate on the list The result is...the cmbBaudRate is also empty! This indicates that the combo box carries some errors with adding the items in using the codes? ... I don't get it Quote
Administrators PlausiblyDamp Posted August 15, 2012 Administrators Posted August 15, 2012 If you put a breakpoint on the line in question and run the app directly from visual studio (F5) then it will enter the debugger when that line is hit. If you then step over that line of code and hover the mouse over the variable myPort you should be able to see it's contents. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
andicom Posted August 16, 2012 Author Posted August 16, 2012 I did that and as I hovered on myPort only this showed up: Dim myPort() As String Did I do it wrong? Quote
Administrators PlausiblyDamp Posted August 16, 2012 Administrators Posted August 16, 2012 If you did it while running under a debugger I would expect it to show the contents of the myPort variable. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
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.