Ports
April 21, 2014, 07:41:51 am *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: SMF - Just Installed!
 
   Home   Help Search Login Register  
Welcome to the PORTS Forum. Please post your questions, answers, and comments relating to USB and other hardware interfaces in the appropriate forums below.

IMPORTANT! This forum is is moving to JANAXELSON.COM. The new site is live now. Try it: http://janaxelson.com/forum. Please update your bookmarks and links. After June 1, 2014, this forum will be available ONLY at janaxelson.com and links to content at lvr.com will no longer work. - Jan
Pages: [1] 2 3 ... 6
  Print  
Author Topic: Simple VB.Net RS-232 program to capture serial port data to a text file  (Read 40453 times)
btcomp
Frequent Contributor
****
Posts: 58



« on: April 24, 2012, 04:23:46 pm »

I know this should be simple. But I haven't used serial ports in a while, but I bought a virtual RS-232 converter off Amazon, for a client connecting a moving machine, that moves in the x and y coordinates. Now I need to be able to write a text file that captures data that is transmitted when a sensor is activated and reads the x,y position of the movable flat machine.

I have created a loop back connector to the virtual port, now COM4. But I can't seem to get data into a VB variable so I can write it to a text file.
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 2041


WWW
« Reply #1 on: April 24, 2012, 07:41:06 pm »

If you just want to be able to read serial data from the machine, you could start with my COM port terminal application:

Lvr.com/serport.htm#my_example_code

Look at the DataReceived routine.

Jan
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #2 on: April 24, 2012, 09:39:02 pm »

Thanks Jan,

I was looking at your code a few days ago, and I see that I could send the data to a text file in the DataReceived routine. I was looking at this program this afternoon, http://www.dreamincode.net/forums/topic/37361-serial-port-communication-in-vbnet/  too, which works also, but your DataReceived routine would be a good place to insert the text file write routine and you have shown where it could be added. Thanks.  Smiley
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #3 on: April 25, 2012, 09:10:55 pm »

I put in the code for a simple text file:

Code:
Friend Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

        Dim newReceivedData As String

        '*********************************************************BT-25Apr2012
        Dim FILE_NAME As String = "C:\RCMtools2012\rs232.txt"
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME,true)
        '*********************************************************BT-25Apr2012
        Try
            ' Get data from the COM port.

            newReceivedData = selectedPort.ReadExisting

            ' Save the number of characters received.

            receivedDataLength += newReceivedData.Length

            RaiseEvent UserInterfaceData("AppendToMonitorTextBox", newReceivedData, Color.Black)

            '*********************************************************BT-25Apr2012
            objWriter.Write(newReceivedData)
            objWriter.Close()
            '*********************************************************BT-25Apr2012

This works great when I type in text. Now the Numerical Control Device will be hooked up instead of using the text box. I am thinking I need to modify the ProcessTextboxInput() procedure. As there isn't going to be any text input, just the text that is generated by the Numerical Control Device! I am not sure where I should be modifying the code!

Is there a way to send some test data through the RS232 cable and port?
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 2041


WWW
« Reply #4 on: April 26, 2012, 10:20:57 am »

Make a copy of the ProcessTextboxInput routine and rename it, say, SendMyData.

Set userInput to the data you want to send.

Delete everything in the routine that has to do with the text box.

To send the data, call the routine.

Jan
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #5 on: April 26, 2012, 01:17:56 pm »

Thanks Jan,

I have copied the procedure and will look at the userInput variable and see how to grab the RS-232 data.
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #6 on: April 27, 2012, 07:40:28 pm »

Jan, your program worked great at the company's shop today. I was pleasantly surprised that the data was just receiving, (in green) in the textbox. But the data was mostly gibberish with ? characters. Well the owner showed the man helping me what the settings were and then I discovered that I couldn't set a couple of parameters, just the baud rate and handshaking. So I did some programming while the man returned to his work and shortly had this.

                       
Code:
'**********************************************27Apr2012
                        ' The port parameters set here for RCMTools - to be sure they are correct.
                        SelectedPort.Parity = Parity.None
                        SelectedPort.DataBits = 7
                        SelectedPort.StopBits = StopBits.One
                        SelectedPort.Handshake = Handshake.None
                        '**********************************************27Apr2012

