SerialDevice IsRequestToSendEnabled Raspberry Pi - raspberry-pi2

I am programming with vb and have a UWP project using a Raspberry Pi.
When I use a USB to RS485 converter I can use the IsRequestToSendEnabled from SerialDevice to sync communication between the Raspberry and the PLC, but when I try to use the UART, with a TTL to RS485 converter, the IsRequestToSendEnabled is not available.
The following error shows up:
The request is not supported. (Exception from HRESULT: 0x80070032)
And
System.AccessViolationException
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The UART does not support it? I am no expert in UART, so how do I sync communication frames in Modbus then? The TTL to RS485 is Half-Duplex, I assume I have to sync communication somehow. It does work with a USB to RS485 converter, but I do not want to use it.
Here is a code sample:
Private Async Function ListAvailablePorts() As Task(Of Boolean)
Try
Dim aqs As String = SerialDevice.GetDeviceSelector("UART0") '"UART0") '"COM1")
Dim _portlist As DeviceInformationCollection = Await DeviceInformation.FindAllAsync(aqs, Nothing)
client = Await SerialDevice.FromIdAsync(_portlist(0).Id)
client.ReadTimeout = TimeSpan.FromMilliseconds(1000)
client.WriteTimeout = TimeSpan.FromMilliseconds(1000)
client.BaudRate = 9600
client.Parity = SerialParity.None
client.DataBits = 8
client.StopBits = SerialStopBitCount.One
client.Handshake = SerialHandshake.None ' SerialHandshake.None ' SerialHandshake.XOnXOff ' SerialHandshake.RequestToSend
client.IsRequestToSendEnabled = False ' error here when using UART
client.IsDataTerminalReadyEnabled = False
Return True
Catch ex As Exception
Debug.WriteLine("Error listing ports: " & ex.Message)
Return False
End Try
End Function

The error you are receiving can happen in various use cases. But I'm guessing the device capapability isn't set for serial access.
Make sure to enable the serial communication device capability in the package.appxmanifest.
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
</Capabilities>
UWP on Raspberry Pi will not produce a proper SerialDevice.GetDeviceSelector when using
SerialDevice.GetDeviceSelector("COM0")
USB-Serial devices don't show as COM ports on Window IoT Core like they do on Windows 10 Desktop.
Also, if you are attempting to use a serial device OTHER than the onboard UART, or an FTDI serial device, it will not work unless you get drivers for the Pi installed on the Pi. Which is unlikely, as I have yet to find a manufacturer other than FTDI provide drivers for Windows IoT Core. (ala CH340)
The Windows.Devices.SerialCommunication namespace is flaky at best. I am willing to accept it that my troubles are of my own design but with the lack of support it's like pulling teeth to get a solution stable. Every iteration of enhancement provides their own set of difficulties.
Here's what I've learned over the past week on the subject of Serial Communication on the Raspberry Pi.
The serial device that you can communicate with must be an FTDI device.
You need to work around GetDeviceSelector and FindAllAsync short comings. (no "COM1") selector.
DataReader.LoadAsync will await until the ENTIRE bufferLength you provide has been filled.
Therefore, processing a buffer length of 1, provides the best way to get data of any length.
DataReader.LoadAsync will await forever the first time after you write something to the serial device. Provide a cancellation token and try again. (Note this is probably my own shortcoming/lack of understanding)
When you are done writing to the device - detach the writer's stream before attempting to read and vice versa.
And a big one - you must keep the serialDevice open for as long as you need it. DO NOT attempt to Dispose of the SerialDevice until your program closes - it will hang.
Here's how I initialize an FTDI USB-Serial device.
var aqs = SerialDevice.GetDeviceSelector();
var devices = await DeviceInformation.FindAllAsync(aqs);
var deviceInfo = devices?.Where(i => i.Name == "FT232R USB UART").FirstOrDefault();
What I did was inspected ALL devices and chose a unique property to select my device. Supposedly, using SerialDevice.GetDeviceSelectorFromUsbVidPid should work, but it did not for me.

