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

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Okay, I made progress. I installed Serial Port Monitor 3.31 and found out interesting data on handshaking between the CNC 88 and the computer. I have tried to mimic the data. I will put down below the beginning part of the NCFadal listing followed by Jan's program that I have modified to send the test file. The beginning and ending parts are what are not the same. When the CNC is in command mode is when data transfer can occur. What starts is an 'A' or 41H sent from the computer. The CNC 88 returns an 'A'. When the CNC 88 sends the 'A' back then the PC starts it's transmission. Now when my program sends an 'A' the CNC 88 also sends back an 'A', BUT before my program can start sending it's data, the CNC 88 manages to sneak in a few more characters first. (See below)


This is the way it is supposed to go:  :)
Code: [Select]
Port opened by process "NCFadal.exe" (PID: 3536)

Request: 5/10/2012 3:14:36 PM.01864 (+27.0630 seconds)

 41                                                A               

Answer: 5/10/2012 3:14:36 PM.01864 (+0.0000 seconds)

 41                                                A               

Request: 5/10/2012 3:14:36 PM.01864 (+0.0000 seconds)

 40 0D 54 41 2C 31 0D 0A 25 0D 0A 4F 37 37 37 0D   @.TA,1..%..O777.
 0A 28 53 54 4F 43 4B 2F 42 4C 4F 43 4B 2C 30 2E   .(STOCK/BLOCK,0.
 30 30 30 2C 30 2E 30 30 30 2C 30 2E 30 30 30 2C   000,0.000,0.000,
Here is my program  :(
Code: [Select]
Port opened by process "COM Port Terminal.exe" (PID: 2596)

Request: 5/10/2012 3:12:27 PM.35964 (+60.0324 seconds)

 41                                                A               

Answer: 5/10/2012 3:12:27 PM.56264 (+0.2031 seconds)

 41 44 56 0D 0A 0D 0A                              ADV....         

Request: 5/10/2012 3:12:29 PM.67264 (+1.2344 seconds)

 40 0D 54 41 2C 31 0D 0A 25 0D 0A 4F 37 37 37 0D   @.TA,1..%..O777.
 0A 28 53 54 4F 43 4B 2F 42 4C 4F 43 4B 2C 30 2E   .(STOCK/BLOCK,0.
 30 30 30 2C 30 2E 30 30 30 2C 30 2E 30 30 30 2C   000,0.000,0.000,

And I am sending the block immediately after I send my 'A' (41H)

HDowns

  • Member
  • ***
  • Posts: 14
I would suggest to monitor a state of hardware flow control lines too (RTS/CTS). Usually CNC machines use these lines to control a data flow. I don't know can Serial Port Monitor 3.31 show it, but Advanced Serial Port Monitor 4 can.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Thanks HDowns. I see the free verison I have shows the green/red light at the bottom for RTS/CTS. But I don't see any data. How much is Advanced Serial Port Monitor 4 and is it downloadable? I

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
I found the Advanced Serial Port Monitor 4. I downloaded the free version, but it can't do anything. The advanced version is 60 dollars. I am wondering if there is a cheaper version that would do what we are looking for. This program has 'lots of bells and whistles' which is great, but it may be a bit overkill for the shop. It will be used a couple of times and then put aside.

HDowns

  • Member
  • ***
  • Posts: 14
The trial version can also help you.
1. Switch it to the Spy mode.
2. In the program settings enable all system events.
3. Open the port
4. Start the NCFadal program

The monitor window should show you same as Serial Port Monitor, but will add some system events in red color.

It is possible it will help you.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Great, I will try it. Thanks. I will let you know.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Below is the NCFadal.exe serial data. It 'Purge the serial port: RXABORT, RXCLEAR which my program doesn't do. So I am thinking that I have to do that in the code first. Will work on, only have an hour to figure this out today, but it's a start!

Code: [Select]
<20120525143207.458 SYS>
Purge the serial port: RXABORT, RXCLEAR
<20120525143207.880 TX>
% [len=1]
<20120525143207.880 TX>
<LF>O777 [len=5]
<20120525143207.880 TX>
<LF>(STOCK/BLOCK,0.000,0.000,0.000,-5.275,1.500,0.188) [len=51]

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Okay, I am going to put in the successful NCFadal program start. The program: Purge the serial port: RXABORT, RXCLEAR, TXABORT, TXCLEAR

To send to Fadal CNC you type the TA,1 which you see near the end of the code, and the program code itself starts when you send a %

My program does not purge the ports nor turn on the DTR, it is the offstate in my code.
Code: [Select]
<20120525141323.189 SYS>
COM is open
<20120525141323.189 SYS>
In/out queue size 1024/512
<20120525141323.189 SYS>
Purge the serial port: RXABORT, RXCLEAR, TXABORT, TXCLEAR
<20120525141323.189 SYS>
Set timeouts: ReadInterval=-1, ReadTotalTimeoutMultiplier=0, ReadTotalTimeoutConstant=0, WriteTotalTimeoutMultiplier=0, WriteTotalTimeoutConstant=5000
<20120525141323.189 SYS>
Baud rate 9600
<20120525141323.189 SYS>
RTS off
<20120525141323.189 SYS>
DTR on
<20120525141323.189 SYS>
Data bits=7, Stop bits=1, Parity=Even
<20120525141323.189 SYS>
Set chars: Eof=0x1A, Error=0x3F, Break=0x3F, Event=0x00, Xon=0x11, Xoff=0x13
<20120525141323.189 SYS>
Handflow: ControlHandShake=(DTR_CONTROL), FlowReplace=(AUTO_TRANSMIT, AUTO_RECEIVE, ERROR_CHAR), XonLimit=256, XoffLimit=256
<20120525141323.189 SYS>
Purge the serial port: TXABORT, TXCLEAR
<20120525141323.189 TX>
A
<20120525141323.189 RX>
A
<20120525141323.189 SYS>
Purge the serial port: TXABORT, TXCLEAR
<20120525141323.189 TX>
@ [len=1]
<20120525141323.189 SYS>
Purge the serial port: RXABORT, RXCLEAR
<20120525141323.501 SYS>
Purge the serial port: RXABORT, RXCLEAR
<20120525141323.814 TX>
TA,1 [len=4]
<20120525141323.814 TX>
<LF>% [len=2]
<20120525141323.939 TX>
<LF>O777 [len=5]
<20120525141323.939 TX>

I hope I haven't confused everyone, but I see the advanced serial port program is valuable. I haven't figured out how to change my program, though I did add this to the ComPort module:

Code: [Select]
' PurgeComm function BT-25May2012

    Declare Function PurgeComm Lib "kernel32" (ByVal hFile As Integer, ByVal dwFlags As Integer) As Integer

Shop is closing, so I will send now, but monitoring.  :)

HDowns

  • Member
  • ***
  • Posts: 14
The DTR signal is important too, because it says the machine that it can send data.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Yes, I will have to see if I can get the VB program to turn DTR on. Alternatively, if I could run another RS-232 program calling it from VB .Net, would be okay, in fact it may be the best way, loading the NC data program and also sending it on the RS-232 program. Then I can just use the VB program to list the different programming files to be used.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Okay then all I would need to do is to:

Code: [Select]
SelectedPort.DtrEnable = True

Before I try communicating with CNC 88 controller?

I want to go into the shop, and be able to accomplish something. Or am I missing something. I think I tried this before I left last week, but haven't been able to get back there. I would like to go in Saturday morning and try this.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
The SelectedPort.DtrEnable = True works. In Jan's program it seems that it needs to be called inside the ComPorts.vb module. I will have to get inside the program more and see what is happening. For production I have hidden the buttons that allow you to change the parity, baud rate and other parameters, but made it visible again so I could see what is happening. One thing I had wrong was the parity was supposed to be even and I had none. That was a big mistake, duh! It should have Xon/Xoff as protocol for transferring data, and when I select it, and then go back it doesn't show that so will have to investigate.

The advanced serial port monitor program works great in showing the state of things. I am trying to use the statements:

       
Code: [Select]
ComPorts.PurgeComm(hFile:=1, dwFlags:=1)
        ComPorts.PurgeComm(hFile:=1, dwFlags:=4)

I am more upbeat on this. In fact I am thinking that somehow I sent a program to the CNC 88 controller, because my program called 777 is there. It's possible I did it with the NCFadal program but not sure. I have logged the data sent from my program in an appended file, so I grabbed it before leaving the shop.

I will be back on Monday, especially since I only have 6 more days to use the trial version of the software!  :)

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
The DTR Enable works using the advanced monitor ports, but I can't seem to purge the ports. My code is below:

Code: [Select]
                        SelectedPort.DtrEnable = True
                                               
                        PurgeComm(hFile:=1, dwFlags:=2)
                        PurgeComm(hFile:=1, dwFlags:=8)
                        PurgeComm(hFile:=1, dwFlags:=1)
                        PurgeComm(hFile:=1, dwFlags:=4)
                       
I am wondering if I have the parameters wrong on the PurgeComm function)?

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
I am still puzzled about why I can't get the NC Fadal Code to go into the CNC 88. I made copies of both my attempt and the successful NCFadal.exe program. I have posted them on my website at:

