none
How to fix it ? RRS feed

  • Question

  • after several questions in this forum, and i wrote a little code

    image : farm5.staticflickr.com/4207/35233583052_0fd1c80d3c_o.png

    Imports System.IO
    Imports System.IO.Ports
    Imports System.Threading
    Imports System.Text
    Imports Microsoft.Office.Interop.Excel
    Imports Microsoft.Office.Interop
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim fname As String = "e:\fone.csv"
            Dim colsexpected As Integer = 2
            Dim thereader As New StreamReader(fname, Encoding.Default)
            Dim sline As String = ""
            Do
                sline = thereader.ReadLine
                If sline Is Nothing Then Exit Do
                Dim words() As String = sline.Split(",")
                DataGridView1.Rows.Add("")
                For ix As Integer = 0 To 1
                    DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(ix).Value = words(ix)
                Next
                If Not IsNumeric(words(0)) Then DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Style.BackColor = Color.Yellow
            Loop
            thereader.Close()
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Start()
            Try
                Dim ports As String() = SerialPort.GetPortNames
                Dim port As String
                For Each port In ports
                    ComboBox1.Items.Add(port)
                Next port
                ComboBox1.SelectedIndex = 0
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Try
                With SerialPort1
                    .PortName = ComboBox1.Text
                    '.BaudRate = 9600
                    .BaudRate = 115200
                    .Parity = Parity.None
                    .StopBits = StopBits.One
                    .DataBits = 8
                    .Handshake = Handshake.RequestToSend
                    .DtrEnable = True
                    .RtsEnable = True
                    .NewLine = vbCrLf
                    .Open()
                End With
                Dim c As Int32 = 0
                Dim itemlist As New List(Of String)
                For i As Integer = 0 To DataGridView1.RowCount - 1
                    itemlist.Add(DataGridView1.Rows(i).Cells("PHONE").Value)
                Next
                For Each number In itemlist
                    Try
                        If SerialPort1.IsOpen Then
                            With SerialPort1
                                .Write("AT" & vbCrLf)
                                .Write("AT+CMGF=1" & vbCrLf)
                                .Write("AT+CMGS=" & Chr(34) & number & Chr(34) & vbCrLf)
                                .Write(RichTextBox1.Text & Chr(26))
                                c += 1
                                'MsgBox("OK")
                            End With
                        Else
                            MsgBox("Erro!")
                        End If
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                    Label3.Text = c.ToString()
                    'Thread.Sleep(4000)
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub  
    End Class

    there is no error, but it don't work, the code don't send SMS, I don't know why ? Pls fix it, thank you very much and sorry about my English.


    Monday, June 19, 2017 8:21 AM

All replies

  • Hi

    You say 'after several questions in this forum,', so you must already be familiar with the need to put code in a CODE BLOCK - yet you didn't!


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, June 19, 2017 12:15 PM
    • Marked as answer by phamduyhoang Monday, June 19, 2017 12:16 PM
    • Unmarked as answer by phamduyhoang Monday, June 19, 2017 12:56 PM
    Monday, June 19, 2017 12:14 PM
  • Hi

    You say 'after several questions in this forum,', so you must already be familiar with the need to put code in a CODE BLOCK - yet you didn't!


    Regards Les, Livingston, Scotland


    Hi

    Much better - thank you.

    BTW: please unmark my post as Answer - my post did not answer your question.


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, June 19, 2017 12:29 PM
    Monday, June 19, 2017 12:18 PM
  • Hi phamduyhoang,

    About sending SMS event, you could have some error, I find one thread about sending SMS, please refer to :

    https://stackoverflow.com/questions/6765774/how-to-send-sms-from-vb-net

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 20, 2017 2:38 AM
    Moderator
  • This code is work if I input one number in textbox1.

    Now I import phone number from .csv file, and loop throught datagrid but it didn't work.

    Tuesday, June 20, 2017 7:55 AM
  • This code is work if I input one number in textbox1.

    Now I import phone number from .csv file, and loop throught datagrid but it didn't work.

    Have you checked that the phone numbers in the CSV file are correct?

    You are writing the values into a DGV then accessing them from the DGV. That creates a risk of corruption.  Place the values into ItemList from the same source as you are updating the DGV. 

    Insert a breakpoint at the start of the loop.  When the code stops at that point, make sure the phone number accessed from the list is correct. If it has been converted to a number and back to a string then it has likely been changed. If it is OK, then step the code through one line at a time to identify the point at which it fails.

    If the first call is OK but the next one fails, then that probably means that you are not terminating the first call correctly, and the modem is not ready to start the next call.

    Tuesday, June 20, 2017 8:20 AM
  • Thank you, I wish I could do that, but I don't know, cause I just search and code it, I have no experience VB, after 2 weeks search google, I can send SMS but just 1 receiver in a textbox, and I can import .csv file to Datagrid and loop, but i don't work, anyway, thank you very much.


    Thursday, June 22, 2017 4:01 AM
  • Thank you, I wish I could do that, but I don't know, cause I just search and code it

    You can't write VB code without knowing how to debug it.  Start here:
    https://vbdotnetblog.wordpress.com/overview/debugging/

    To avoid corruption caused by saving the phone numbers in a DGV, change this:

           Do
                sline = thereader.ReadLine
                If sline Is Nothing Then Exit Do
                Dim words() As String = sline.Split(",")
                DataGridView1.Rows.Add("")
                For ix As Integer = 0 To 1
                    DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(ix).Value = words(ix)
                Next
                If Not IsNumeric(words(0)) Then DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Style.BackColor = Color.Yellow
            Loop
            thereader.Close()
    
    ...
    
                Dim itemlist As New List(Of String)
                For i As Integer = 0 To DataGridView1.RowCount - 1
                    itemlist.Add(DataGridView1.Rows(i).Cells("PHONE").Value)
                Next

    to

    Dim itemlist As New List(Of String)
    
    ...
    
           Do
                sline = thereader.ReadLine
                If sline Is Nothing Then Exit Do
                Dim words() As String = sline.Split(",")
                DataGridView1.Rows.Add("")
                For ix As Integer = 0 To 1
                    DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(ix).Value = words(ix)
                    If Not IsNumeric(words(ix)) Then 
                        DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Style.BackColor = Color.Yellow
                    Else
                        itemlist.Add(words(ix))
                    End If
                Next
            Loop
            thereader.Close()
    I have assumed that you only want the numbers in the first two columns of the CSV file.





    • Edited by Acamar Thursday, June 22, 2017 5:21 AM cmnt
    Thursday, June 22, 2017 5:16 AM