Related

Serial communication with Velleman relais card

I am trying to integrate a Velleman relay card VM8090 into my vb.net project. Unfortunately Velleman only provides a 32bit DLL, but our program is compiled x64. Therefore I try the communication directly over the serial interface (USB).
This is the protocol description from Velleman.
Communication with the demo app provided by Velleman works perfectly. After setting a relaystate I get a response from the relay card. This is displayed in the demo app.
The crazy thing is: after opening the serial interface in vb.net I don’t get any traffic infos in the demo app (and vb.net also) anymore. But I need this relais-status-infos (51h). But even though no more data is sent from the card, I can still turn relays on and off.
This code already causes the card to stop sending info.
myComPort = New SerialPort("COM3") With {
.BaudRate = 19200,
.DataBits = 8,
.Parity = Parity.None,
.StopBits = 1,
.Handshake = Handshake.None,
.ReadBufferSize = 1024,
.WriteBufferSize = 1024}
myComPort.Open()
myComPort.Close()
Make sure that only 1 program uses the SerialPort, usually only 1 program can read and send data through a SerialPort.
Also check if the device is connected to the correct usb port (usb port 3 in this case).
Check if all SerialPort settings are set correctly
Finally, in your code the comPort is opened and immediately closed again
myComPort.Open() myComPort.Close()
Only close the comPort when you want to close the program or the communication between your computer and the hardware.
I hope this helped.

Issues communicating with devices over usb hub

I'm facing issues when communicating with devices over USB hub. When enumerating devices directly to host port, it does work, some devices over usb hub have issues.
Setup: STM32F103C8 - MAX3421E - LUFA (usb stack) (ported to MAX3421E (host) and STM32F103C8T6 (device)) - USB Full-Speed setup
Scenario:
When I attach device directly to host, I don't experience any issues enumerating almost all (some devices seems to be faulty and have weird/nonstandard behavior) devices. But when I try to enumerate over usb hub, devices starts to behave very strangely. I'm receiving much more NAK's from devices than when connected directly to host. Some devices are able to return Device Descriptor, but retrieving Configuration Descriptor fail. Some devices return Toggle Error after several NAK's, this could be remedied so far by delaying retry IN token. Also there is different behavior of devices when connected over different hubs. I.e. one device has no problems when connected to HUB1, but have issues when connected to HUB2. Then I have HUB3 (7 port) which internally acts as HUB in HUB. On this HUB3 device working fine on port behind secondary internal hub, but not on primary ports exposed over "root" hub.
I'm in suspicion that hub's TT could be somehow interfering with usb communication, but according to information I have found, TT should not be enabled under Full-Speed setup.
I have checked (many times) that I'm setting correct device address assigned during SetAddress phase (which is proved by returning Device Descriptor). When I step debug it seems that I can get Configuration Descriptor also, but while in normal system run, it isn't retrieved, but only over hub.
Does anyone has any ideas, what to look after? I've run out of ideas here after week of trying to find a root cause.
Thanks
so...
- as usual after searching for root cause, solution after days of trying comes naturally after asking on somewhere (this is hapenning to me always, but I do try prior asking always)
- when using hubs, make sure you don't suspend SOF generation during control transfers. LUFA just resume bus inside control transfer routines, so make sure you don"t stops and reenable SOF within (my fault as I'm using ported version to MAX)
- if you have tight main loop make sure you don"t reinitialize usb transfer without completion of previous try, but if you do so, check you don't owerwrite data which haven"t been processed yet fully (especially when using interrupt-driven transfer complete processing) [things seems to work when you have quite some debug output, as it delay that time critical transfers]
Enumeration over hub isuues are now second to none. Small glitches are subject for tweaking.
Unfortunately as I was in question for electrical issues, I had to unsolder usb host shield and soldered another one, which in light of new information seems unneeded. Nevermind, I have trained my soldering skills.

stm32f3 'USER USB' not detected

