Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial port baud rate in measurement studio visa?

I am using the following code to open a session to a serial port. Can anyone tell me how to change the baud rate, stop bits, parity, flow control, etc.
The ses.ResourceName is equal to "ASRL1::INSTR" which is com1 on the computer the program is running on. What I want to do is set com1 to a baud rate of 9600, 1 stop bit, no parity, no flow control. How do I do this?
Thanks

Barry


//Creates a message based session on com1 and tries to write ascii 8 out the serial port and then read the response back.
MessageBasedSession mbs = (MessageBasedSession)ResourceManager.GetLocalManager().Open(ses.ResourceName);
mbs.Write("8");
string idQueryResponse = mbs.ReadString();
Message 1 of 4
(4,979 Views)
Hello Barry

Cast the messagebasedsession to a serialsession and this should provide you with all the properties you are looking for.

SerialSession ss = mbs as SerialSession;
If( ss!= null)
{
//Do serial configuration here by setting the appropriate properties on the serialsession class

}

Or you could use the SerialSession constructor if you are sure that you will not be switching between different types of resources (GPIB, TCP).

SerialSession ss = new SerialSession("ASRL1::INSTR");

Refer to the documentation for information about the SerialSession constructor and some of its other overloads.

Hope this helps. Let me know if I missed anything.
Bilal Durrani
NI
Message 2 of 4
(4,964 Views)
Thanks, that did the trick.
From your response I assume the following are ok also:

SerialSession ss = mbs as SerialSession;
GpibSession gpibs = mbs as GpibSession;
UsbSession usbs = mbs as UsbSession;
TcpipSession Tcpips = mbs as TcpipSession;

Regarding the serial port setup, can you tell me which parameter sets the port mode to text or binary transfer? I have been using the MSCOMM activex control and this is one of its properties that can be set in code or the properties editor. I just can't seem to find any parameter that does the same in the property list for the ss serial session.
The previous reply really helped,
Thank you very much.
Barry
Message 3 of 4
(4,955 Views)
Those casts will only work if you have a session of the correct type. The MessageBaseSession class is actually a base class for the specific session classes so as long as you have the right kind of resource, you should be able to cast that messagebasedsession class into something more specfic. So for example, using a resource like "TCPIP::MyComputerName::7::SOCKET" instatiates a Tcpipsocket class and trying to cast that into a GpibSession will generate an invalid cast exception. Check out the help for ResourceManager.Open for more information. The help should be integrated into the Visual Studio envirnment and would show up under Contents >> Measurement Studio .NET class library.

There is no specific property for binary or text writes. You must use the appropriate overload for the Write method. You can either write out a byte array or a string. The string will transfer data as ASCII. The byte array Write() overload will transfer as binary data.

Hope this helps
Bilal Durrani
NI
Message 4 of 4
(4,931 Views)