LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Read data from RS232 (Serial) port

Solved!
Go to solution

Hi.

 

I'm trying to develop a code to control an automated traverse (Holmarc) which connects through the serial communication port. The current code works when I run it in Highlight Execution mode, but does not work when I run it normally. I assume the time delay is what makes the code work.

 

The problem is I need to be able to read the data sent by the traverse back. For instance, if I send '19', I'm supposed to get '10' back, but it does not happen instantly; there's a time delay, and it's not a fixed delay. And it only send 1 byte of acknowledgement for each byte sent, so I assume trying to read the port continuously won't work (I tried it and it gave me a timeout error)

 

Is there any way I would be able to send data, and be able to read the data as and when it responds? Any help would be appreciated.

0 Kudos
Message 1 of 13
(6,854 Views)

Hi Arun,

 

DON'T use BytesAtPort! Especially NOT when you use a TermChar!

Either read a fixed number of bytes or read more than you expect to receive - the TermChar will be handled correctly…

 

Read this thread where Victor had similar problems!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 13
(6,818 Views)

Hi GerdW.

 

My problem is the opposite of what Victor faced, from what I understand.

For each 1 byte command I send, I only get 1 byte in return. And as of now, it seems the code is reading the port before it receives anything. And I don't know how long the delay is; since it's a traverse, the more I move it, the longer the response code will take, so I cannot have a fixed delay. As to the BytesAtPort, I tried using that, and giving a constant of '1', but hasn't made a difference so far.

 

And I always will only receive 1 byte data, but I need to be sending the next byte after I receive it only, so I don't see how reading more than 1 byte helps. Can you clarify?

 

And what did you mean by TermChar? I'm not using WaitOnEvent (I tried that in an unsuccessful attempt; I don't know if I implemented it right though.)

 

0 Kudos
Message 3 of 13
(6,808 Views)

Update: I fixed part of the problem by putting the BytesAtPort in a loop, to read only when data is available, with a loop termination condition of BytesAtPort>0

 

But for some weird reason, that code only works occasionally. After a while, the BytesAtPort return 0 for some reason, and I have to reset the traverse to get it to work again.

 

So now my problem is 1. to make the code always work.

And 2. In after.jpg, you can see '170' in the 18th position of the last array. That is not supposed to be there. In the Input 17th position, when I send '125', the traverse responds with a '10' (as seen) and starts moving, and sends '170' when it completes. And THEN, I should be sending '163' (Input 18th element), which should return '164'. So, I should be reading 2 bytes there, one after the other, only for that case, but each byte comes at 2 different times. Right now, when I send the last 125, the traverse starts moving, but I end up sending '163' before completion, which stops the movement and sends me '170' there.

Download All
0 Kudos
Message 4 of 13
(6,799 Views)

Hi Arun,

 

the solution to your problem is very simple: Get rid of BytesAtPort!

 

Either rely on TermChar (as you have it configured!) or read a fixed amount of bytes from serial port. Using BytesAtPort is wrong in 99.9% of all cases!

 

I still believe you got the very same problem like Victor…

 

Btw. Attaching images of frontpanels doesn't really help us to understand your VI/block diagram…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 13
(6,790 Views)

Hi GerdW,

 

I am still not following what you mean by TermChar, and I don't see where I have it configured.

 

As for reading a fixed amount of bytes from serial port, I have already tried that, and it did not work. The only thing that HAS worked is using the BytesAtPort; whether it's a good approach or bad, I don't know, but at least it worked.

 

Also, I attached the images of the front panel because I have already attached the code I used in my first post; it's prety much redundant to post it again when all I did was throw the BytesAtPort into a loop.

0 Kudos
Message 6 of 13
(6,774 Views)

Hi Arun,

 

I am still not following what you mean by TermChar, and I don't see where I have it configured.

Read the (full) help for the VISA-SerialPortConfig function!

 

it's prety much redundant to post it again when all I did was throw the BytesAtPort into a loop.

You made changes to your VI and we don't know how those changes look like…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 13
(6,761 Views)

Hi GerdW,

 

Thank you for clarifying. I don't understand what you meant when you said I had it configured, because I wasn't even aware of those options in VISA Configure Serial Port it until you pointed it out; I'm relatively new at LabView. 

 

I see your point, but I cannot configure for one termination character, since I have many. 10, or newline, is just one of them. Like I said earlier, 170 is one of them. 7 or 8 more different codes are received; I'm using a traverse, so it responds differently for each command, like Move, or Stop, or Read. Can I use multiple termination characters?

 

Also, I've attached the updated code.

 

0 Kudos
Message 8 of 13
(6,751 Views)

Hi Arun,

 

but I cannot configure for one termination character, since I have many.

The usage case for TermChars is to have just one TermChar in a communication protocol. Using different TermChars would be problematic as you would need to change it whenever you expect a different one…

It would really help if you would name the exact type of your hardware and could provide a link to its manual (with information about the used comm protocol)!

 

I'm relatively new at LabView.

So you should always read the help… 😄

 

Btw. I always wonder why people change their nicknames in the middle of a discussion. It makes it harder to follow when someone else reads this thread somewhen in the future…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 13
(6,748 Views)

Hi GerdW, 

 

Exactly. And it's not exactly a termincation character. 10 is the acknowledgement code that the traverse sends back, which is one of the codes that the traverse replies with, along with 7 or 8 others like I said, all of which would come in one run. 

As to the hardware, I've attached a link of what it looks like, if you want. It's the XYZ Theta Stage: 

http://www.holmarc.com/motorized_multi_axis_stage.php

As for the manual, even I'd like to get one myself. 😄 They don't have a proper manual, or a software; they use a code they wrote in C, which in itself is not at all user-friendly, and very clunky. The response codes were obtained by directly emailing them about the hardware. 

 

And I do read the Help section. It's just that I don't have that amazing a memory to remember everything I do. 😛 Or I need to have the experience coding in LabView like you do, which takes a lot of time. 

 

About the name, I actually changed it before starting the discussion itself; I was wondering why it didn't changed when I did, and now when I logged in from my own PC, it magically changed. So don't blame me. 😛

0 Kudos
Message 10 of 13
(6,741 Views)