UWP app slows, then crashes after certain number of serial writes - c#

I am making an application using Windows IoT Core on the Raspberry Pi 3.
I am trying to communicate with an Arduino using serial communication. I have used most of the methods shown in this example on GitHub for creating the serial device and writing to it: https://github.com/ms-iot/samples/tree/develop/SerialSample/CS.
The program runs fine for the first 130 bytes or so, but then will begin to slow down dramatically after that and crash around the 135th byte or so.
Sometimes it causes the Raspberry Pi to display the Windows 10 "blue screen of death" saying that it needs to restart.
I've tried rewriting the code many ways and using different variations of async, task, and await, but can't seem to get it to work.
Here's the code I use to initialize the serial device:
private async void setup()
{
serialPort = await initializeSerial();
}
private async Task<SerialDevice> initializeSerial()
{
try
{
string aqs = SerialDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(aqs);
for (int i = 0; i < dis.Count; i++)
{
if (dis[i].Name.Contains("Arduino Uno"))
{
var serialPort = await SerialDevice.FromIdAsync(dis[i].Id);
/* Configure serial settings */
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
// Display configured settings
//Debug.WriteLine("Serial port configured successfully.");
// Create cancellation token object to close I/O operations when closing the device
ReadCancellationTokenSource = new CancellationTokenSource();
return serialPort;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return null;
}
and here's the code I use to write to the serial device:
private async void sendString(string stringToSend)
{
try
{
if (serialPort != null)
{
// Create the DataWriter object and attach to OutputStream
dataWriteObject = new DataWriter(serialPort.OutputStream);
//Launch the WriteAsync task to perform the write
await WriteAsync(stringToSend);
}
else
{
status.Text = "Select a device and connect";
}
}
catch (Exception ex)
{
status.Text = "sendString: " + ex.Message;
}
finally
{
// Cleanup once complete
if (dataWriteObject != null)
{
dataWriteObject.DetachStream();
dataWriteObject = null;
}
}
}
/// <summary>
/// WriteAsync: Task that asynchronously writes data from the input text box 'sendText' to the OutputStream
/// </summary>
/// <returns></returns>
private async Task WriteAsync(string stringToWrite)
{
Task<UInt32> storeAsyncTask;
dataWriteObject.WriteString(stringToWrite);
// Launch an async task to complete the write operation
storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
counter++;
status.Text = stringToWrite + ", ";
status.Text += "bytes written successfully! (" + counter + ")";
}
}
Any help/advice would be appreciated.
Update
I rewrote my sendString method as this and simplified it using a using statement:
private async void sendString(string stringToSend)
{
using (DataWriter dataWriter = new DataWriter(serialPort.OutputStream))
{
dataWriter.WriteString(stringToSend);
await dataWriter.StoreAsync();
//await dataWriter.FlushAsync();
dataWriter.DetachStream();
counter++;
status.Text = stringToSend + ", ";
status.Text += "bytes written successfully! (" + counter + ")";
}
}
However, the problem still persists.

I tried using the UART pins instead of the USB cable to write data, and experienced no more issues. Here's the code I used to initialize the UART bus on the Pi and I used the same function to write as I used above:
private async Task<SerialDevice> initializeSerial()
{
try
{
string aqs = SerialDevice.GetDeviceSelector("UART0");
var dis = await DeviceInformation.FindAllAsync(aqs);
SerialDevice serialPort = await SerialDevice.FromIdAsync(dis[0].Id);
/* Configure serial settings */
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
status.Text = "Serial port configured succesfully.";
return serialPort;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
status.Text = "Serial port not configured successfully. Are you sure a serial device is connected?";
return null;
}
This doesn't exactly answer the problem with sending data over the USB cable, but I'm posting it here in case anyone has a similar problem with using the USB port and would like to try this instead.

Related

How to create a "DataReceived" Event for SerialDevice?

I am using the SerialDevice class to use RS-485 with my serial ports. It currently does not have an event for when data is received. And I currently have to poll a Listener with a DataReader which doesn't always capture all the data in its buffer.
I am looking for a way to create a receiver event for the SerialDevice class so that I don't have to poll and so that it can be triggered right when the correct data is received.
My current method:
async void Initialize()
{
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync();
if (devices.Count > 0)
{
for (int i = 0; i < devices.Count; i++)
{
if (devices[i].Id.Contains("PNP0501#2"))
{
GpioStatus = string.Format("Connecting... {0} found", devices[i].Id);
DeviceInformation deviceInfo = devices[i];
serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
serialDevice.BaudRate = 9600;
serialDevice.DataBits = 8;
serialDevice.StopBits = SerialStopBitCount.Two;
serialDevice.Parity = SerialParity.None;
serialDevice.Handshake = SerialHandshake.None;
}
}
}
Listen();
}
Initialize();
DataReader dataReader;
SerialDevice serialDevice;
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
const uint ReadBufferLength = 1024;
async void Listen()
{
try
{
dataReader = new DataReader(serialDevice.InputStream);
dataReader.InputStreamOptions = InputStreamOptions.Partial;
while (true)
{
await ReadAsync(cancellationTokenSource.Token);
}
}
catch
{
GpioStatus = "Failed to listen";
}
}
private async Task ReadAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Task<UInt32> loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
UInt32 bytesRead = await loadAsyncTask;
GpioStatus = dataReader.ReadString(bytesRead);
}
Any ideas would be much appreciated!
As it's a serial protocol, you need to interpret as things come in. If the first thing expected is (say) a 4 byte length, followed by the rest of the packet, then:
while (dataReader.UnconsumedBufferLength > 0)
{
// Note that the call to readString requires a length of "code units"
// to read. This is the reason each string is preceded by its length
// when "on the wire".
uint bytesToRead = dataReader.ReadUInt32();
receivedStrings += dataReader.ReadString(bytesToRead) + "\n";
}
Code snippet taken from : https://learn.microsoft.com/en-us/uwp/api/windows.storage.streams.datareader

Exception after reset serial connection to a different Baud rate

In my application I'm using a 9600 baud rate serial connection and I want to use a 115200 baud rate connection for data transfer.
I've disconnected from the old connection and set it to be null value, and set my serial connection to new connection with different baud rate.
The connection is unstable and I sometimes get a System.ObjectDisposedException - what did I miss?
The connection code
public string startConnection()
{
if (serial != null)
{
serial.Dispose();
}
foreach (string portname in SerialPort.GetPortNames())
{
serial = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
serial.ReadTimeout = 5000;
serial.WriteTimeout = 5000;
serial.Handshake = System.IO.Ports.Handshake.None;
serial.NewLine = "\n";
string received = "";
try
{
serial.Open();
serial.DiscardInBuffer();
serial.Write(":09;BATTERY;");
Thread.Sleep(500);
received = serial.ReadLine();
if (received.Contains(";BATTERY;V="))
{
status = SERIAL_CONNECTED;
return portname;
}
}
catch (Exception err)
{
try
{
serial.Close();
status = DISCONNECTED;
}
catch (Exception)
{
// throw;
}
}
}
throw new Exception("couldn't connect to coms");
//return "couldn't connect to coms";
//this.Close();
}
Disconnect function:
public void disconnect ()
{
if (serial == null || serial.IsOpen==false ||status == DISCONNECTED)
return;
status = DISCONNECTED;
serial.Close();
serial = null;
}
The main program is:
private async void BurnOFP_click(object sender, RoutedEventArgs e)
{
startConnection();
some actions.............
disconnect();
var t = new Task(() =>
{
try
{
myUswm.startModemConnection(); // same but with different baud rate
}
catch (Exception e2) { MessageBox.Show(e2.Message); }
});
t.Start();
t.Wait();
modem = new XMODEM_FullDotNET(myUswm.getSerialPort(), XMODEM_FullDotNET.Variants.XModemCRC);
buff = File.ReadAllBytes(softwareFilePath_Text.Text);
if (buff.Length < 1)
{
MessageBox.Show("ERROR : wrong OFP file");
return;
}
if (myUswm.prepareOFPBurning()) // sends u to start transfer
{
if (isBurning == false)
{
isBurning = true;
modem._ProgressSent = 0;
myProgBar = new myProgressBar(modem);
myProgBar.StartTransfer(modem, buff.Length);
myProgBar.Show(); // show window
// got the Exception here!!!!!!!!!!
var t3 = new Task(() =>
{
modem.Send(buff);
});
............
}
else
MessageBox.Show("burning in progress..");
}
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
Thanks for any help
RESOLVED
my problem was A bad timing caused by closing and reopen the same port.
I've found the information in MSDN Serial class:
The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.
my solution was keeping the connection alive and change the baud rate and update the connection status in my application manually.

Windows Universal App Serial Ports won't open, SerialDevice.FromIdAsync always null

I am trying to use serial ports in a Windows Universal Application. I have been using Microsoft's Serial Sample application as a template however I have hit a rather strange issue.
var dis = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelectorFromUsbVidPid(0x04D8, 0x000A));
var sp = await SerialDevice.FromIdAsync(dis[0].Id);
This is a the snippet that is failing. It is slightly altered from the original sample as it connects to the first device with a specific vendor. Now if I take this snippet and put it in the sample application produced by Microsoft the serial port opens and all is good. However when I move this code into my own project it always returns null from the method.
Here is the full MainPage.xaml.cs from the Microsoft sample with my alerations:
This Works!!
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
using Windows.Storage.Streams;
using System.Threading;
using System.Threading.Tasks;
namespace SerialSample
{
public sealed partial class MainPage : Page
{
/// <summary>
/// Private variables
/// </summary>
private SerialDevice serialPort = null;
DataWriter dataWriteObject = null;
DataReader dataReaderObject = null;
private ObservableCollection<DeviceInformation> listOfDevices;
private CancellationTokenSource ReadCancellationTokenSource;
public MainPage()
{
this.InitializeComponent();
comPortInput.IsEnabled = false;
sendTextButton.IsEnabled = false;
listOfDevices = new ObservableCollection<DeviceInformation>();
ListAvailablePorts();
}
/// <summary>
/// ListAvailablePorts
/// - Use SerialDevice.GetDeviceSelector to enumerate all serial devices
/// - Attaches the DeviceInformation to the ListBox source so that DeviceIds are displayed
/// </summary>
private async void ListAvailablePorts()
{
try
{
string aqs = SerialDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelectorFromUsbVidPid(0x04D8, 0x000A));
serialPort = await SerialDevice.FromIdAsync(dis[0].Id);
status.Text = "Select a device and connect";
for (int i = 0; i < dis.Count; i++)
{
listOfDevices.Add(dis[i]);
}
DeviceListSource.Source = listOfDevices;
comPortInput.IsEnabled = true;
ConnectDevices.SelectedIndex = -1;
try
{
// Disable the 'Connect' button
comPortInput.IsEnabled = false;
// Configure serial settings
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
// Display configured settings
status.Text = "Serial port configured successfully: ";
status.Text += serialPort.BaudRate + "-";
status.Text += serialPort.DataBits + "-";
status.Text += serialPort.Parity.ToString() + "-";
status.Text += serialPort.StopBits;
// Set the RcvdText field to invoke the TextChanged callback
// The callback launches an async Read task to wait for data
rcvdText.Text = "Waiting for data...";
// Create cancellation token object to close I/O operations when closing the device
ReadCancellationTokenSource = new CancellationTokenSource();
// Enable 'WRITE' button to allow sending data
sendTextButton.IsEnabled = true;
Listen();
}
catch (Exception ex)
{
status.Text = ex.Message;
comPortInput.IsEnabled = true;
sendTextButton.IsEnabled = false;
}
}
catch (Exception ex)
{
status.Text = ex.Message;
}
}
/// <summary>
/// comPortInput_Click: Action to take when 'Connect' button is clicked
/// - Get the selected device index and use Id to create the SerialDevice object
/// - Configure default settings for the serial port
/// - Create the ReadCancellationTokenSource token
/// - Start listening on the serial port input
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void comPortInput_Click(object sender, RoutedEventArgs e)
{
var selection = ConnectDevices.SelectedItems;
if (selection.Count <= 0)
{
status.Text = "Select a device and connect";
return;
}
DeviceInformation entry = (DeviceInformation)selection[0];
try
{
serialPort = await SerialDevice.FromIdAsync(entry.Id);
// Disable the 'Connect' button
comPortInput.IsEnabled = false;
// Configure serial settings
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
// Display configured settings
status.Text = "Serial port configured successfully: ";
status.Text += serialPort.BaudRate + "-";
status.Text += serialPort.DataBits + "-";
status.Text += serialPort.Parity.ToString() + "-";
status.Text += serialPort.StopBits;
// Set the RcvdText field to invoke the TextChanged callback
// The callback launches an async Read task to wait for data
rcvdText.Text = "Waiting for data...";
// Create cancellation token object to close I/O operations when closing the device
ReadCancellationTokenSource = new CancellationTokenSource();
// Enable 'WRITE' button to allow sending data
sendTextButton.IsEnabled = true;
Listen();
}
catch (Exception ex)
{
status.Text = ex.Message;
comPortInput.IsEnabled = true;
sendTextButton.IsEnabled = false;
}
}
/// <summary>
/// sendTextButton_Click: Action to take when 'WRITE' button is clicked
/// - Create a DataWriter object with the OutputStream of the SerialDevice
/// - Create an async task that performs the write operation
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void sendTextButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (serialPort != null)
{
// Create the DataWriter object and attach to OutputStream
dataWriteObject = new DataWriter(serialPort.OutputStream);
//Launch the WriteAsync task to perform the write
await WriteAsync();
}
else
{
status.Text = "Select a device and connect";
}
}
catch (Exception ex)
{
status.Text = "sendTextButton_Click: " + ex.Message;
}
finally
{
// Cleanup once complete
if (dataWriteObject != null)
{
dataWriteObject.DetachStream();
dataWriteObject = null;
}
}
}
/// <summary>
/// WriteAsync: Task that asynchronously writes data from the input text box 'sendText' to the OutputStream
/// </summary>
/// <returns></returns>
private async Task WriteAsync()
{
Task<UInt32> storeAsyncTask;
if (sendText.Text.Length != 0)
{
// Load the text from the sendText input text box to the dataWriter object
dataWriteObject.WriteString(sendText.Text);
// Launch an async task to complete the write operation
storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
status.Text = sendText.Text + ", ";
status.Text += "bytes written successfully!";
}
sendText.Text = "";
}
else
{
status.Text = "Enter the text you want to write and then click on 'WRITE'";
}
}
/// <summary>
/// - Create a DataReader object
/// - Create an async task to read from the SerialDevice InputStream
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void Listen()
{
try
{
if (serialPort != null)
{
dataReaderObject = new DataReader(serialPort.InputStream);
// keep reading the serial input
while (true)
{
await ReadAsync(ReadCancellationTokenSource.Token);
}
}
}
catch (Exception ex)
{
if (ex.GetType().Name == "TaskCanceledException")
{
status.Text = "Reading task was cancelled, closing device and cleaning up";
CloseDevice();
}
else
{
status.Text = ex.Message;
}
}
finally
{
// Cleanup once complete
if (dataReaderObject != null)
{
dataReaderObject.DetachStream();
dataReaderObject = null;
}
}
}
/// <summary>
/// ReadAsync: Task that waits on data and reads asynchronously from the serial device InputStream
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task ReadAsync(CancellationToken cancellationToken)
{
Task<UInt32> loadAsyncTask;
uint ReadBufferLength = 1024;
// If task cancellation was requested, comply
cancellationToken.ThrowIfCancellationRequested();
// Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
// Create a task object to wait for data on the serialPort.InputStream
loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
// Launch the task and wait
UInt32 bytesRead = await loadAsyncTask;
if (bytesRead > 0)
{
rcvdText.Text = dataReaderObject.ReadString(bytesRead);
status.Text = "bytes read successfully!";
}
}
/// <summary>
/// CancelReadTask:
/// - Uses the ReadCancellationTokenSource to cancel read operations
/// </summary>
private void CancelReadTask()
{
if (ReadCancellationTokenSource != null)
{
if (!ReadCancellationTokenSource.IsCancellationRequested)
{
ReadCancellationTokenSource.Cancel();
}
}
}
/// <summary>
/// CloseDevice:
/// - Disposes SerialDevice object
/// - Clears the enumerated device Id list
/// </summary>
private void CloseDevice()
{
if (serialPort != null)
{
serialPort.Dispose();
}
serialPort = null;
comPortInput.IsEnabled = true;
sendTextButton.IsEnabled = false;
rcvdText.Text = "";
listOfDevices.Clear();
}
/// <summary>
/// closeDevice_Click: Action to take when 'Disconnect and Refresh List' is clicked on
/// - Cancel all read operations
/// - Close and dispose the SerialDevice object
/// - Enumerate connected devices
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void closeDevice_Click(object sender, RoutedEventArgs e)
{
try
{
status.Text = "";
CancelReadTask();
CloseDevice();
ListAvailablePorts();
}
catch (Exception ex)
{
status.Text = ex.Message;
}
}
}
}
This next block if from my code. Those first 2 lines in theConnectToSerialPort() are the same as before for testing however SerialDevice.FromIdAsync(dis[0].id); is always null. This does not work!!
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
namespace OpenLab.Kitchen.Receiver
{
public sealed partial class MainPage : Page
{
private List<byte> Bytes { get; set; }
public MainPage()
{
this.InitializeComponent();
Bytes = new List<byte>();
ConnectToSerialPort();
}
private async void ConnectToSerialPort()
{
var dis = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelectorFromUsbVidPid(0x04D8, 0x000A));
var sp = await SerialDevice.FromIdAsync(dis[0].Id);
Debug.WriteLine(sp.UsbVendorId);
DeviceInformationCollection serialDevices;
while ((serialDevices = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelectorFromUsbVidPid(0x04D8, 0x000A))).Count < 1)
{
Debug.WriteLine("Unable to locate...");
}
SerialDevice serialPort;
while ((serialPort = await SerialDevice.FromIdAsync(serialDevices[0].Id)) == null)
{
Debug.WriteLine("Failed to open serial port...");
}
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
var dataReader = new DataReader(serialPort.InputStream);
var buffer = new byte[1024];
while (true)
{
var bytesRead = await dataReader.LoadAsync((uint)buffer.Length);
dataReader.ReadBytes(buffer);
Bytes.AddRange(buffer.Take((int)bytesRead));
byte[] slipPacket;
while ((slipPacket = Slip.ExtractSlipPacket(Bytes)) != null)
{
var waxPacket = WaxPacketConverter.FromBinary(slipPacket, DateTime.Now);
if (waxPacket != null)
{
Debug.WriteLine(waxPacket);
}
}
}
}
}
}
I have checked all the manifest permissions to match them up and checked stuff like referenced DLL versions and everything looks the same. Also checked things like running VS as admin and I haven't got both applications open at the same time so its not that one should be holding the port open or anything...
Does anyone have any ideas?
So Microsoft do not mention that you need to add something to the app manifest for serial communication nor is there a tick box in the app manifest GUI for serial communication.
The following needs to be added to your app manifest (create the <Capabilities> section if it doesn't exist):
<Capabilities>
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>
UPDATE 10/2016
Microsoft have updated their documentation to reflect this requirement.
https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.serialcommunication.aspx

Nmodbus and multiple slave

Im trying to make a Energy plant simulator and i like to simulate in my app few devices.
The idea is that the master request for something and the app ask to the needed slave and make something.
I just start using Nmodbus and all work fine with 1 device.
I tryed using SlaveId to ask for each device but when i read or write, i ever got all writed in the same Datastore.
The app and device run in localhost.
This is how i declare each device in the same network:
int port = 502;
IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
// create and start the TCP slave
slaveTcpListener = new TcpListener(address, port);
slaveTcpListener.Start();
slave1 = ModbusTcpSlave.CreateTcp(1, slaveTcpListener);
slave1.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave1.Listen();
slave2 = ModbusTcpSlave.CreateTcp(2, slaveTcpListener);
slave2.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave2.Listen();
And this is how i request data of each device:
using (TcpClient client = new TcpClient("127.0.0.1", 502))
{
ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
// read five input values
ushort startAddress = 0;
ushort numInputs = 10;
ushort[] inputs = master.ReadHoldingRegisters(1,startAddress, numInputs);
ushort[] inputs2 = master.ReadHoldingRegisters(2, startAddress, numInputs);
}
But the result is ever the Datastore of slave1.
Unfortunately, there is no way to run multiple slave devices, because slave.Listen() method includes infinite cycle:
public override void Listen()
{
while (true)
{
try
{
try
{
// read request and build message
byte[] frame = SerialTransport.ReadRequest();
IModbusMessage request = ModbusMessageFactory.CreateModbusRequest(frame);
if (SerialTransport.CheckFrame && !SerialTransport.ChecksumsMatch(request, frame))
{
string msg = $"Checksums failed to match {string.Join(", ", request.MessageFrame)} != {string.Join(", ", frame)}.";
Debug.WriteLine(msg);
throw new IOException(msg);
}
// only service requests addressed to this particular slave
if (request.SlaveAddress != UnitId)
{
Debug.WriteLine($"NModbus Slave {UnitId} ignoring request intended for NModbus Slave {request.SlaveAddress}");
continue;
}
// perform action
IModbusMessage response = ApplyRequest(request);
// write response
SerialTransport.Write(response);
}
catch (IOException ioe)
{
Debug.WriteLine($"IO Exception encountered while listening for requests - {ioe.Message}");
SerialTransport.DiscardInBuffer();
}
catch (TimeoutException te)
{
Debug.WriteLine($"Timeout Exception encountered while listening for requests - {te.Message}");
SerialTransport.DiscardInBuffer();
}
// TODO better exception handling here, missing FormatException, NotImplemented...
}
catch (InvalidOperationException)
{
// when the underlying transport is disposed
break;
}
}
That same limitation caused a problem for me, so I took over the NModbus4 project (now just NModbus):
https://github.com/NModbus/NModbus
public static async Task StartModbusSerialRtuSlaveNetwork()
{
using (SerialPort slavePort = new SerialPort("COM5"))
{
// configure serial port
slavePort.BaudRate = 19200;
slavePort.DataBits = 8;
slavePort.Parity = Parity.Even;
slavePort.StopBits = StopBits.One;
slavePort.Open();
//Create an instance of the ModbusFactory
IModbusFactory factory = new ModbusFactory();
//Create an adapter for the serial port
var adapter = new SerialPortAdapter(slavePort);
//Create the slave network
IModbusSlaveNetwork modbusSlaveNetwork = factory.CreateRtuSlaveNetwork(adapter);
//Create the slaves
IModbusSlave slave1 = factory.CreateSlave(1);
IModbusSlave slave2 = factory.CreateSlave(2);
//Add the slaves to the network
modbusSlaveNetwork.AddSlave(slave1);
modbusSlaveNetwork.AddSlave(slave2);
await modbusSlaveNetwork.ListenAsync();
await Task.Delay(1);
}
}

SerialPort.Write method hanging, timeout not fired in C#

I have to write a program that writes on a serial port but sometimes the call to the Write method hangs and the WriteTimeout is never fired so my program hangs indefinitely.
Here is the port creation code:
void DetectX1BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
String[] ports = SerialPort.GetPortNames();
int i = 0;
foreach (string PortName in ports)
{
try
{
Console.WriteLine("Trying to open:" + PortName);
SerialPort port = openSerial(PortName);
Console.WriteLine("Port is open:" + PortName);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Write("$ST+IMEI=0000\r\n");
if (IMEIFoundEvent.WaitOne(250))
{
Console.WriteLine("IMEI Found:[" + imei + "]");
if (addresses.ContainsKey(imei))
{
((BackgroundWorker)sender).ReportProgress(0, new X1Model(imei, PortName, addresses[imei]));
}
else
Console.WriteLine("imei not in file: " + imei);
}
port.Close();
}
catch (Exception ex)
{
Console.WriteLine("Erreur port " + PortName + ex.Message);
}
finally
{
i++;
((BackgroundWorker)sender).ReportProgress(i * 100 / ports.Length);
}
}
}
private SerialPort openSerial(string PortName)
{
SerialPort port = new SerialPort(PortName);
port.BaudRate = 57600;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.ReceivedBytesThreshold = 1;
port.Handshake = Handshake.None;
port.DtrEnable = true;
port.RtsEnable = true;
port.WriteTimeout = 5000;
port.ReadTimeout = 5000;
if (!port.IsOpen)
port.Open();
return port;
}
Is there anything I'm missing ?
I don't know if it's relevant but I'm using Serial To USB Adapters.
Edit: I'm using Windows XP with .Net 4.0. The line doesnt't exceed 50 characters and ends by a EOL character.
I know it's an old question, which you've probably solved by now, but there's no accepted answer yet. I was having the same issue yesterday and seem to have fixed it -- were you setting the Write Timeout?
_serialPort.WriteTimeout = 500;
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.writetimeout.aspx
You also need to set the PortName. You can get the list of ports from GetPortNames. This will typically be like COM1 or COM2

Categories