Author Topic: Simple VB.Net RS-232 program to capture serial port data to a text file  (Read 156936 times)

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
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

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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.  :)

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
I put in the code for a simple text file:

Code: [Select]
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?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
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

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Thanks Jan,

I have copied the procedure and will look at the userInput variable and see how to grab the RS-232 data.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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: [Select]
'**********************************************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  ;D .

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?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
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

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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! :)

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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: [Select]
%
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
%

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Unfortunately, I have no knowledge of NCFadal and thus no tips on how to save the program code.

Jan

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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.  :)

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Good luck!

Jan

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
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?  :)