nachiketh
Posts: 45
Joined: Wed Sep 25, 2013 4:54 pm

RS232 Interfacing on RPi

Wed Jul 15, 2015 9:25 am

Hi Folks,
So I have developed a Win32 C++ program which basically communicates over RS232 interface to receive some data from a legacy hardware. I'm trying to port this application onto a RaspberryPi, so I first wanted to check the feasibility of doing this. I have already checked with PySerial, however the low level controls that are required by my Applications are not provided by PySerial, for example, there is an IOCTL setting which sets the IOCTL_SERIAL_LSRMST_INSERT using the following C++ code:

Code: Select all

lsrmst[0] = 255;
DeviceIoControl(file,IOCTL_SERIAL_LSRMST_INSERT,lsrmst,1,NULL,0,&junk,(LPOVERLAPPED) NULL);
According to the MSDN documentation:
Enables or disables the placement of line status and modem status values into the regular data stream that an application acquires through the ReadFile function.When this line-status and modem-status data placement mode is enabled, status values are preceded in the data stream by an escape character. The user-definable escape character is set by the IOCTL_SERIAL_LSRMST_INSERT control code.
There is no API in PySerial to do this at this point in time.
If I want to use these kind of advanced settings on the serial port, which programming language/APIs would be the best to take to go about programming it. TIA!
Regards,
nachiketh

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 9:29 am

The Pi's UART is TTL (0-3.3V) not RS232 (-12-12V).

Does Linux support those IOCTLs? Have you googled for Linux and the ioctl?

nachiketh
Posts: 45
Joined: Wed Sep 25, 2013 4:54 pm

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 9:37 am

Hi Joan,
joan wrote:The Pi's UART is TTL (0-3.3V) not RS232 (-12-12V).
So does that mean the UART on RPi cannot be used as is for my application?
joan wrote:Does Linux support those IOCTLs? Have you googled for Linux and the ioctl?
Yes I have googled around quite a bit around this, but there is nothing available on Linux IOCTL for this.
Regards,
nachiketh

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 9:44 am

nachiketh wrote:Hi Joan,
joan wrote:The Pi's UART is TTL (0-3.3V) not RS232 (-12-12V).
So does that mean the UART on RPi cannot be used as is for my application?
You can buy cheap converter boards that use the MAX3232 (not the 5V MAX323) chip to convert the Txd and Rxd lines of the Pi to RS-232 levels.

As to the IOCTLs, I suspect you could write the functionality of them yourself, if you go low enough level on the port. It sounds like they are just injecting some special sequences into the byte stream, which shouldn't be hard to do, although I don't know exactly what "line status and modem status" refer to in this case.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 9:45 am

You'll need a level shifter if you want to connect a "real" RS232 device to the on-board UART.

Alternatively you could use a USB to serial adapter, which will accept the full RS232 voltage swing.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 9:47 am

And yes, you should be able to inject your own characters trivially into the data stream. What are they?

nachiketh
Posts: 45
Joined: Wed Sep 25, 2013 4:54 pm

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 9:57 am

ame wrote:And yes, you should be able to inject your own characters trivially into the data stream. What are they?
The API is setting the value to 255(ff), so it in the protocol it looks something like this:

Code: Select all

34	13/07/2015 14:43:13	IRP_MJ_DEVICE_CONTROL (IOCTL_SERIAL_LSRMST_INSERT)	DOWN	STATUS_SUCCESS	ff 	ÿ
35	13/07/2015 14:43:13	IRP_MJ_DEVICE_CONTROL (IOCTL_SERIAL_LSRMST_INSERT)	UP	STATUS_SUCCESS		
Can you please elaborate on "inject your own characters" ? How can I do this ?
Thanks & Regards,
nachiketh

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 10:00 am

nachiketh wrote: So does that mean the UART on RPi cannot be used as is for my application?
It means that you should be aware of it, and use the appropritate convertion chip/circuit.
You could also just attach a usb2serial device, and dont use the GPIO pins.
I also don't think the GPIO has pins for hardware handshaking (RTS/CTS etc.) but I might be wrong.
nachiketh wrote:
joan wrote:Does Linux support those IOCTLs? Have you googled for Linux and the ioctl?
Yes I have googled around quite a bit around this, but there is nothing available on Linux IOCTL for this.
Regards,
nachiketh
[/quote]

The question is, do you really need it ? As I understand this mode just adds escaped bytes to the data stream which your code has to filter out when talking to your device. That means that the protocol might work just as fine without these extra characters.
If your legacy device is highly dependent on the extra control signals ( RTS/CTS/DSR ) then there is other methods of handling them.

nachiketh
Posts: 45
Joined: Wed Sep 25, 2013 4:54 pm

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 10:05 am

topguy wrote:The question is, do you really need it ?
I tried interfacing with the legacy device using PySerial without setting the controls, I did not receive any response from the device. I then had to move to C++ where I can set these control codes and I was then able to successfully interface with the device. So I guess the legacy device is expecting these Control Codes to be set to complete the Handshake.
Thanks & Regards,
nachiketh

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 10:45 am

By "inject your own characters" I mean just send some extra characters to the serial port which are additional to any characters that the protocol specifies.

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 10:55 am

nachiketh wrote:
topguy wrote:The question is, do you really need it ?
I tried interfacing with the legacy device using PySerial without setting the controls, I did not receive any response from the device. I then had to move to C++ where I can set these control codes and I was then able to successfully interface with the device.
But you did this on Windows ?
I have a feeling that these codes we are talking about only exist on the Windows side. (between hardware and application)
(Thats how I interpret "Enables or disables placement of a line and modem status data into the data stream." anyway.)

But I can't be sure if it implies (or requires) "hardware handshake" to be enabled, and maybe that is all you are missing from your PySerial code.

nachiketh
Posts: 45
Joined: Wed Sep 25, 2013 4:54 pm

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 6:41 pm

topguy wrote: But you did this on Windows ?
I have a feeling that these codes we are talking about only exist on the Windows side. (between hardware and application)
(Thats how I interpret "Enables or disables placement of a line and modem status data into the data stream." anyway.)

But I can't be sure if it implies (or requires) "hardware handshake" to be enabled, and maybe that is all you are missing from your PySerial code.
Yes I did this on Windows. First I tried with PySerial, and then Java, and finally C++. I was able to successfully communicate with the device only through my C++ code, upon analysing the serial data traffic, It was clear that the only difference was the Control signals which I was able to set via C++ IOCTL APIs. I'm not an expert in hardware, so I'm assuming its the software which is lacking some functionalities in PySerial and Java, I might be wrong! :roll:

nachiketh
Posts: 45
Joined: Wed Sep 25, 2013 4:54 pm

Re: RS232 Interfacing on RPi

Wed Jul 15, 2015 6:42 pm

ame wrote:By "inject your own characters" I mean just send some extra characters to the serial port which are additional to any characters that the protocol specifies.
Thanks, will try this out and see if I can actually simulate the same behaviour on Linux

Return to “Advanced users”