I'm trying to read the input values of port com
, in specific COM3
, but it does not work.
I tried the examples provided by the MS page, about the ports, but these simply do not receive data ..
I have tried several codes, and demos, but none seems to receive the data: 1.- link 2.- link
These are some examples, that I have based, and I see that the majority uses the event of SerialDataReceivedEventHandler(port_DataReceived);
....
I have tried emulators, which communicate for example com1
to com2
. and these do work. but already receive the data of com3
, no.
Someone who has handled or resolved this?
Edited
I am connected to a digital scale, which uses the COM3 port The image that I am adding are the parameters that require for the connection in hyperterminal. basically those are manually configured to the "SerialPort" component.
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lectura
{
class Program
{
static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DtrEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
/*
while (true) {
if (mySerialPort.BytesToRead > 0) {
byte b = (byte)mySerialPort.ReadByte();
Console.WriteLine(b);
}
}*/
//byte b = (byte)mySerialPort.ReadByte();
//char c = (char)mySerialPort.ReadChar();
//string line = mySerialPort.ReadLine();
//
//Console.WriteLine("byte "+ b);
//Console.WriteLine("char " + c);
//Console.WriteLine("line " + line);
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
print(indata);
//Console.WriteLine("Data Received:");
// Console.Write(indata);
}
private static void print(string data) {
Console.WriteLine(data);
}
}
}
and this is the example provided on the ms page about reading the "System.IO.Ports" ports. Greetings