LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

problem with serial communication PLEASE HELP

Hello everybody !
 
I'd like to ask You for some feedback on my wierdo problem that I got with my program.
 
I have built a device that detects changes in magnetic field. It is used for detecting vehicles passing it on a highway. Generally the main components communicate with each other by RS-485 protocol, but I would like to connect the device to a PC. To do so I am using a RS-232 protocol (I have a RS485=RS232 hardware converter).
 
Now, I made the program (even two), which generally works, BUT some wierd things are going on sometimes; it happens that I have a problem to get a communication (after that, resetting the device by un-powering it helps, sometimes I have to restart the PC as well) but overall it happens quite not so often. The project itself is for passing one of my subject on the university - so I have to run the whole thing over there, here comes another problem : the program did not work on all of the bunch of PC's that are there. Same thing happens on my notebook. The effect of 'non working' is : I am recieving same data packet that is beeing sent... BTW : notebook does not have a RS232 port and I am using a USB-RS232 calbe converter (to count it out from the reason of problems I have checked it on my main PC and it works almost fine - almost because ocasionally I am getting the same sympthomps as I mentioned before ).
 
Other problem is, when it works, that I am loosing communication for couple of hundreds of miliseconds and then it comes back to normal (i can see it on the osciloscope).
 
I alredy looked for some answears here and there, and I found some suggestions about using 'buffer cleaning method', unfortunatelly I kinda do not know how to do it, and might that help anyways ?
I also thought about using VISA drivers, but I am having trouble with it as well - the damn thing just does not work.
 
My very bling guess is that everyting that I am expierencing is about using the communication protocol and seting it right. I am attaching both of the programs, PLEASE anyone pretty pretty help I totally ran out of ideas and knowledge and day after day I am turning it on and off on my PC and notebook hoping some miracle will occour ...  😞
.
Many many thanks in advance !
 
  Best regards
                     
                Tom
 
P.S. the program is very unfinished (man things are just put in), the main thing I am struggling now is getting, and setting the communication protocol right.
0 Kudos
Message 1 of 2
(2,507 Views)
The biggest problem I see is in the sequence:

  1. Write out a string.
  2. Wait 50 mSec.
  3. Ask how many bytes have been received.
  4. Read that many bytes.

Can you guarantee that the data can make a round trip in 50 mSec?

You should know that the SERIAL PORT WRITE completes NOT when the data has been sent out the port, but when it has been BUFFERED for sending. In other words it returns and starts the timer BEFORE it is actually sent.

Also, given that there are 232 -> 485 --> USB conversions involved in both directions, I would bet that you are sometimes not allowing enough time for the data to get back.

If you can control the protocol of what gets sent back, I would change your receiving logic. I think you should check for a carriage return or some other delimiter, and not react until you get that character (or until a reasonable timeout expires).

In other words, try synchronizing the two ends. Something like:

  1. N = Get # bytes at port
  2. Read N bytes from port (flush any characters waiting).
  3. Write out command string.
  4. TimeLimit = NOW + 1000 mSec (or whatever)
  5. S = ''; (Working string)
  6. repeat
  7. N = #Bytes at Port
  8. If N > 0
  9. S = S + Read N Bytes
  10. Found = Search String for Delimiter
  11. until Found or (Now > TimeLimit)
  12. If Found, parse string S
You are also re-initializing the port (SERIAL PORT INIT) every time thru the outer loop - that's unnecessary.

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 2 of 2
(2,496 Views)