I'm working on stm32f3 discovery board. For my current project I planned on coding a simple UART program on the board, I happened to read somewhere on the internet that to use the board for UART communication with PC the 'USER USB' has to be used.
My questions are the following
Firstly when i connect my board to PC using the 'USER USB'. The PC refuses to recognize the board.(F.Y.I- OS used is windows 7 64bit). This problem persists even after I update the driver! How to resolve this?
Is the "Connecting USER USB" part correct? because I can't see any RS232 chip on board
Should I use an add on board?
P.S I've installed all the necessary drivers from ST website and works flawlessly while using 'ST-Link interface' part of the board
I started working with the STM32F3 disco a few weeks ago. The user USB thing is a bit tricky.
To get it clear: The board has 2 USB ports.
One mostly for programming and debugging. It connected to a second ST chip which works as programming adapter.
The seconds (called USER USB) is connected to the STM32F303 chips you program
So what you can do is to configure your STM32 to implement a CDC over USB. With the windows driver installed (automatic or from ST, depends on OS version) you should get a virtual com port in your device manager. If you don't have the driver, you also get a device labeled as virtual com port but with a yellow exclamation mark.
So if I get you question right, this is what you're trying to do? Then yes, "Connecting USER USB" is correct. No, you don't need an extension board.
How to resolve that? It depends:
Do you already see the device in your device manager as communication device or serial interface or something? Then only the CDC driver from ST is missing.
Or do you get an "Unknown Device" in your device manager? That is what I experienced.
In STM32CubeMX you need to enable the USB device (Peripehrals->USB)
In STM32CubeMX you need to select the USB stack (MiddleWares->USB_DEVICE->Communication Device Class)
Generate the code. If you are using HSI as clock source CubeMX will generate a error message you can ignore for now. For a real product you should use an external clock.
Note that there is a bug in CubeMX version 4.20. The code generator generates code which may not work if you select HSE as clock input source. Even if you go back to HSI, the error remains.
Compile an run the code. You maybe need to reset the board removing BOTH USB plugs
Windows should detect the board now.
Extra problem: The board is ... well. Tricky. If you only plug in the USER USB to your PC, it should work but Windows may also inform you that you have an UNKNOWN DEVICE because enumeration has failed.
Remove the plug again. Now, first plug in the other, ST-LINK USB to power on the board. Wait 1-2 seconds. Now plug in the USER USB. There seems to be a startup problem. Only using the USER USB for power supply and CDC seems to run in a race condition between boot and USB and USB enumeration fails. First powering the board and then plugging in removes this race condition (at least at my board)
Now Windows should enumerate the device and offer you a virtual com port. Actually you should get two of them: One "ST... STLink Virtual COM Port" and one "ST... Virtual COM Port". The seconds one is the one you are looking for.
I hope that was the answer on the question you had.
Thanks for the post, had the same issue, here is what I added
Approach: Turn-off USB lines programmatically by changing input to output, forcing it to zero and reenable them after around 1 second
Also used power-off and on from ST USB Libs
// Force Re-Enumeration by the USB Host
//
int aux_retrigger_usb()
{
GPIO_InitTypeDef GPIO_InitStructure;
// ST USB Function
PowerOff();
// Program Pin 12 USB_DP from Input to Output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Set USB_DP to 0
GPIOA->BRR |= GPIO_Pin_12 ;
// Wait for for minimum 32* 50msec , 1,6 secs
while(tim3_cnt%32!=0) ; while(tim3_cnt%32!=31) ;
// ST USB Functions
PowerOn();
USB_Init();
Virtual_Com_Port_Reset() ;
// Program Pin 12 USB_DP from Output to input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
return 1 ;
}
So basically to avoid the power on race condition, the ports may be programmed
first to outputs and then released after 1 second.
I'm also struggling with the STM32F302. The USB is not recognized by the HUB Down stream port, and therefore does not appear in a device manager on PC(HUB's upstream port is connected to PC).
So, I made a deep search after I checked all the HW and SW.
It appears that 1.5Kohm resistor should be added on a DP, as F302 does not have the embedded one.
My design does not have neither, so hopefully, it will solve the issue tomorrow, so I have a hope for tonight)
You can read AN4879 by ST - the place I found these directions.

