Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: Reading from serial port Pin
alfie.max1524-Oct-13 8:18
alfie.max1524-Oct-13 8:18 
AnswerRe: Reading from serial port Pin
OriginalGriff24-Oct-13 8:08
mveOriginalGriff24-Oct-13 8:08 
GeneralRe: Reading from serial port Pin
alfie.max1524-Oct-13 8:58
alfie.max1524-Oct-13 8:58 
GeneralRe: Reading from serial port Pin
OriginalGriff24-Oct-13 9:07
mveOriginalGriff24-Oct-13 9:07 
GeneralRe: Reading from serial port Pin
alfie.max1524-Oct-13 9:13
alfie.max1524-Oct-13 9:13 
AnswerRe: Reading from serial port Pin
OriginalGriff24-Oct-13 9:51
mveOriginalGriff24-Oct-13 9:51 
GeneralRe: Reading from serial port Pin
alfie.max1524-Oct-13 19:12
alfie.max1524-Oct-13 19:12 
GeneralRe: Reading from serial port Pin
OriginalGriff24-Oct-13 21:50
mveOriginalGriff24-Oct-13 21:50 
First off, stop opening and closing it - just leave it open. That ensures that nothing else can get at the port, since serial ports need exclusive locks.

ReadLine is also unhelpful - it will always block until a newline is received, so if whatever device is on the other end of the connection doesn't use the same newline as your PC, it will never return.
So, try this:

C#
bool Receive_Enable = false;

public Terminal (SerialPort Serial_Port)
        {
            InitializeComponent ();
            _SerialPort = Serial_Port;
            _SerialPort.Open (); // **** IF IT ISN'T ALREADY OPENED BY THE CALLING CODE.
            _SerialPort.DataReceived += new SerialDataReceivedEventHandler (Port_DataReceived);
        }

private void Start_Click (object sender, EventArgs e)
        {
            if(Start.Text == "Start")
            {
                Start.Text = "Stop";
                Receive_Enable = true;
            }
            else
            {
                Receive_Enable = false;
                Start.Text = "Start";
            }
        }

private void Port_DataReceived (object sender, SerialDataReceivedEventArgs e)
        {
            if(Receive_Enable)
            {
                int bytes = _SerialPort.BytesToRead;
                byte[] data = new byte[bytes];
                _SerialPort.Read(data, 0, bytes);
                string s = System.Text.Encoding.ASCII.GetString(data);
                Console.WriteLine(s);
                rtbTerminal.AppendText(s);
                rtbTerminal.ScrollToCaret();
            }
        }
You may want to play with the Encoding later, but that should be pretty safe.

The Console output is just for debugging - it shows you what is received, in what chunks.
The only instant messaging I do involves my middle finger.

English doesn't borrow from other languages.
English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

GeneralRe: Reading from serial port Pin
alfie.max1525-Oct-13 1:30
alfie.max1525-Oct-13 1:30 
AnswerRe: Reading from serial port Pin
PIEBALDconsult24-Oct-13 17:32
mvePIEBALDconsult24-Oct-13 17:32 
QuestionI need help with control inheritance Pin
deebow24-Oct-13 4:39
deebow24-Oct-13 4:39 
AnswerRe: I need help with control inheritance Pin
OriginalGriff24-Oct-13 5:02
mveOriginalGriff24-Oct-13 5:02 
GeneralRe: I need help with control inheritance Pin
BillWoodruff24-Oct-13 6:07
professionalBillWoodruff24-Oct-13 6:07 
AnswerRe: I need help with control inheritance Pin
BillWoodruff24-Oct-13 6:02
professionalBillWoodruff24-Oct-13 6:02 
GeneralRe: I need help with control inheritance Pin
OriginalGriff24-Oct-13 6:25
mveOriginalGriff24-Oct-13 6:25 
GeneralRe: I need help with control inheritance Pin
deebow24-Oct-13 6:44
deebow24-Oct-13 6:44 
GeneralRe: I need help with control inheritance Pin
BillWoodruff24-Oct-13 16:56
professionalBillWoodruff24-Oct-13 16:56 
Questionhow to create stetmen foreach gridcontrol devexpress Pin
Member 1027082523-Oct-13 23:19
Member 1027082523-Oct-13 23:19 
QuestionRe: how to create stetmen foreach gridcontrol devexpress Pin
Richard MacCutchan23-Oct-13 23:27
mveRichard MacCutchan23-Oct-13 23:27 
AnswerRe: how to create stetmen foreach gridcontrol devexpress Pin
OriginalGriff23-Oct-13 23:54
mveOriginalGriff23-Oct-13 23:54 
AnswerRe: how to create stetmen foreach gridcontrol devexpress Pin
Eddy Vluggen24-Oct-13 0:30
professionalEddy Vluggen24-Oct-13 0:30 
QuestionRoulette wheel selection Pin
Member 995886723-Oct-13 17:16
Member 995886723-Oct-13 17:16 
AnswerRe: Roulette wheel selection Pin
Abhinav S23-Oct-13 20:24
Abhinav S23-Oct-13 20:24 
GeneralRe: Roulette wheel selection Pin
Dave Kreskowiak24-Oct-13 3:21
mveDave Kreskowiak24-Oct-13 3:21 
GeneralRe: Roulette wheel selection Pin
Abhinav S24-Oct-13 3:51
Abhinav S24-Oct-13 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.