*/
Stuck? Need help? Ask questions on our forums.
*/

Read Post

C#

Moderators: None (Apply to moderate this forum)
Number of threads: 1943
Number of posts: 4193

This Forum Only
Post New Thread

Report
How to read messages from an RS 232? Posted by mouthpiec on 5 Jul 2008 at 2:39 AM
Hi,

I am writing a program that will connect to a modem via an RS232 port, and send commands, like for example to start a call.

Also I will need to read the feedback given from the modem, and by the code I have below, nothing is happening.

Can someone please help?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;


namespace IREG_Tester_V2
{
    class Program
    {
        static SerialPort port1 = new SerialPort();
        static bool _continue = false; 

        static void Main(string[] args)
        {
            //Create Seriel Port Settings
                        
            port1.PortName = "COM3";
            port1.BaudRate = 9600;
            port1.Parity = Parity.None;
            port1.DataBits = 8;
            port1.StopBits = StopBits.One;



            // Open the port for communications 
            port1.Open();

            //Start Call
            Console.WriteLine("Enter Number to Dial and press Enter");
            string ip = Convert.ToString(Console.ReadLine());
            port1.WriteLine("ATD" + ip + ";");
            Console.WriteLine(DateTime.Now.ToLongTimeString());


            //Console.ReadLine();

            int pauseTime = 9000;
            System.Threading.Thread.Sleep(pauseTime);
            // During the 9 seconds pause, answer the call, and 
            // a messege OK should be received



            //Disconnect Call
            port1.WriteLine("ATH");
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            Console.WriteLine("Call Disconnected!");

            
            Console.WriteLine("Press enter to Exit!");
            Console.ReadLine();

            // Close the port 
            port1.Close();
        }


             public void SerialPortProgram() 
             { 
                 Console.WriteLine("Incoming Data:"); 
                 
                 // Attach a method to be called when there is data waiting in the port's buffer 
                 port1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); 
             } 
        
            private void port_DataReceived(object sender,SerialDataReceivedEventArgs e) 
            { 
            // Show all the incoming data in the port's buffer 
                Console.WriteLine(port1.ReadExisting()); 
            }

    }
}
Reply




corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.