How to connect an OutputStream to an UsbDeviceConnection in Android?

I need to connect an Android device to a thermal printer (DDP-250) thru an USB cable.
The Android device has USB Host capabilities.
The thermal printer has an USB interface.
So far, I'm already able to create an UsbDeviceConnection to the printer.
The tricky part is that the printer's SDK works based on an OutputStream to send data to the device.
From the javadoc:
Printer(java.io.OutputStream out)
Constructs a new instance of this class from a given OutputStream.
This means that sending data using UsbDeviceConnection.bulkTransfer is not an option.
I need to, somehow, get an OutputStream that streams data to the UsbDeviceConnection.
I even thought about using UsbDeviceConnection.getFileDescriptor to create a FileOutputStream, but UsbDeviceConnection.getFileDescriptor just returns an integer.
Oh, just to be clear, i know this printer has a bluetooth interface. But while using it, i was having wifi instability problems. And i need to have access to both, wifi internet and the printer, at the same time. I also tried to connect the android device to the internet using an usb/ethernet adapter (in this case the thermal printer would stay connected thru Bluetooth), but this android device does not have ethernet capabilities.
Any ideas? Anything at all. I'll take anything, even the recommendation of another thermal printer.
Thanks!
file descriptor (FD) is an abstract indicator for accessing a file.
so I think following statement will work.
public FileOutputStream (FileDescriptor fd);
Constructs a new FileOutputStream that writes to fd.

Serial port communication

i'm creating a windows form to send/receive data to/from serial port.
At first : i send the data as string to the serial port .
Second: i tried to read the string again for test the successfull transmission , but i recieved empty string
this is my code :
Try
Dim SerialPort1
As New SerialPort("Com1",9600, Parity.None, 8, StopBits.One)
SerialPort1.Open()
SerialPort1.DtrEnabled=True
SerialPort1.WriteLine("This is my test message ." )
' ================= Read from serial port
Label1.Text=SerialPort1.ReadExisting() ' this returns empty string
Catch ex As Exception
MessageBox.Show(
"Error writing to serial port:" & ex.Message)
Finally
SerialPort1.Close()
End Try
i need to ask another question:
is it required to connect device to serial port to send/recieve data successfully ????
please i need an urgent help
thanks
As Heinzi already mentioned, if you want to see data you need to have some coming in. There is no automatic echo of data you send out.
To answer your other question: Yes, you need to have another device connected to your serial port in order to send/receive data successfully. With no other device, what would be the point?
Fortunately for you, the "device" you connect can be as simple as a plug with some wires. Here is a set of instructions and diagrams for building a so-called loopback plug: http://www.airborn.com.au/serial/rs232.html
This will allow you to echo your output to your input using very simple hardware. If you're not into soldering up your own plug, you can use a so-called breakout box or board. Here's an example: http://www.breakoutboxes.com/D-Series-9-Position-Breakout-Board_p_31.html .
ReadExisting returns the data sent by the device you are communicating with, not the data sent by you.
You could try to use com0com for generating a virtual serial port pair, then you can rename one of these virtual ports to common name like "COM4". You should open other SW like hyperterminal for serial communication, then set to open the remaining port from the pair.
You may try to use com0com fist with two hyperterminals.
You will need a device through which your application will send and receive data.
For testing, you might consider creating a virtual serial port with software like this Virtual Serial Port Driver. It allows you to create serial ports that aren't actually connected to any physical device. You can then debug your program with another program or with something like HyperTerminal or PuTTY.
Assuming you have a loopback plug (simply connect pins 2 and 3 together) so that anything you send is immediately received.
However - you need to consider that the RS232 is slowly. ("S L O W L Y") and the transfer happens asynchronously so your program is hitting the readexisting long before the data has even been sent. So you're reading an empty buffer.
Just for the sake of your test you need to put some delay in there. so that you can wait a moment after sending before trying to receive.
In a real application though you'd use the receive event to read the serial port.