0

I have a little windows form App which I'm using to send texts out to Customers using AT Commands, However we need a way to see the texts the customers will be sending back to us, I've already attempted to read data from the virtual Serial port I'm using and place that information in a text box, but this code is non-functional; I click the button on my form and nothing happens.

code is below, and any help whatsoever would be greatly appreciated.

public partial class MailBox : Form
    {

        public MailBox()
        {
            InitializeComponent();
        }
 private SerialPort _serialPort2 = new SerialPort("COM5", 115200);
    private void MailBox_Load(object sender, EventArgs e)
    {
        _serialPort2.Open();
        _serialPort2.Write("AT+CMGR=1\r");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = _serialPort2.ReadExisting();
        // _serialPort2.Close();
        // _serialPort2.DataReceived += new SerialDataReceivedEventHandler(_serialPort2_DataReceived);
    }
Captain_Custard
  • 1,308
  • 6
  • 21
  • 35
  • You need to add some more information. Where do you declare the `_serialPort2` object? What libraries are you using? – Maurice Reeves May 12 '14 at 12:44
  • My apologies! I declare the _serialport2 object just after my InitializeComponent(), I've updated my code accordingly – Captain_Custard May 12 '14 at 12:45
  • No worries. Just hard to figure out what may be wrong. Did you look at the MSDN article about using the SerialPort object? http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx – Maurice Reeves May 12 '14 at 12:48
  • I didn't actually, that's most useful! I wonder if I'm using the right AT Command to get the SMS messages from the GSM modem as that might be part of the problem as well – Captain_Custard May 12 '14 at 12:51
  • 1
    the DataRecieved event seems to be key in solving this, however when I attempted to raise the event for some reason it wouldn't fire when I subscribed to it, hence it is commented out – Captain_Custard May 12 '14 at 12:52
  • Most likely. I'm not terribly familiar with serial ports, but yes, the DataReceived event looks to be where you're going to get the data. I think if you emulate what's in the MSDN article you'll be most of the way there. – Maurice Reeves May 12 '14 at 13:03
  • I've managed to fix it and get the information I want, but now it's throwing an InvalidOperationException, Additional information: Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on. any ideas on that? – Captain_Custard May 12 '14 at 13:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52520/discussion-between-maurice-reeves-and-reece-cottam) – Maurice Reeves May 12 '14 at 13:10

0 Answers0