我在这段代码中错过了什么......它根本不起作用 [英] What Did I Miss In This Code That I Have..It Doesn't Work At All

查看:88
本文介绍了我在这段代码中错过了什么......它根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

For Each item As Object In ListBox1.Items
            Dim i As Integer = ListBox1.Items.Count - 1
            Try
                With SerialPort1
                    .Write("AT" & vbCrLf)
                    Threading.Thread.Sleep(1000)
                    .Write("AT+CMGF=1" & vbCrLf)
                    Threading.Thread.Sleep(1000)
                    .Write("AT+CMGS=" & Chr(34) & item & Chr(34) & vbCrLf)
                    Threading.Thread.Sleep(1000)
                    .Write(tx1.Text & Chr(26))
                    MsgBox("Sending Message")
                    Exit For
                End With

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        Next







我在这段代码中错过了什么?我尝试了所有我知道的但没有任何作品




What did i miss in this code?i tried everything i know but nothing works

推荐答案

请参阅我对qu的评论estion。这不是一个有效的问题;我回答的唯一原因是我记得你以前的问题。我仍然不知道你的失败或问题是什么,所以我只讨论一些明显的问题。



让我们看看:



Dim i As Integer = ListBox1.Items.Count - 1 从不使用。这是您之前的问题中您之前的代码示例中保留的行;在那个时候,它根本没有任何意义。最后,删除它。并且,来吧:你应该理解你写的每一行,否则你的整个工作都没有意义。您应该编写代码,而不是使用试错法。



正如您之前所要求的,您需要将列表框项的内容发送到序列港口。我不知道你的项目中有什么,但我想你只想发送每个项目中显示的文字。在这种情况下,请使用 item.ToString 。可以假设,但你做的事情在概念上并不正确;项目的类型是 System.Object ,因此它可以包含任何数据。在其他情况下,你应该将项目类型输入到真正的运行时类型(你知道那是什么吗?)并获得它携带的任何数据。



不要使用多个字符串/字符串联('&')。字符串是不可变的。我甚至要解释后果吗?无论如何,请改用string.Format:

http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx [ ^ ]。



上述问题告诉我们你对如何编写代码几乎没有任何想法,但并不是可能出现故障的直接原因(你再次无需解释)。 br />


我看不到串口是如何初始化的。它可以用错误的参数初始化:奇偶校验,波特率,握手等。请参阅: http://msdn.microsoft .com / zh-cn / library / system.io.ports.serialport%28v = vs.110%29.aspx [ ^ ]。



它主要取决于RS-232电缆另一端设备的要求。通过AT,我猜它是一些调制解调器。你也懒得解释它的要求。请根据设备文档自行检查。



-SA
Please see my comment to the question. This is not a valid question; the only reason for me to place an answer is that I remember your previous question. I still don't know what was your failure or problem, so I'll only discuss some apparent problems.

Let's see:

The line Dim i As Integer = ListBox1.Items.Count - 1 is never used. It is the line remained from your previous code sample from your previous question; at that time, it also did not make any sense at all. Finally, remove it. And, come on: you should understand every single line you write, otherwise your whole work makes no sense. You should write code, not using trial-and-error approach.

As you asked before, you needed, to send the content of the list box items to the serial port. I have no idea what are in your items, but let me assume that you simply want to send the text shown in each item. In this case, use item.ToString. It can be assumed, but what you do is not conceptually correct; the type of the item is System.Object, so it can contain any data. In other cases, you should type-cast item type to its real runtime type (do you know what is that?) and get whatever data it carries.

Don't use multiple string/character concatenation ('&'). Strings are immutable. Do I even have to explain the consequences? Anyway, use string.Format instead:
http://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx[^].

The above problems tells use that you have little idea on how to write code, but are not immediate reasons of possible failure (which you, again, did not bother to explain).

I cannot see how the serial port was initialized. And it could be initialized with wrong parameters: parity, baud rate, handshake and the like. Please see: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport%28v=vs.110%29.aspx[^].

It majorly depends on the requirements of the device on the other end of your RS-232 cable. By "AT", I can guess it's some modem. You also did not bother to explain what it requires. Check it by yourself according to the device documentation.

—SA


这篇关于我在这段代码中错过了什么......它根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