Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read the serial port with windows 10 core

Using Python on the Raspberry PI, I use similar code to what is shown below to read data from the serial port:

baud = 9600                 # baud rate
port = '/dev/ttyACM0'       # serial URF port on this computer

ser = serial.Serial(port, baud)
ser.timeout = 0 
var message = ser.read(9);

Essentially I just want to be able to read a message of the serial port and perform an action based on that message.

How can this be achieved using Windows 10 Core and c#, can anyone point me in the right direction or provide a code sample?

like image 524
Code Junkie Avatar asked May 12 '15 06:05

Code Junkie


1 Answers

it turns out that the Serial Port on the PI is not supported yet, which is very frustrating: https://www.raspberrypi.org/forums/viewtopic.php?t=109047&p=751638

Here is the supported way:

serialPort = await SerialDevice.FromIdAsync(comPortId);

serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); 

serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); 

serialPort.BaudRate = 115200;

serialPort.Parity = SerialParity.None; 

serialPort.StopBits = SerialStopBitCount.One; 

serialPort.DataBits = 7; 

serialPort.Handshake = SerialHandshake.None; 

serialPort.IsRequestToSendEnabled = true; 

dataReaderObject = new DataReader(serialPort.InputStream);

  // 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)
            {
                try
                {
                    var msg = dataReaderObject.ReadString(bytesRead);
                }
                catch (Exception ex ) {
                }
}            
like image 69
Code Junkie Avatar answered Oct 13 '22 00:10

Code Junkie