Shop OBEX P1 Docs P2 Docs Learn Events
Problem with VB serial — Parallax Forums

Problem with VB serial

WhelzornWhelzorn Posts: 256
edited 2007-11-13 19:57 in General Discussion
I might have asked this before, but I just can't get past it. I need to send a hex command (0xBB, or 187) out the serial port.
A propeller-based device I'm developing needs to see the command and do something. I can send 187 out the serial port in any number of ways (terminal, python, whatever) and they all work perfectly fine.
But when I use "SerialPort.Write(187)" in VB (after setting up the port, etc.) the propeller obviously doesn't see it as 187, because it doesn't respond.
I know the serial port is set up, because the TX LED on my USB to serial converter flashed every time I try to send it from the VB app.
Is there some difference in the way VB would handle a normal serial port vs. the virtual one created by the FTDI chip?
thanks, Justin

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-11-11 23:19
    Hi Justin, I think what is happening here is that you are transmitting the ASCII string for 187 (three separate bytes).

    You might try the following method

    Dim my_array As Byte() = New Byte(3) {&H0,&H0,&H0,&H0}·· 'create and initialize an array of bytes (a buffer)

    my_array(0)=&HBB·· 'assign 187 to element 0

    SerialPort1.Write(my_array, 0, 1)· 'transmit 187 as 1 byte

    If you need a Newline or Carriage return you would need to transmit 2 bytes like the following

    my_array(0)=&HBB·· 'assign 187 to element 0
    my_array(1)=&HD·····'assign carriage return to element 1

    SerialPort1.Write(my_array, 0, 2)· 'transmit 187 as·one byte followed by a second byte 13 (CR)

    The array gives you the option to transmit a string of bytes if needed and also elements can be added if required

    Jeff T.

    EDIT missed a curly bracket off the end of the array


    Post Edited (Unsoundcode) : 11/11/2007 11:28:27 PM GMT
  • WhelzornWhelzorn Posts: 256
    edited 2007-11-11 23:58
    If that was the case, couldn't I just say "SerialPort.Write(New Byte = 187)"? maybe not because that doesn't work, but I'll give your idea a try.

    edit: well, your idea works fine so I guess I'll stick with it. I am just not entirely sure why it's necessary...
    edit 2: well now that I think about it, I will need to send more than one byte in some cases.

    Thanks for the help!

    Post Edited (Whelzorn) : 11/12/2007 12:05:17 AM GMT
  • FranklinFranklin Posts: 4,747
    edited 2007-11-12 01:02
    Well, as I look at your posts you are sending SerialPort.Write and unsoundcode is sending SerialPort1.write wouls this make a difference?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-11-12 01:14
    Thanks for pointing that out Stephen, it actually does not make a difference my version is just some code I copied and pasted.

    The SerialPort or SerialPort1 is just a name used in declaration.

    Justin there are a couple of ways of sending one byte but I figured the array is much faster and much·more flexible. The array can be used in Write or Read statements and down the line I am confident you will find it·much more·useful than being able to send one byte.

    Jeff T.
  • WhelzornWhelzorn Posts: 256
    edited 2007-11-12 03:56
    Ok, so now I just have a VB problem: the prop sends about 15kb of data back (a jpeg) after it gets the 0xBB command. I need to write all of that data to a jpeg file, and then close the file when EOF is detected.
    If I use a terminal, capture all the data to a file, then rename it a jpg, that works fine. But if I send the 0xBB from this basic app, the FTDI chip's LED stops blinking about half way through the transfer, but it blinks the entire time when I use a terminal. I haven't actually found a way to have the VB app to send all of the data to a file (I need to use a delegate, which I don't really understand) but the USB to serial converter's LED should indicate transfer regardless, right?? Obviously not, but I'm not sure why.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-11-12 06:23
    Do you have a sample of how you are reading and storing the data in the VB application.

    I gave this link in another post recently, and although I still have a hard job getting my head around delegates at times this article really helped me understand the basics.

    http://www.developerfusion.co.uk/show/5251/

    Jeff T.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-11-13 19:57
    I am not sure why the serial should hang, possibly if you are trying to write to disk as you receive then you·might miss incoming data. You could try declaring the array a little differently and try reading the image file into the array before writing to disk. If everything is being caught in a terminal it seems the Prop transmission is good.

    Dim my_array(20000) as Byte···

    Dim index as Int16=0

    Then in the Data_Received event

    Serialport.Read(my_array,index,1)

    If my_array(index) = EOF Then

    index=0

    Goto the write array to·file routine

    Else

    index +=1

    EndIf

    Jeff T.
Sign In or Register to comment.