6 Replies - 1746 Views - Last Post: 11 September 2011 - 12:01 AM Rate Topic: -----

#1 AdamK90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-September 11

Serial Port in receiving file problem

Posted 09 September 2011 - 11:20 PM

I am very new to VB and I am using VB studio 2010 now. What I want to do is to receive data from another port and save it in my directory. However the port can receive my buffer from another port but could not save the file. The codes are as below:


Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

Dim string_data As String
Dim binary_data() As Byte

string_data = SerialPort1.ReadExisting
binary_data = System.Text.Encoding.ASCII.GetBytes(string_data)
SaveFile.ShowDialog()
Dim fs As IO.FileStream = New IO.FileStream(SaveFile.FileName, IO.FileMode.Create)
Dim bw As BinaryWriter = New IO.BinaryWriter(fs)
bw.Write(binary_data)
bw.Flush()
bw.Close()
fs.Close()


Thanks in advance
J

Is This A Good Question/Topic? 0
  • +

Replies To: Serial Port in receiving file problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Serial Port in receiving file problem

Posted 10 September 2011 - 09:49 AM

So you've basically saying "Does not save the file after successfully getting the data. Can someone debug it for me?" Is that right?

Does it create ANY file at all, even something that is 0k in size?
Does it create a file, but with data you think is wrong?
Do you get an error?

See FAQ #5 for debugging tutorials


FAQ (Frequently Asked Questions - Updated Aug 2011

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 AdamK90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-September 11

Re: Serial Port in receiving file problem

Posted 10 September 2011 - 09:54 AM

The situation is like tis. For example, I send a mp3 file through a port; on the receiver side, I don't know how to convert the data back to mp3 file. When I check through virtual port, the sent and received ports are having the same bytes, so I assumed the data is successfully sent over to received port.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Serial Port in receiving file problem

Posted 10 September 2011 - 10:03 AM

That doesn't answer a single question I asked.

So is the problem that you don't know how to covert a byte array to a playable MP3, or is the problem that your file never writes to the hard drive?

As a byte array you should be able to play the mp3 without ever saving it. Can you play it through your program?

In other words, I'm trying to narrow down the problem to the data, or the saving process.

http://www.dreaminco...mymusic-player/
Was This Post Helpful? 0
  • +
  • -

#5 AdamK90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-September 11

Re: Serial Port in receiving file problem

Posted 10 September 2011 - 10:23 AM

That doesn't answer a single question I asked.

So is the problem that you don't know how to covert a byte array to a playable MP3, or is the problem that your file never writes to the hard drive?

As a byte array you should be able to play the mp3 without ever saving it. Can you play it through your program?

In other words, I'm trying to narrow down the problem to the data, or the saving process.

The problem is I don't know how to convert a byte array to a file (eg playable MP3). For this line, it does not open any safe file dialog.
SaveFile.ShowDialog()


I wonder how to play the mp3 without saving it? I though it is needed to save in database and play it?
AxWindowsMediaPlayer1.URL = string_data

Is it like that?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Serial Port in receiving file problem

Posted 10 September 2011 - 10:42 AM

That doesn't even make sense in your own example.
If you think you need to save it in a database then you still wouldn't have a URL path to a file. It would be bytes in a database record that you would receive from the database. And thus still a set of bytes whether you got it from a database or got it from a serial port.

AxWindowsMeidaPlayer sucks - lets start with that. Why would you use an ActiveX control for this when you have the .NET framework behind you and thus the SoundPlayer class?

Good article to help you learn about serial ports.

In C# we would do it like this: You should be able to read enough of this to translate it to VB
Make a memory stream out of your byte array that you got via the serialport.
Make a new SoundPlayer and send it the memorystream
Tell the SoundPlayer to play.
3 lines

Stream audioStream = new MemoryStream(myByteArray);
SoundPlayer player = new SoundPlayer(audioStream);
player.Play();


MemoryStream class
SoundPlayer class
Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Serial Port in receiving file problem

Posted 11 September 2011 - 12:01 AM

The file would have to be wav to use the soundplayer class
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1