My failed attemp: http://www.bdtcomp.com/rs232/bob4jun2012_131pm.html

NC Fadal successful program code transfer: http://www.bdtcomp.com/rs232/ncfadal4jun2012_135pm.html

The black text highlighted in yellow is from the Windows XP desktop. The black text is from the CNC 88

The red text is system events, as well as the greyed text which shows sys, Tx and Rx states.
 :)

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
As I finish up the day at the plant, I have been successful in uploading the programs, I removed two characters in the file, an 'A' and an '@' .  The reason I did this, is because the protocol instructions say nothing about these characters. Now you ask, WHY did I put them in to begin with? The reason was that I captured them with the port sniffing programs. So I am now able to get the code into the CNC 88 and they register as programs and appear to be good. There are two issues, one is that there is supposed to be one of two things that print on the CNC 88 screen when the program is loaded. If you run a Checksum (which I want to do at a future date) you get the message, 'TAPE IS GOOD', and without a checksum, which has been the norm here 'TAPE INPUT TERMINATED' . I don't get the message, rather the data stops near the end. I took some pictures with my phone, maybe I will upload these later.


2nd, I am not getting any characters in the port sniffing program now? I do get the red characters and I have put what I am getting on another web page:  http://www.bdtcomp.com/rs232/bob4jun2012_425pm.html

I have to consider that I have made progress, and I may have lost people here, but maybe their is a reincarnated Einstein that is seeing something here.  8)

I HAVE UPLOADED THE CNC 88 PICTURES!

[attachment deleted by admin]
« Last Edit: June 04, 2012, 04:56:25 pm by btcomp »