Then worked like a charm. Now this is hooked up to their Fadal CNC 88 controller which is connected to the Fadal VMC 4020 machine which position is being mapped and data sent through the RS-232 port and will be able to record the data nicely to a text file. The problem is the Fadal software on the same computer has to communicate through the same RS-232 port as Jan's program. There has to be a way for the Fadal program to let Jan's program know to turn off and vice versa. Jan's program ONLY needs to close it's port connection when the Fadal software on the computer is loading the program data into the CNC.

Note: I am not a Fadal programmer, though I would love to learn it, and if this project succeeds like it did today, I may be putting in more hours, here  Grin .

I am not sure if it is possible for the Fadal an Jan's program to communicate with each other. I am thinking perhaps something like a 'socket' which would allow communication between the two. I guess I am going to have to learn more about CNC programming?
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 2041


WWW
« Reply #7 on: April 28, 2012, 11:51:17 am »

Here is one article about sharing data between applications:

http://www.codeguru.com/csharp/csharp/cs_syntax/remoting/article.php/c6901/NET-Remoting-in-Visual-C.htm

I'm not an expert on this.

Jan
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #8 on: April 28, 2012, 12:32:48 pm »

Thanks Jan, looks pretty complex, but will print out the text pages.  My local library has a couple of books on CNC which I am planning on picking up, so will review them to see if there is anything I can learn in the books that may help.
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #9 on: May 05, 2012, 08:20:40 am »

Well I am able to communicate with the CNC and I am thinking that I can upload the program code NC stuff, and your program has amazed me and the workers that it can talk to the CNC monitor. So I think that just using the Vb.Net may be the answer, then the RS-232 port can stay open all the time. I just have to learn more CNC and I got a bit familiar with uploading downloading and running the test program that is used to generate data for the text file. Crude yet, but I ordered your RS-232 book, even paid 3 dollars more on my Prime account to get it Monday. Well I'm at the shop on their dime so I will need to get busy, and brush up on my VB code and CNC code for this project! Smiley
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #10 on: May 09, 2012, 05:43:41 am »

Well I am stuck with getting program code to be saved in the CNC controller. Normally a program called NCFadal is used. It is a serial communications program that you open the code (ex. below) and click 'to CNC' or 'from CNC' . I can get the code to show on the CNC screen but I can't get it to be saved as a program. Below is just the start and end code blocks. All programs start and end with a percent character. CR characters on each line.

The idea is to have the VB program sit on the desktop computer and whatever program the operator wants to use they choose it, the program uploads automatically and is ready to use at the CNC monitor. I could also have the VB program load the NCFadal program (and I already have that parameter in the line, but I can't find a way to get the CNC code (i.e. TestData.nc) as a paramter of the NCFadal program to then load into the CNC. That's the dilemma.

I seem to be missing some important serial communcication commands that are needed between the CNC and the desktop PC?

Code:
%
O1
(STOCK/BLOCK,0.000,0.000,0.000,-5.275,1.500,0.188)
( MILL: 4020_3 )
( 4.2530 Z HEIGHT OFF BED TO BOTTOM OF PART )
( 1.3750 PARALLEL HEIGHT )
( FRIDAY 4/27/12 11.05AM )
*
*PUT TOOL 500 IN SPINDLE
# SPRINT "POCKET-ID 1234"
# SPRINT "WALLS-PLUS-TOL 0.001"

,,,

#:END_WALL_SIZE_CHECK
G9
M5M9
* DONE *
T2M6
G52 X0 Y0 Z0
G00 H0 Z0
E0 X0 Y0
(POST UTILITY *8063*)
M02
%
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 2041


WWW
« Reply #11 on: May 09, 2012, 08:55:45 am »

Unfortunately, I have no knowledge of NCFadal and thus no tips on how to save the program code.

Jan
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #12 on: May 09, 2012, 09:01:53 am »

Thanks Jan,

I kind of figured this, being it was company specific. But I got a phone number to their technical support, so maybe I can get info that way. Will post if it is helpful here.  Smiley
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 2041


WWW
« Reply #13 on: May 09, 2012, 10:38:46 am »

Good luck!

Jan
Logged
btcomp
Frequent Contributor
****
Posts: 58



« Reply #14 on: May 09, 2012, 10:40:55 am »

Thanks Jan, I was thinking of this while on the treadmill this morning, maybe I can capture the RS-232 data coming out of the PC on com1 to the CNC88? I don't know if possible but will Google this?  Smiley
Logged
Pages: [1] 2 3 ... 6
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!